org.axiondb.util
public class ComparatorChain extends Object implements Comparator, Serializable
A ComparatorChain is a Comparator that wraps one or more Comparators in sequence. The ComparatorChain calls each Comparator in sequence until either 1) any single Comparator returns a non-zero result (and that result is then returned), or 2) the ComparatorChain is exhausted (and zero is returned). This type of sorting is very similar to multi-column sorting in SQL, and this class allows Java classes to emulate that kind of behaviour when sorting a List.
To further facilitate SQL-like sorting, the order of any single Comparator in the list can be reversed.
Calling a method that adds new Comparators or changes the ascend/descend sort after compare(Object, Object) has been called will result in an UnsupportedOperationException. However, take care to not alter the underlying List of Comparators or the BitSet that defines the sort order.
Instances of ComparatorChain are not synchronized. The class is not thread-safe at construction time, but it is thread-safe to perform multiple comparisons after all the setup operations are complete.
Version: $Revision: 1.1 $ $Date: 2005/10/13 02:25:32 $
Field Summary | |
---|---|
protected List | comparatorChain |
protected boolean | isLocked |
protected BitSet | orderingBits |
Constructor Summary | |
---|---|
ComparatorChain()
Construct a ComparatorChain with no Comparators. | |
ComparatorChain(Comparator comparator)
Construct a ComparatorChain with a single Comparator, sorting in the forward order
| |
ComparatorChain(Comparator comparator, boolean reverse)
Construct a Comparator chain with a single Comparator, sorting in the given order
| |
ComparatorChain(List list)
Construct a ComparatorChain from the Comparators in the List. | |
ComparatorChain(List list, BitSet bits)
Construct a ComparatorChain from the Comparators in the given List. |
Method Summary | |
---|---|
void | addComparator(Comparator comparator)
Add a Comparator to the end of the chain using the forward sort order
|
void | addComparator(Comparator comparator, boolean reverse)
Add a Comparator to the end of the chain using the given sort order
|
int | compare(Object o1, Object o2)
Perform comparisons on the Objects as per Comparator.compare(o1,o2).
|
boolean | equals(Object that)
Returns true iff that Object is is a Comparator
whose ordering is known to be equivalent to mine.
|
int | hashCode()
Implement a hash code for this comparator that is consistent with ComparatorChain.
|
boolean | isLocked()
Determine if modifications can still be made to the ComparatorChain.
|
void | setComparator(int index, Comparator comparator)
Replace the Comparator at the given index, maintaining the existing sort order.
|
void | setComparator(int index, Comparator comparator, boolean reverse)
Replace the Comparator at the given index in the ComparatorChain, using the given
sort order
|
void | setForwardSort(int index)
Change the sort order at the given index in the ComparatorChain to a forward sort.
|
void | setReverseSort(int index)
Change the sort order at the given index in the ComparatorChain to a reverse sort.
|
int | size()
Number of Comparators in the current ComparatorChain.
|
Parameters: comparator First comparator in the Comparator chain
Parameters: comparator First Comparator in the ComparatorChain reverse false = forward sort; true = reverse sort
Parameters: list List of Comparators
See Also: ComparatorChain
Parameters: list List of Comparators. NOTE: This constructor does not perform a defensive copy of the list bits Sort order for each Comparator. Extra bits are ignored, unless extra Comparators are added by another method.
Parameters: comparator Comparator with the forward sort order
Parameters: comparator Comparator to add to the end of the chain reverse false = forward sort order; true = reverse sort order
Parameters: o1 object 1 o2 object 2
Returns: -1, 0, or 1
Throws: UnsupportedOperationException if the ComparatorChain does not contain at least one Comparator
true
iff that Object is is a Comparator
whose ordering is known to be equivalent to mine.
This implementation returns true
iff
that.
equals
Object#getClass getClass()
this.getClass()
, and the underlying comparators and order bits are
equal. Subclasses may want to override this behavior to remain consistent with the
Comparator#equals contract.
Since: Commons Collections 3.0
Since: Commons Collections 3.0
Returns: true = ComparatorChain cannot be modified; false = ComparatorChain can still be modified.
Parameters: index index of the Comparator to replace comparator Comparator to place at the given index
Throws: IndexOutOfBoundsException if index < 0 or index >= size()
Parameters: index index of the Comparator to replace comparator Comparator to set reverse false = forward sort order; true = reverse sort order
Parameters: index Index of the ComparatorChain
Parameters: index Index of the ComparatorChain
Returns: Comparator count