edu.nrao.sss.sort
Class ReflectiveIntSortKey<T>

java.lang.Object
  extended by edu.nrao.sss.sort.SortKey<Integer>
      extended by edu.nrao.sss.sort.IntSortKey
          extended by edu.nrao.sss.sort.ReflectiveIntSortKey<T>
All Implemented Interfaces:
Orderable, Comparator<T>

public final class ReflectiveIntSortKey<T>
extends IntSortKey
implements Comparator<T>

A sort key and comparator that works with the Integers 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 Integer 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 integers returned from those methods are what is actually compared.

Because this class is a SortKey, you may configure its instances with a particular SortOrder. 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 integer: height (cm), weight (kg), age. Furthermore, assume the normal convention for the getters (e.g., getAge()). You could sort a collection of people by age, then height, then weight, without writing any new classes by doing this:

   ReflectiveIntSortKey<Person> ageKey =
     new ReflectiveIntSortKey<Person>("getAge");
     
   ReflectiveIntSortKey<Person> heightKey =
     new ReflectiveIntSortKey<Person>("getHeight");
     
   ReflectiveIntSortKey<Person> weightKey =
     new ReflectiveIntSortKey<Person>("getWeight");
     
   CompoundComparator<Person> comparator =
     new CompoundComparator<Person>(ageKey, heightKey, weightKey);
     
   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
ReflectiveIntSortKey(String methodName)
          Creates a new instance that will base its comparison on the Integer returned by the method with the given name.
 
Method Summary
 int compare(T o1, T o2)
           
 void setMethodName(String newMethodName)
          Sets the name of the method used for making comparisons.
 
Methods inherited from class edu.nrao.sss.sort.IntSortKey
compareAscending, compareNatural
 
Methods inherited from class edu.nrao.sss.sort.SortKey
compareObjects, compareObjects, getOrder, nullOrdering, objectsAreEqual, setOrder
 
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

ReflectiveIntSortKey

public ReflectiveIntSortKey(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 Integer (or int). If there is any trouble creating or calling the method with this name, it will be treated as if it returns an Integer of Integer.MIN_VALUE.
Method Detail

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.