blackboard.base
Class BaseComparator

java.lang.Object
  extended by blackboard.base.BaseComparator
All Implemented Interfaces:
java.util.Comparator
Direct Known Subclasses:
GenericFieldComparator

public abstract class BaseComparator
extends java.lang.Object
implements java.util.Comparator

Base comparator class that implements ascending/descending and chaining. Comparators may be chained together in a linked list to support a succession of orderings.

Since:
Bb 6.0
Version:
$Revision: #1 $ $Date: 2005/06/27 $

Field Summary
static boolean ASCENDING
          ASCENDING constant for use in this class's constructor.
static boolean DESCENDING
          DESCENDING constant for use in this class's constructor.
 
Constructor Summary
protected BaseComparator(boolean ascending)
          Constructor.
 
Method Summary
 void appendSecondaryComparator(java.util.Comparator comparator)
          Appends the provided Comparator to the end of this linked list of comparators.
 int compare(java.lang.Object o1, java.lang.Object o2)
          Applies the lined list of comparators to resolve the objects relationship.
static BaseComparator convertToBaseComparator(java.util.Comparator comparator)
          Wraps any comparator in an instance of BaseComparator that proxies through to the supplied comparator.
protected abstract  int doCompare(java.lang.Object o1, java.lang.Object o2)
          Subclasses must override this method to provide functionality.
 boolean isAscending()
          Returns true if this comparator has been set to compare objects in ascending order.
 void setAscending(boolean ascending)
          Sets this comparators ascending/descending mode.
 
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
 

Field Detail

ASCENDING

public static final boolean ASCENDING
ASCENDING constant for use in this class's constructor.

See Also:
Constant Field Values

DESCENDING

public static final boolean DESCENDING
DESCENDING constant for use in this class's constructor.

See Also:
Constant Field Values
Constructor Detail

BaseComparator

protected BaseComparator(boolean ascending)
Constructor.

Parameters:
ascending - Provides a generic way to reverse the ordering without having to implement an additional comparator class. An object's natural ordering is by definition ascending.
Method Detail

isAscending

public final boolean isAscending()
Returns true if this comparator has been set to compare objects in ascending order. This fields allows an ordering to be reversed without modifying the underlying comparator.


setAscending

public final void setAscending(boolean ascending)
Sets this comparators ascending/descending mode. To reverse the order that a list is sorted using a comparator, one could do:
 comparator.setAscending( !comparator.isAscending() );
 


appendSecondaryComparator

public final void appendSecondaryComparator(java.util.Comparator comparator)
Appends the provided Comparator to the end of this linked list of comparators. The comparator does not have to be a subclass of BaseComparator.


compare

public final int compare(java.lang.Object o1,
                         java.lang.Object o2)
                  throws java.lang.ClassCastException,
                         java.lang.IllegalArgumentException
Applies the lined list of comparators to resolve the objects relationship. The comparators in the list are applied in order. Each time a comparator finds the objects to be equal based on its criteria, the next comparator in the list is called. This allows for arbitrarily nested 'order by' functionality.

Specified by:
compare in interface java.util.Comparator
Throws:
java.lang.ClassCastException
java.lang.IllegalArgumentException

doCompare

protected abstract int doCompare(java.lang.Object o1,
                                 java.lang.Object o2)
                          throws java.lang.ClassCastException,
                                 java.lang.IllegalArgumentException
Subclasses must override this method to provide functionality. This method follows the same rules as the methods java.util.Comparator.compare( Object, Object ). Implementations should return the value according to this comparator's ordering rules without taking into account ascending / descending.

Throws:
java.lang.ClassCastException
java.lang.IllegalArgumentException

convertToBaseComparator

public static BaseComparator convertToBaseComparator(java.util.Comparator comparator)
Wraps any comparator in an instance of BaseComparator that proxies through to the supplied comparator. The new BaseComparator instance is initialized in ASCENDING mode. If the provided comparator is already and instance of BaseComparator, then it is returned unmodified.



Copyright © 2003 Blackboard, Inc. All Rights Reserved.