public class ComparatorChain<E> extends Objectimplements Comparator <E>, Serializable
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.
| Constructor and Description |
|---|
ComparatorChain()
Construct a ComparatorChain with no Comparators.
|
ComparatorChain(Comparator
Construct a ComparatorChain with a single Comparator, sorting in the forward order
|
ComparatorChain(Comparator
Construct a Comparator chain with a single Comparator, sorting in the given order
|
ComparatorChain(List
Construct a ComparatorChain from the Comparators in the List.
|
ComparatorChain(List
Construct a ComparatorChain from the Comparators in the given List.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addComparator(Comparator
Add a Comparator to the end of the chain using the forward sort order
|
void |
addComparator(Comparator
Add a Comparator to the end of the chain using the given sort order
|
int |
compare(E o1, E o2)
Perform comparisons on the Objects as per Comparator.compare(o1,o2).
|
boolean |
equals(Object
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
equals.
|
boolean |
isLocked()
Determine if modifications can still be made to the ComparatorChain.
|
void |
setComparator(int index, Comparator
Replace the Comparator at the given index, maintaining the existing sort order.
|
void |
setComparator(int index, Comparator
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.
|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitcomparing, comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongpublic ComparatorChain()
public ComparatorChain(Comparator<E> comparator)
comparator - First comparator in the Comparator chain
public ComparatorChain(Comparator<E> comparator, boolean reverse)
comparator - First Comparator in the ComparatorChain
reverse - false = forward sort; true = reverse sort
public ComparatorChain(List<Comparator <E>> list)
list - List of Comparators
ComparatorChain(List,BitSet)
public ComparatorChain(List<Comparator <E>> list, BitSet bits)
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.
public void addComparator(Comparator<E> comparator)
comparator - Comparator with the forward sort order
public void addComparator(Comparator<E> comparator, boolean reverse)
comparator - Comparator to add to the end of the chain
reverse - false = forward sort order; true = reverse sort order
public void setComparator(int index,
Comparator<E> comparator)
throws IndexOutOfBoundsException
index - index of the Comparator to replace
comparator - Comparator to place at the given index
IndexOutOfBoundsException - if index < 0 or index >= size()
public void setComparator(int index,
Comparator<E> comparator,
boolean reverse)
index - index of the Comparator to replace
comparator - Comparator to set
reverse - false = forward sort order; true = reverse sort order
public void setForwardSort(int index)
index - Index of the ComparatorChain
public void setReverseSort(int index)
index - Index of the ComparatorChain
public int size()
public boolean isLocked()
public int compare(E o1, E o2) throws UnsupportedOperationException
compare in interface
Comparator<E>
o1 - the first object to compare
o2 - the second object to compare
UnsupportedOperationException - if the ComparatorChain does not contain at least one Comparator
public int hashCode()
equals.
public boolean equals(Objectobject)
true iff
that Object is is a
Comparator whose ordering is known to be equivalent to mine.
This implementation returns true iff object. equals 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 contract.
equals in interface
Comparator<E>
equals in class
Object
object - the object to compare with