com.aliasi.util
Class ReversibleMTFListComparator<E>

java.lang.Object
  extended by com.aliasi.util.ReversibleMTFListComparator<E>
All Implemented Interfaces:
Comparator<E>

public class ReversibleMTFListComparator<E>
extends Object
implements Comparator<E>

A ReversibleMTFListComparator contains an ordered list of comparators, any member of which can be reversed or moved to the front of the list. The result of comparison is defined to be the first non-zero result from the contained comparators evaluated in order, or zero if they all return zero. A reversible move-to-front list comparator is constructed with an array of comparators, and will be initialized with them in the order supplied and in their original polarity (not reversed). At any point, a comparator may be reversed using reverse(Comparator), or may be moved to the front of the list with moveToFront(Comparator).

This class is useful for maintaining ordering on columns in tables, where the ordering of sort on a column may be reversed, and where sorts should be stable except for reordering the column in focus.

To provide concurrent access to reversible move-to-front list comparators, they must be concurrent-read/single-write (CRSW) locked, where the methods moveToFront(Comparator), and reverse(Comparator) are writers, and the methods compare(Object,Object) and sort(Object[]) are readers. Exclusive locking on all access suffices for safety, but the result is not as live as CRSW synchronization.

Implementation Note:  The original comparators are maintained in a hash map, so their equality and hashcode methods must be stable after construction of a list comparator. Having comparators simply inherit implementations of Object.hashCode() and Object.equals(Object) from Object suffices.

Since:
LingPipe1.0
Version:
3.0
Author:
Bob Carpenter

Constructor Summary
ReversibleMTFListComparator(Comparator<? super E>[] comparators)
          Construct a reversible move-to-front comparator containing reversible versions of the specified comparators.
 
Method Summary
 int compare(E obj1, E obj2)
          Compares the specified objects using this list comparator, returning a positive number if the first is greater than the second, a negative number if the first is less than the second, and zero if they are equal.
 void moveToFront(Comparator comparator)
          Moves the specified comparator to the front of the list of comparators.
 void reverse(Comparator comparator)
          Reverse the ordering on the specified comparator.
<F extends E>
void
sort(F[] xs)
          Sort the specified array of objects using this comparator.
 
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

ReversibleMTFListComparator

public ReversibleMTFListComparator(Comparator<? super E>[] comparators)
Construct a reversible move-to-front comparator containing reversible versions of the specified comparators.

Parameters:
comparators - Comparators to use, in order.
Method Detail

moveToFront

public void moveToFront(Comparator comparator)
Moves the specified comparator to the front of the list of comparators.

Parameters:
comparator - Comparator to move to the front of the list.
Throws:
IllegalArgumentException - If the comparator specified is not one of the comparators supplied at construction time.

reverse

public void reverse(Comparator comparator)
Reverse the ordering on the specified comparator.

Parameters:
comparator - Comparator whose order is reversed.
Throws:
IllegalArgumentException - If the comparator specified is not one of the comparators supplied at construction time.

compare

public int compare(E obj1,
                   E obj2)
Compares the specified objects using this list comparator, returning a positive number if the first is greater than the second, a negative number if the first is less than the second, and zero if they are equal. The contained comparators, either reversed or in their original order, are evaluated in order until one produces a non-zero result, which is then returned. If the contained comparators all return zero, zero is returned from this method.

Specified by:
compare in interface Comparator<E>
Parameters:
obj1 - First object to compare.
obj2 - Second object to compare.
Returns:
A positive integer if the first object is greater than the second, a negative integer if the second is greater than the first, and zero if they are the same.

sort

public <F extends E> void sort(F[] xs)
Sort the specified array of objects using this comparator.

Parameters:
xs - Array of objects to sort.