Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmauro committed Dec 19, 2017
0 parents commit acacc9b
Show file tree
Hide file tree
Showing 19 changed files with 178 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SortedValueMap</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
Binary file added bin/IPair.class
Binary file not shown.
Binary file added bin/Pair.class
Binary file not shown.
Binary file added bin/PairFactory.class
Binary file not shown.
Binary file added bin/Person.class
Binary file not shown.
Binary file added bin/PersonPairByEmail.class
Binary file not shown.
Binary file added bin/PersonPairFactory.class
Binary file not shown.
Binary file added bin/SortedMap.class
Binary file not shown.
Binary file added bin/Test.class
Binary file not shown.
5 changes: 5 additions & 0 deletions src/IPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

public interface IPair<K, V> extends Comparable<IPair<K, V>> {
public K getKey();
public V getValue();
}
22 changes: 22 additions & 0 deletions src/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


public abstract class Pair<K,V> implements IPair<K,V>{
private K key;
private V value;

public Pair(K key, V value) {
super();
this.key = key;
this.value = value;
}

@Override
public K getKey() {
return key;
}

@Override
public V getValue() {
return value;
}
}
4 changes: 4 additions & 0 deletions src/PairFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public interface PairFactory<K, V, R extends IPair<K, V>>{
public R create(K key, V value);
}
37 changes: 37 additions & 0 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

public class Person {
private Long id;
private String email;
private int age;

public Person(){}

public Person(Long id, String email, int age) {
super();
this.id = id;
this.email = email;
this.age = age;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}
13 changes: 13 additions & 0 deletions src/PersonPairByEmail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

public class PersonPairByEmail extends Pair<Long, Person>{

public PersonPairByEmail(Long key, Person value) {
super(key, value);
}

@Override
public int compareTo(IPair<Long, Person> o) {
return this.getValue().getEmail().compareTo(o.getValue().getEmail());
}

}
9 changes: 9 additions & 0 deletions src/PersonPairFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

public class PersonPairFactory implements PairFactory<Long, Person, PersonPairByEmail>{

@Override
public PersonPairByEmail create(Long key, Person value) {
return new PersonPairByEmail(key, value);
}

}
30 changes: 30 additions & 0 deletions src/SortedMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SortedMap <K, V>{
private Map<K, IPair<K, V>> map;
private PairFactory<K, V, ? extends IPair<K, V>> factory;

public SortedMap(PairFactory<K, V, ? extends IPair<K, V>> factory){
this.factory = factory;
map = new HashMap<K, IPair<K, V>>();
}

public IPair<K, V> put(K key, V value) {
return map.put(key, factory.create(key, value));
//return map.put(key, new Pair<K, V>(key, value));
//http://stackoverflow.com/questions/75175/create-instance-of-generic-type-in-java
}

public List<IPair<K, V>> sortByValue(){
List<IPair<K, V>> l = new ArrayList<IPair<K, V>>(map.values());
Collections.sort(l);
return l;
}


}
24 changes: 24 additions & 0 deletions src/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.List;


public class Test {

public static void main(String[] args) {
Person p1 = new Person (1l, "doe@gmail.com", 47);
Person p2 = new Person (2L, "andre@gmail.com", 21);
Person p3 = new Person (3L, "zanetti@gmail.com", 34);

SortedMap<Long, Person> s = new SortedMap<Long, Person>(new PersonPairFactory());

s.put(p1.getId(), p1);
s.put(p2.getId(), p2);
s.put(p3.getId(), p3);

List<IPair<Long, Person>> sortByValue = s.sortByValue();

for (IPair<Long, Person> i : sortByValue){
System.out.println(i.getValue().getEmail());
}
}

}

0 comments on commit acacc9b

Please sign in to comment.