edu.nrao.sss.sort
Class EnumSortKey<E extends Enum<E>>

java.lang.Object
  extended by edu.nrao.sss.sort.SortKey<E>
      extended by edu.nrao.sss.sort.EnumSortKey<E>
All Implemented Interfaces:
Orderable
Direct Known Subclasses:
ReflectiveEnumSortKey

public abstract class EnumSortKey<E extends Enum<E>>
extends SortKey<E>

A sort key for enumerations.

The most common way to extend this class is to subclass it into a class that also implements Comparator. The main job of the subclass is to provide the integers to this parent class. Example:

   /**
    * Sorts players according to the hand with which they bat.
    */
   public class PlayerBattingHandKey extends EnumSortKey<Handedness>
     implements Comparator<Player>
   {
     public int compare(Player p1, Player p2)
     {
       return compareObjects(p1.getBattingHand()
                             p2.getBattingHand());
     }
   }

Clients of the example PlayerBattingHandKey class are able to configure that comparator via the SortKey.setOrder(SortOrder) method, something they cannot do with the plain Comparator interface. They may then place instances of this class in a CompoundComparator.

Version Info:

$Revision: 593 $
$Date: 2007-05-07 15:54:14 -0600 (Mon, 07 May 2007) $
$Author: dharland $

Since:
2007-05-03
Author:
David M. Harland

Constructor Summary
protected EnumSortKey()
          Helps create a new instance.
 
Method Summary
protected  int compareAscending(E o1, E o2)
          Uses the natural ordering of E.
protected  int compareCustom(E o1, E o2)
          Uses a client-specified ordering
protected  int compareNatural(E o1, E o2)
          Uses the natural ordering of E.
protected  int compareObjects(E o1, E o2, SortOrder ordering)
          Returns a number less than zero, zero, or greater than zero as o1 is before, coincident with, or after o2 when sorted in according to t he given sort order.
 void setCustomOrder(List<E> ordering)
          Sets the ordering for this sort key.
 
Methods inherited from class edu.nrao.sss.sort.SortKey
compareObjects, getOrder, nullOrdering, objectsAreEqual, setOrder
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EnumSortKey

protected EnumSortKey()
Helps create a new instance.

Method Detail

setCustomOrder

public void setCustomOrder(List<E> ordering)
Sets the ordering for this sort key.

The list of elements should contain no more than one occurrence of a given element. Additional occurrences after the first are permitted but will be ignored. The list need not have an occurrence of every element of the enumeration. Any elements not present in the list will be deemed to be greater than an element that is in the list. A comparison between two unlisted elements will result in a tie.

The custom ordering provided here will be used only when the sort order is set to SortOrder.CUSTOM.

This method will not save a reference to ordering, so any changes made to the list after this method is called will not be reflected in this object.

Parameters:
ordering - a list containing elements of an enumeration in the order in which this key should sort them.

compareObjects

protected int compareObjects(E o1,
                             E o2,
                             SortOrder ordering)
Returns a number less than zero, zero, or greater than zero as o1 is before, coincident with, or after o2 when sorted in according to t he given sort order.

This method is called by compareObjects(T o1, T o2).

Overrides:
compareObjects in class SortKey<E extends Enum<E>>

compareNatural

protected int compareNatural(E o1,
                             E o2)
Uses the natural ordering of E.

Specified by:
compareNatural in class SortKey<E extends Enum<E>>

compareAscending

protected int compareAscending(E o1,
                               E o2)
Uses the natural ordering of E.

Specified by:
compareAscending in class SortKey<E extends Enum<E>>

compareCustom

protected int compareCustom(E o1,
                            E o2)
Uses a client-specified ordering



Copyright © 2009. All Rights Reserved.