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

java.lang.Object
  extended by edu.nrao.sss.sort.SortKey<Double>
      extended by edu.nrao.sss.sort.DoubleSortKey
          extended by edu.nrao.sss.sort.ReflectiveDoubleSortKey<T>
All Implemented Interfaces:
Orderable, Comparator<T>

public final class ReflectiveDoubleSortKey<T>
extends DoubleSortKey
implements Comparator<T>

A sort key and comparator that works with the Doubles 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 Double 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 doubles 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, Pants, that has the following attributes, each of which is a double: waist, inseam, price, and a String attribute: style. Furthermore, assume the normal convention for the getters (e.g., getPrice()). You could sort a collection of pants by waist, then price, then style, then inseam, without writing any new classes by doing this:

   ReflectiveDoubleSortKey<Pants> waistKey =
     new ReflectiveDoubleSortKey<Pants>("getWaist");
     
   ReflectiveDoubleSortKey<Pants> lengthKey =
     new ReflectiveDoubleSortKey<Pants>("getInseam");
     
   ReflectiveDoubleSortKey<Pants> costKey =
     new ReflectiveDoubleSortKey<Pants>("getPrice");
     
   ReflectiveStringSortKey<Pants> styleKey =
     new ReflectiveStringSortKey<Pants>("getStyle");
     
   CompoundComparator<Pants> comparator =
     new CompoundComparator<Pants>(waistKey, priceKey, styleKey, lengthKey);
     
   Collections.sort(pantsCatalog, 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
ReflectiveDoubleSortKey(String methodName)
          Creates a new instance that will base its comparison on the Double 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.DoubleSortKey
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

ReflectiveDoubleSortKey

public ReflectiveDoubleSortKey(String methodName)
Creates a new instance that will base its comparison on the Double 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 Double (or double). If there is any trouble creating or calling the method with this name, it will be treated as if it returns a Double of Double.NaN.
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 a Double (or double). If there is any trouble creating or calling the method with this name, it will be treated as if it returns a Double of Double.NaN.

compare

public int compare(T o1,
                   T o2)
Specified by:
compare in interface Comparator<T>


Copyright © 2009. All Rights Reserved.