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.
| Modifier and Type | Field and Description |
|---|---|
| private final List | comparatorChain
The list of comparators in the chain. |
| private boolean | isLocked
Whether the chain has been "locked". |
| private BitSet | orderingBits
Order - false (clear) = ascend; true (set) = descend. |
| private static final long | serialVersionUID
Serialization version from Collections 2.0. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | ComparatorChain(final Comparator<E>
First comparator in the Comparator chain comparator)Construct a ComparatorChain with a single Comparator, sorting in the forward order |
| public | ComparatorChain(final Comparator<E>
First Comparator in the ComparatorChain comparator, final boolean false = forward sort; true = reverse sort reverse)Construct a Comparator chain with a single Comparator, sorting in the given order |
| public | ComparatorChain(final List<Comparator<E>>
List of Comparators list)Construct a ComparatorChain from the Comparators in the List. |
| public | ComparatorChain(final List<Comparator<E>>
List of Comparators. list, final BitSet Note This constructor does not perform a defensive copy of the list Sort order for each Comparator. Extra bits are ignored,
unless extra Comparators are added by another method. bits)Construct a ComparatorChain from the Comparators in the given List. |
| Modifier and Type | Method and Description |
|---|---|
| public void | addComparator(final Comparator<E>
Comparator with the forward sort order comparator)Add a Comparator to the end of the chain using the forward sort order |
| public void | addComparator(final Comparator<E>
Comparator to add to the end of the chain comparator, final boolean false = forward sort order; true = reverse sort order reverse)Add a Comparator to the end of the chain using the given sort order |
| private void | |
| private void | |
| public int | Returns: -1, 0, or 1the first object to compare o1, final E the second object to compare o2)Implements java. |
| public boolean | Returns: true if equalthe object to compare with object)Overrides java. Implements java. true iff that Object is
is a Comparator whose ordering is known to be
equivalent to mine.
|
| public int | Returns: a suitable hash codeOverrides java. equals.
|
| public boolean | Returns: true = ComparatorChain cannot be modified; false = ComparatorChain can still be modified.Determine if modifications can still be made to the ComparatorChain. |
| public void | setComparator(final int
index of the Comparator to replace index, final Comparator<E> Comparator to place at the given index comparator)Replace the Comparator at the given index, maintaining the existing sort order. |
| public void | setComparator(final int
index of the Comparator to replace index, final Comparator<E> Comparator to set comparator, final boolean false = forward sort order; true = reverse sort order reverse)Replace the Comparator at the given index in the ComparatorChain, using the given sort order |
| public void | setForwardSort(final int
Index of the ComparatorChain index)Change the sort order at the given index in the ComparatorChain to a forward sort. |
| public void | setReverseSort(final int
Index of the ComparatorChain index)Change the sort order at the given index in the ComparatorChain to a reverse sort. |
| public int |
| comparatorChain | back to summary |
|---|---|
| private final List<Comparator<E>> comparatorChain The list of comparators in the chain. | |
| isLocked | back to summary |
|---|---|
| private boolean isLocked Whether the chain has been "locked". | |
| orderingBits | back to summary |
|---|---|
| private BitSet orderingBits Order - false (clear) = ascend; true (set) = descend. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialization version from Collections 2.0. | |
| ComparatorChain | back to summary |
|---|---|
| public ComparatorChain() Construct a ComparatorChain with no Comparators. You must add at least one Comparator before calling the compare(Object,Object) method, or an UnsupportedOperationException is thrown | |
| ComparatorChain | back to summary |
|---|---|
| public ComparatorChain(final Comparator<E> comparator) Construct a ComparatorChain with a single Comparator, sorting in the forward order
| |
| ComparatorChain | back to summary |
|---|---|
| public ComparatorChain(final Comparator<E> comparator, final boolean reverse) Construct a Comparator chain with a single Comparator, sorting in the given order
| |
| ComparatorChain | back to summary |
|---|---|
| public ComparatorChain(final List<Comparator<E>> list) Construct a ComparatorChain from the Comparators in the List. All Comparators will default to the forward sort order.
| |
| ComparatorChain | back to summary |
|---|---|
| public ComparatorChain(final List<Comparator<E>> list, final BitSet bits) Construct a ComparatorChain from the Comparators in the given List. The sort order of each column will be drawn from the given BitSet. When determining the sort order for Comparator at index i in the List, the ComparatorChain will call BitSet.get(i). If that method returns false, the forward sort order is used; a return value of true indicates reverse sort order.
| |
| addComparator | back to summary |
|---|---|
| public void addComparator(final Comparator<E> comparator) Add a Comparator to the end of the chain using the forward sort order
| |
| addComparator | back to summary |
|---|---|
| public void addComparator(final Comparator<E> comparator, final boolean reverse) Add a Comparator to the end of the chain using the given sort order
| |
| checkChainIntegrity | back to summary |
|---|---|
| private void checkChainIntegrity() Throws an exception if the
| |
| checkLocked | back to summary |
|---|---|
| private void checkLocked() Throws an exception if the
| |
| compare | back to summary |
|---|---|
| public int compare(final E o1, final E o2) throws UnsupportedOperationException Implements java. Perform comparisons on the Objects as per Comparator.compare(o1,o2).
| |
| equals | back to summary |
|---|---|
| public boolean equals(final Object object) Overrides java. Implements java. Returns
This implementation returns | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Implement a hash code for this comparator that is consistent with
| |
| isLocked | back to summary |
|---|---|
| public boolean isLocked() Determine if modifications can still be made to the ComparatorChain. ComparatorChains cannot be modified once they have performed a comparison.
| |
| setComparator | back to summary |
|---|---|
| public void setComparator(final int index, final Comparator<E> comparator) throws IndexOutOfBoundsException Replace the Comparator at the given index, maintaining the existing sort order.
| |
| setComparator | back to summary |
|---|---|
| public void setComparator(final int index, final Comparator<E> comparator, final boolean reverse) Replace the Comparator at the given index in the ComparatorChain, using the given sort order
| |
| setForwardSort | back to summary |
|---|---|
| public void setForwardSort(final int index) Change the sort order at the given index in the ComparatorChain to a forward sort.
| |
| setReverseSort | back to summary |
|---|---|
| public void setReverseSort(final int index) Change the sort order at the given index in the ComparatorChain to a reverse sort.
| |
| size | back to summary |
|---|---|
| public int size() Number of Comparators in the current ComparatorChain.
| |