| |
"Dynamic Sorting With JAVA"
Vol. 9, Issue 8, p. 34
Listing 1
package simple;
import java.util.Comparator;
import java.io.Serializable;
public class EmployeeTO implements Comparator, Serializable {
private int id;
private String lastName;
private double salary;
public int getId() {
return id;
}
public String getLastName() {
return lastName;
}
public double getSalary() {
return salary;
}
public void setLastName(String string) {
lastName = string;
}
public void setSalary(double d) {
salary = d;
}
public void setId(int i) {
id = i;
}
public int compare(Object o1, Object o2) {
EmployeeTO emp1 = (EmployeeTO) o1;
EmployeeTO emp2 = (EmployeeTO) o2;
int id1 = emp1.getId();
int id2 = emp2.getId();
if (id1 == id2) return 0;
if (id1 < id2) return -1;
if (id1 > id2) return 1;
return 0;
}
public boolean equals(Object o) {
return true;
}
public String toString() {
return "id="+ id +
" lastname="+lastName+
" salary="+salary;
}
}
Listing 2
package simple;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
public class SimpleTest {
public static void main(String args[]) {
List elements = new ArrayList();
EmployeeTO e2 = new EmployeeTO();
e2.setId(2);
e2.setLastName("Nesler");
e2.setSalary(11000);
elements.add(e2);
EmployeeTO e1 = new EmployeeTO();
e1.setId(1);
e1.setLastName("Davis");
e1.setSalary(10000);
elements.add(e1);
EmployeeTO e3 = new EmployeeTO();
e3.setId(3);
e3.setLastName("Phachantry");
e3.setSalary(12000);
elements.add(e3);
Collections.sort(elements, new EmployeeTO());
final Iterator li = elements.iterator();
EmployeeTO e = null;
while (li.hasNext())
{
e = (EmployeeTO) li.next();
System.out.println(e.toString());
}
}
}
Listing 3
package dynamic;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.io.Serializable;
public final class DynamicComparator implements Comparator, Serializable {
private Collection collection;
private String method;
private boolean sortAsc;
public static final int EQUAL = 0;
public static final int LESS_THAN = -1;
public static final int GREATER_THAN = 1;
private DynamicComparator(Collection collection, String field, boolean sortAsc)
{
super();
this.collection = collection;
this.method = constructMethodName(field);
this.sortAsc = sortAsc;
}
private final static String constructMethodName(String name) {
StringBuffer fieldName = new StringBuffer("get");
fieldName.append(name.substring(0, 1).toUpperCase());
fieldName.append(name.substring(1));
return fieldName.toString();
}
public static void sort(Collection collection, String field, boolean sortAsc)
{
Collections.sort((List)collection, new DynamicComparator(collection, field,
sortAsc));
}
public int compare(Object o1, Object o2) {
boolean val1Null = false;
boolean val2Null = false;
try {
// determine the return type
Method method = getMethod(o1);
final String returnType = method.getReturnType().getName();
if (returnType.equals("java.lang.String")) {
final String f1 = (String) invoke(method, o1);
method = getMethod(o2);
final String f2 = (String) invoke(method, o2);
if (f1 != null && f2 != null) {
return f1.compareTo(f2) * getSortOrder();
}
if (f1 == null && f2 != null) {
return LESS_THAN * getSortOrder();
}
if (f1 != null && f2 == null) {
return GREATER_THAN * getSortOrder();
}
if (f1 == null && f2 == null) {
return EQUAL * getSortOrder();
}
}
if (returnType.equals("int")) {
int f1 = 0;
try {
f1 = ((Integer) invoke(method, o1)).intValue();
} catch (NullPointerException npe) {
val1Null = true;
}
method = getMethod(o2);
int f2 = 0;
try {
f2 = ((Integer) invoke(method, o2)).intValue();
} catch (NullPointerException npe) {
val2Null = true;
}
if (!val1Null && !val2Null) {
int retType = 0;
if (f1 == f2) retType = EQUAL;
if (f1 < f2) retType = LESS_THAN;
if (f1 > f2) retType = GREATER_THAN;
return retType * getSortOrder();
}
if (val1Null && !val2Null) {
return LESS_THAN * getSortOrder();
}
if (!val1Null && val2Null) {
return GREATER_THAN * getSortOrder();
}
if (val1Null && val2Null) {
return EQUAL * getSortOrder();
}
}
if (returnType.equals("double")) {
double f1 = 0;
try {
f1 = ((Double) invoke(method, o1)).doubleValue();
} catch (NullPointerException npe) {
val1Null = true;
}
method = getMethod(o2);
int f2 = 0;
try {
f2 = ((Double) invoke(method, o2)).doubleValue();
} catch (NullPointerException npe) {
val2Null = true;
}
if (!val1Null && !val2Null) {
int retType = 0;
if (f1 == f2) retType = EQUAL;
if (f1 < f2) retType = LESS_THAN;
if (f1 > f2) retType = GREATER_THAN;
return retType * getSortOrder();
}
if (val1Null && !val2Null) {
return LESS_THAN * getSortOrder();
}
if (!val1Null && val2Null) {
return GREATER_THAN * getSortOrder();
}
if (val1Null && val2Null) {
return EQUAL * getSortOrder();
}
}
throw new RuntimeException("DynamicComparator does not currently
support '" + returnType + "'!");
}
catch (NoSuchMethodException nsme) {
System.out.println("Error " + nsme);
return LESS_THAN;
}
catch (IllegalAccessException iae) {
System.out.println("Error " + iae);
return LESS_THAN;
}
catch (InvocationTargetException ite) {
System.out.println("Error " + ite);
return LESS_THAN;
}
}
/**
* Not used for sorting. Only here to meet the requirements of the Comparator
interface.
*
* @param o The object for comparison
* @return boolean
*/
public boolean equals(Object o) {
return true;
}
private final Method getMethod(Object o) throws NoSuchMethodException {
return o.getClass().getMethod(method, null);
}
private final static Object invoke(Method method, Object o) throws
InvocationTargetException, IllegalAccessException {
return method.invoke(o, null);
}
/**
*
* @return -1 to change the sort order if appropriate.
*/
private int getSortOrder() {
return sortAsc ? 1 : -1;
}
}
Listing 4
package dynamic;
public class EmployeeTO
{
private int id;
private String lastName;
private double salary;
public int getId() {
return id;
}
public String getLastName() {
return lastName;
}
public double getSalary() {
return salary;
}
public void setLastName(String string) {
lastName = string;
}
public void setSalary(double d) {
salary = d;
}
public void setId(int i) {
id = i;
}
public String toString()
{
return "id="+ id +
" lastname="+lastName+
" salary="+salary;
}
}
Listing 5
package dynamic;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class DynamicTest {
public static void main(String args[]) {
List elements = new ArrayList();
EmployeeTO e2 = new EmployeeTO();
e2.setId(2);
e2.setLastName("Nesler");
e2.setSalary(13000);
elements.add(e2);
EmployeeTO e1 = new EmployeeTO();
e1.setId(1);
e1.setLastName("Davis");
e1.setSalary(11000);
elements.add(e1);
EmployeeTO e3 = new EmployeeTO();
e3.setId(3);
e3.setLastName("Phachantry");
e3.setSalary(12000);
elements.add(e3);
DynamicComparator.sort(elements, "salary", true);
final Iterator li = elements.iterator();
EmployeeTO e = null;
while (li.hasNext())
{
e = (EmployeeTO) li.next();
System.out.println(e.toString());
}
}
}
|
|