com.sas.util
Class ReverseComparator

com.sas.util.ReverseComparator
All Implemented Interfaces:
com.sas.PublicClonable, java.io.Serializable, java.lang.Cloneable, java.util.Comparator

public class ReverseComparator
implements com.sas.util.Comparator, java.io.Serializable, com.sas.PublicClonable, java.util.Comparator

A Comparator which reverses the result of another Comparator. The original comparator is called with the same arguments in the same order (since it is possible although probably not desireable) for a comparator to return inconsistent results for compare(a,b) and compare(b,a).) The result is negated and guaranteed to be one of Comparator.LESS_THAN, Comparator.EQUALS , or Comparator.GREATER_THAN , or Comparator.INCOMPARABLE.

The ReverseComparator allows you to simplify other comparators by not having to worry about ascending or descending sorts. If you write all Comparators c to assume an ascending sort context, then to sort in descending order, simply use ReverseComparator.reverse(c) to reverse it.

See Also:
Serialized Form

Field Summary
 
Fields inherited from interface com.sas.util.Comparator
EQUALS, GREATER_THAN, INCOMPARABLE, LESS_THAN
 
Constructor Summary
ReverseComparator(com.sas.util.Comparator comparator)
          Construct a Comparator which reverses the result of another comparator.
ReverseComparator(java.util.Comparator comparator)
          Construct a Comparator which reverses the result of another comparator.
 
Method Summary
 java.lang.Object clone()
          Clone this reverse comparator.
 int compare(java.lang.Object a, java.lang.Object b)
          Compare two objects using the original comparator, and reverse and return that result.
 java.util.Comparator getComparator()
          Return the root comparator with which this ReverseCompatator was constructed.
static java.util.Comparator reverse(java.util.Comparator comparator)
          Return a comparator whose sense is a reversed compararison of comparator.
 

Constructor Detail

ReverseComparator

public ReverseComparator(com.sas.util.Comparator comparator)
Construct a Comparator which reverses the result of another comparator.

Parameters:
comparator - another comparator whose comparison result you wish to reverse. This argument cannot be null.

ReverseComparator

public ReverseComparator(java.util.Comparator comparator)
Construct a Comparator which reverses the result of another comparator.

Parameters:
comparator - another comparator whose comparison result you wish to reverse. This argument cannot be null.
Since:
2.0
Method Detail

compare

public int compare(java.lang.Object a,
                   java.lang.Object b)
Compare two objects using the original comparator, and reverse and return that result.

Specified by:
compare in interface java.util.Comparator
Parameters:
a - the first object
b - the second object
Returns:
one of Comparator.LESS_THAN, Comparator.EQUALS, Comparator.GREATER_THAN, Comparator.INCOMPARABLE

clone

public java.lang.Object clone()
Clone this reverse comparator.

Specified by:
clone in interface com.sas.PublicClonable
Overrides:
clone in class java.lang.Object
Returns:
a new ReverseComparator, based on the comparator with which this ReverseComparator was instantiated.

getComparator

public java.util.Comparator getComparator()
Return the root comparator with which this ReverseCompatator was constructed.

Returns:
the root comparator passed to the constructor.
Since:
2.0

reverse

public static java.util.Comparator reverse(java.util.Comparator comparator)
Return a comparator whose sense is a reversed compararison of comparator. It is safe to invoke this multiple times, for example to toggle the sort order of a table column: for a comparator c, c = ReverseComparator.reverse(c); will sort in the reverse order. Reversing a comparator c2 twice will restore the comparator; i.e. for any comparator c, c == ReverseComparator.reverse(ReverseComparator.reverse(c))

Parameters:
comparator - a comparator
Returns:
a new comparator whose sense is a reversed compararison of comparator. If the comparator is already a ReverseComparator, return its root comparator, else return a new ReverseComparator(comparator).
Since:
2.0



Copyright © 2009 SAS Institute Inc. All Rights Reserved.