Holger's Java API

## com.antelmann.util
Class MethodComparator<T>

    java.lang.Object
      com.antelmann.util.MethodComparator<T>

All Implemented Interfaces:
  Comparator<T>

    public class MethodComparator<T>

extends Object
implements 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. If the return value in used is not Comparable, a UniversalComparator is used to handle the comparison.

Author:
  Holger Antelmann

Constructor Summary
`MethodComparator(String methodName)` calls `MethodComparator(methodName, true)`
`MethodComparator(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.
`String` `getMethodName()`
`UniversalComparator` `getUniversalComparator()` returns the embedded UniversalComparator in use for customization
`static String[]` `getUsableMethodNames(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()`

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(String methodName)

  calls `MethodComparator(methodName, true)`

### MethodComparator

    public MethodComparator(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

### getUniversalComparator

    public UniversalComparator getUniversalComparator()

  returns the embedded UniversalComparator in use for customization

### getUsableMethodNames

    public static String[] getUsableMethodNames(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.

### getMethodName

    public String getMethodName()

### isAscending

    public boolean isAscending()

### compare

    public int compare(T o1,
                       T o2)
                throws 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 `Comparator<T>`
  Throws:
    `ClassCastException` - if any of the following Exceptions occur: InvocationTargetException, NoSuchMethodException, NoSuchMethodException

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads