public class ComparatorChain<E>
extends java.lang.Object
implements java.util.Comparator<E>, java.io.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(java.util.Comparator<E> comparator)
Construct a ComparatorChain with a single Comparator,
sorting in the forward order
|
ComparatorChain(java.util.Comparator<E> comparator,
boolean reverse)
Construct a Comparator chain with a single Comparator,
sorting in the given order
|
ComparatorChain(java.util.List<java.util.Comparator<E>> list)
Construct a ComparatorChain from the Comparators in the
List.
|
ComparatorChain(java.util.List<java.util.Comparator<E>> list,
java.util.BitSet bits)
Construct a ComparatorChain from the Comparators in the
given List.
|
Modifier and Type | Method and Description |
---|---|
void |
addComparator(java.util.Comparator<E> comparator)
Add a Comparator to the end of the chain using the
forward sort order
|
void |
addComparator(java.util.Comparator<E> comparator,
boolean reverse)
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(java.lang.Object 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,
java.util.Comparator<E> comparator)
Replace the Comparator at the given index, maintaining
the existing sort order.
|
void |
setComparator(int index,
java.util.Comparator<E> 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.
|
public ComparatorChain()
public ComparatorChain(java.util.Comparator<E> comparator)
comparator
- First comparator in the Comparator chainpublic ComparatorChain(java.util.Comparator<E> comparator, boolean reverse)
comparator
- First Comparator in the ComparatorChainreverse
- false = forward sort; true = reverse sortpublic ComparatorChain(java.util.List<java.util.Comparator<E>> list)
list
- List of ComparatorsComparatorChain(List,BitSet)
public ComparatorChain(java.util.List<java.util.Comparator<E>> list, java.util.BitSet bits)
list
- List of Comparators. NOTE: This constructor does not perform a
defensive copy of the listbits
- Sort order for each Comparator. Extra bits are ignored,
unless extra Comparators are added by another method.public void addComparator(java.util.Comparator<E> comparator)
comparator
- Comparator with the forward sort orderpublic void addComparator(java.util.Comparator<E> comparator, boolean reverse)
comparator
- Comparator to add to the end of the chainreverse
- false = forward sort order; true = reverse sort orderpublic void setComparator(int index, java.util.Comparator<E> comparator) throws java.lang.IndexOutOfBoundsException
index
- index of the Comparator to replacecomparator
- Comparator to place at the given indexjava.lang.IndexOutOfBoundsException
- if index < 0 or index >= size()public void setComparator(int index, java.util.Comparator<E> comparator, boolean reverse)
index
- index of the Comparator to replacecomparator
- Comparator to setreverse
- false = forward sort order; true = reverse sort orderpublic void setForwardSort(int index)
index
- Index of the ComparatorChainpublic void setReverseSort(int index)
index
- Index of the ComparatorChainpublic int size()
public boolean isLocked()
public int compare(E o1, E o2) throws java.lang.UnsupportedOperationException
compare
in interface java.util.Comparator<E>
o1
- the first object to compareo2
- the second object to comparejava.lang.UnsupportedOperationException
- if the ComparatorChain does not contain at least one Comparatorpublic int hashCode()
equals
.hashCode
in class java.lang.Object
public boolean equals(java.lang.Object object)
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.equals(Object)
contract.
equals
in interface java.util.Comparator<E>
equals
in class java.lang.Object
object
- the object to compare with"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"