edu.nrao.sss.sort
Class ReflectiveStringSortKey<T>
java.lang.Object
edu.nrao.sss.sort.SortKey<String>
edu.nrao.sss.sort.StringSortKey
edu.nrao.sss.sort.ReflectiveStringSortKey<T>
- All Implemented Interfaces:
- Orderable, Comparator<T>
public final class ReflectiveStringSortKey<T>
- extends StringSortKey
- implements Comparator<T>
A sort key
and comparator
that works with
the String
s returned by a method that is found reflectively.
Clients of this class must provide the name of a public, no argument, method
that returns a String
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 strings 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 a StringSortKey
, you
may tell it to ignore differences in case. You may place instances of this
class in a CompoundComparator
.
Example
Let there be a class, PostalAddress, that has the following
attributes, each of which is a String:
state, city, postalCode. Furthermore, assume the normal convention
for the getters (e.g., getCity()). You could sort a collection of
addresses by state, then city, then postal code, without writing any new
classes by doing this:
ReflectiveStringSortKey<Address> stateKey =
new ReflectiveStringSortKey<Address>("getState");
ReflectiveStringSortKey<Address> cityKey =
new ReflectiveStringSortKey<Address>("getCity");
ReflectiveStringSortKey<Address> codeKey =
new ReflectiveStringSortKey<Address>("getPostalCode");
CompoundComparator<Address> comparator =
new CompoundComparator<Address>(stateKey, cityKey, codeKey);
Collections.sort(myAddresses, comparator);
Version Info:
$Revision: 593 $ |
$Date: 2007-05-07 15:54:14 -0600 (Mon, 07 May 2007) $ |
$Author: dharland $ |
- Since:
- 2007-05-04
- Author:
- David M. Harland
Constructor Summary |
ReflectiveStringSortKey(String methodName)
Creates a new instance that will base its comparison on the String
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 |
ReflectiveStringSortKey
public ReflectiveStringSortKey(String methodName)
- Creates a new instance that will base its comparison on the
String
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 a
String
. If there is any trouble creating or
calling the method with this name, it will be treated
as if it returns a String
of 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 a
String
. If there is any trouble creating or
calling the method with this name, it will be treated
as if it returns a String
of null.
compare
public int compare(T o1,
T o2)
- Specified by:
compare
in interface Comparator<T>
Copyright © 2009. All Rights Reserved.