org.jscience.util
Class MethodComparator<T>

java.lang.Object
  extended by org.jscience.util.MethodComparator<T>
Type Parameters:
T - DOCUMENT ME!
All Implemented Interfaces:
java.util.Comparator<T>

public class MethodComparator<T>
extends java.lang.Object
implements java.util.Comparator<T>

compares objects by the return value of a specified method name. This Comparator uses reflection to access the return values of the methods to be compared.


Constructor Summary
MethodComparator(java.lang.String methodName)
          calls MethodComparator(methodName, true)
MethodComparator(java.lang.String methodName, boolean ascending)
          the methodName (usually a 'getter'-method) must have no arguments for the objects to be compared.
 
Method Summary
 int compare(T o1, T o2)
          if the object returned by the specified method of the first given object doesn't implement Comparable, the toString() value of that object is used instead.
 java.lang.String getMethodName()
          DOCUMENT ME!
static java.lang.String[] getUsableMethodNames(java.lang.Class c, boolean comparableReturnValuesOnly)
          lists all method names of the given class that take no parameters and return something but void - and are therefore usable for a MethodComparator.
 boolean isAscending()
          DOCUMENT ME!
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

MethodComparator

public MethodComparator(java.lang.String methodName)
calls MethodComparator(methodName, true)


MethodComparator

public MethodComparator(java.lang.String methodName,
                        boolean ascending)
the methodName (usually a 'getter'-method) must have no arguments for the objects to be compared. If ascending is false, the return value of compareTo(Object) will be reversed.

Method Detail

getUsableMethodNames

public static java.lang.String[] getUsableMethodNames(java.lang.Class c,
                                                      boolean comparableReturnValuesOnly)
lists all method names of the given class that take no parameters and return something but void - and are therefore usable for a MethodComparator. If comparableReturnValuesOnly is true, the return values of the methods must also implement Comparable.

Parameters:
c - DOCUMENT ME!
comparableReturnValuesOnly - DOCUMENT ME!
Returns:
DOCUMENT ME!

getMethodName

public java.lang.String getMethodName()
DOCUMENT ME!

Returns:
DOCUMENT ME!

isAscending

public boolean isAscending()
DOCUMENT ME!

Returns:
DOCUMENT ME!

compare

public int compare(T o1,
                   T o2)
            throws java.lang.ClassCastException
if the object returned by the specified method of the first given object doesn't implement Comparable, the toString() value of that object is used instead. If a null value is encountered, it is considered to come before any other value.

Specified by:
compare in interface java.util.Comparator<T>
Parameters:
o1 - DOCUMENT ME!
o2 - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
java.lang.ClassCastException - if any of the following Exceptions occur: InvocationTargetException, NoSuchMethodException, NoSuchMethodException