Class ComparatorChain<E>
- Type Parameters:
- E- the type of objects compared by this comparator
- All Implemented Interfaces:
- Serializable,- Comparator<E>
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.
- Since:
- 2.0
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionConstructs a ComparatorChain with no Comparators.ComparatorChain(Comparator<E> comparator) Constructs a ComparatorChain with a single Comparator, sorting in the forward orderComparatorChain(Comparator<E> comparator, boolean reverse) Constructs a Comparator chain with a single Comparator, sorting in the given orderComparatorChain(List<Comparator<E>> list) Constructs a ComparatorChain from the Comparators in the List.ComparatorChain(List<Comparator<E>> list, BitSet bits) Constructs a ComparatorChain from the Comparators in the given List.
- 
Method SummaryModifier and TypeMethodDescriptionvoidaddComparator(Comparator<E> comparator) Add a Comparator to the end of the chain using the forward sort ordervoidaddComparator(Comparator<E> comparator, boolean reverse) Add a Comparator to the end of the chain using the given sort orderintPerform comparisons on the Objects as per Comparator.compare(o1,o2).booleanReturnstrueiff that Object is aComparatorwhose ordering is known to be equivalent to mine.inthashCode()Implement a hash code for this comparator that is consistent withequals.booleanisLocked()Determine if modifications can still be made to the ComparatorChain.voidsetComparator(int index, Comparator<E> comparator) Replace the Comparator at the given index, maintaining the existing sort order.voidsetComparator(int index, Comparator<E> comparator, boolean reverse) Replace the Comparator at the given index in the ComparatorChain, using the given sort ordervoidsetForwardSort(int index) Change the sort order at the given index in the ComparatorChain to a forward sort.voidsetReverseSort(int index) Change the sort order at the given index in the ComparatorChain to a reverse sort.intsize()Number of Comparators in the current ComparatorChain.Methods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Comparatorreversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
- 
Constructor Details- 
ComparatorChainpublic ComparatorChain()Constructs a ComparatorChain with no Comparators. You must add at least one Comparator before calling the compare(Object,Object) method, or an UnsupportedOperationException is thrown
- 
ComparatorChainConstructs a ComparatorChain with a single Comparator, sorting in the forward order- Parameters:
- comparator- First comparator in the Comparator chain
 
- 
ComparatorChainConstructs a Comparator chain with a single Comparator, sorting in the given order- Parameters:
- comparator- First Comparator in the ComparatorChain
- reverse- false = forward sort; true = reverse sort
 
- 
ComparatorChainConstructs a ComparatorChain from the Comparators in the List. All Comparators will default to the forward sort order.- Parameters:
- list- List of Comparators
- See Also:
 
- 
ComparatorChainConstructs 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.- 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.
 
 
- 
- 
Method Details- 
addComparatorAdd a Comparator to the end of the chain using the forward sort order- Parameters:
- comparator- Comparator with the forward sort order
 
- 
addComparatorAdd a Comparator to the end of the chain using the given sort order- Parameters:
- comparator- Comparator to add to the end of the chain
- reverse- false = forward sort order; true = reverse sort order
 
- 
comparePerform comparisons on the Objects as per Comparator.compare(o1,o2).- Specified by:
- comparein interface- Comparator<E>
- Parameters:
- o1- the first object to compare
- o2- the second object to compare
- Returns:
- -1, 0, or 1
- Throws:
- UnsupportedOperationException- if the ComparatorChain does not contain at least one Comparator
 
- 
equalsReturnstrueiff that Object is aComparatorwhose ordering is known to be equivalent to mine.This implementation returns trueiff<em>object</em>.{@link Object#getClass() getClass()}equalsthis.getClass(), and the underlying comparators and order bits are equal. Subclasses may want to override this behavior to remain consistent with theComparator.equals(Object)contract.- Specified by:
- equalsin interface- Comparator<E>
- Overrides:
- equalsin class- Object
- Parameters:
- object- the object to compare with
- Returns:
- true if equal
- Since:
- 3.0
 
- 
hashCode
- 
isLockedDetermine if modifications can still be made to the ComparatorChain. ComparatorChains cannot be modified once they have performed a comparison.- Returns:
- true = ComparatorChain cannot be modified; false = ComparatorChain can still be modified.
 
- 
setComparatorReplace the Comparator at the given index, maintaining the existing sort order.- Parameters:
- index- index of the Comparator to replace
- comparator- Comparator to place at the given index
- Throws:
- IndexOutOfBoundsException- if index < 0 or index >= size()
 
- 
setComparatorReplace the Comparator at the given index in the ComparatorChain, using the given sort order- Parameters:
- index- index of the Comparator to replace
- comparator- Comparator to set
- reverse- false = forward sort order; true = reverse sort order
 
- 
setForwardSortChange the sort order at the given index in the ComparatorChain to a forward sort.- Parameters:
- index- Index of the ComparatorChain
 
- 
setReverseSortChange the sort order at the given index in the ComparatorChain to a reverse sort.- Parameters:
- index- Index of the ComparatorChain
 
- 
size
 
-