edu.nrao.sss.sort
Class ReflectiveEnumSortKey<T,E extends Enum<E>>
java.lang.Object
edu.nrao.sss.sort.SortKey<E>
edu.nrao.sss.sort.EnumSortKey<E>
edu.nrao.sss.sort.ReflectiveEnumSortKey<T,E>
- All Implemented Interfaces:
- Orderable, Comparator<T>
public final class ReflectiveEnumSortKey<T,E extends Enum<E>>
- extends EnumSortKey<E>
- implements Comparator<T>
A sort key
and comparator
that works with
the enumeration elements returned by a method that is found reflectively.
Clients of this class must provide the name of a public, no argument, method
that returns an Enum
and that is present in classes of the
parameterized type T
. The method created from that name is called
on the objects to be compared, and the enumeration elements returned from
those methods are what is actually compared.
Because this class is a SortKey
, you may configure its instances with
a particular SortOrder
. Because it is also an EnumSortKey
,
you may specify a custom ordering. You may place instances of this
class in a CompoundComparator
.
Example
Let there be a class, Person, that has the following
attributes, each of which is an enumeration:
gender and handedness.
Furthermore, assume the normal convention
for the getters (e.g., getGender()).
If you are studying the distribution of handedness by age and gender,
you might want to sort a collection of people by age, gender, and
handedness. You could do this without writing any new
classes by doing as follows:
ReflectiveIntSortKey<Person> ageKey =
new ReflectiveIntSortKey<Person>("getAge");
ReflectiveEnumSortKey<Person,Gender> sexKey =
new ReflectiveEnumSortKey<Person,Gender>("getGender");
ReflectiveEnumSortKey<Person,Handedness> handKey =
new ReflectiveEnumSortKey<Person,Handedness>("getHandedness");
CompoundComparator<Person> comparator =
new CompoundComparator<Person>(ageKey, sexKey, handKey);
Collections.sort(population, comparator);
Version Info:
$Revision: 593 $ |
$Date: 2007-05-07 15:54:14 -0600 (Mon, 07 May 2007) $ |
$Author: dharland $ |
- Since:
- 2007-05-07
- Author:
- David M. Harland
Constructor Summary |
ReflectiveEnumSortKey(String methodName)
Creates a new instance that will base its comparison on the Integer
returned by the method with the given name. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ReflectiveEnumSortKey
public ReflectiveEnumSortKey(String methodName)
- Creates a new instance that will base its comparison on the
Integer
returned by the method with the given name.
- Parameters:
methodName
- the name of a public, no argument, method that belongs
to objects of type T
and that returns an
Enum
). If there is any
trouble creating or calling the method with this name,
it will be treated as if it returns null.
setMethodName
public void setMethodName(String newMethodName)
- Sets the name of the method used for making comparisons.
- Parameters:
newMethodName
- the name of a public, no argument, method that belongs
to objects of type T
and that returns an
Integer
(or in
). If there is any
trouble creating or calling the method with this name,
it will be treated as if it returns a Integer
of Integer.MIN_VALUE
.
compare
public int compare(T o1,
T o2)
- Specified by:
compare
in interface Comparator<T>
Copyright © 2009. All Rights Reserved.