| java.lang.Object org.apache.commons.collections.comparators.ComparatorChain
ComparatorChain | public class ComparatorChain implements Comparator,Serializable(Code) | | 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.
since: Commons Collections 2.0 author: Morgan Delagrange version: $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $ |
Field Summary | |
protected List | comparatorChain The list of comparators in the chain. | protected boolean | isLocked Whether the chain has been "locked". | protected BitSet | orderingBits Order - false (clear) = ascend; true (set) = descend. |
Method Summary | |
public void | addComparator(Comparator comparator) | public void | addComparator(Comparator comparator, boolean reverse) | public int | compare(Object o1, Object o2) Perform comparisons on the Objects as per
Comparator.compare(o1,o2). | public boolean | equals(Object object) Returns true iff that Object is
is a
Comparator whose ordering is known to be
equivalent to mine. | public int | hashCode() Implement a hash code for this comparator that is consistent with
ComparatorChain.equals(Object) equals . | public boolean | isLocked() Determine if modifications can still be made to the
ComparatorChain. | public void | setComparator(int index, Comparator comparator) Replace the Comparator at the given index, maintaining
the existing sort order. | public void | setComparator(int index, Comparator comparator, boolean reverse) | public void | setForwardSort(int index) Change the sort order at the given index in the
ComparatorChain to a forward sort. | public void | setReverseSort(int index) Change the sort order at the given index in the
ComparatorChain to a reverse sort. | public int | size() Number of Comparators in the current ComparatorChain. |
comparatorChain | protected List comparatorChain(Code) | | The list of comparators in the chain.
|
isLocked | protected boolean isLocked(Code) | | Whether the chain has been "locked".
|
orderingBits | protected BitSet orderingBits(Code) | | Order - false (clear) = ascend; true (set) = descend.
|
ComparatorChain | public ComparatorChain()(Code) | | 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 | public ComparatorChain(Comparator comparator)(Code) | | Construct a ComparatorChain with a single Comparator,
sorting in the forward order
Parameters: comparator - First comparator in the Comparator chain |
ComparatorChain | public ComparatorChain(Comparator comparator, boolean reverse)(Code) | | Construct a Comparator chain with a single Comparator,
sorting in the given order
Parameters: comparator - First Comparator in the ComparatorChain Parameters: reverse - false = forward sort; true = reverse sort |
ComparatorChain | public ComparatorChain(List list, BitSet bits)(Code) | | 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.
Parameters: list - List of Comparators. NOTE: This constructor does not perform adefensive copy of the list Parameters: bits - Sort order for each Comparator. Extra bits are ignored,unless extra Comparators are added by another method. |
addComparator | public void addComparator(Comparator comparator)(Code) | | Add a Comparator to the end of the chain using the
forward sort order
Parameters: comparator - Comparator with the forward sort order |
addComparator | public void addComparator(Comparator comparator, boolean reverse)(Code) | | Add a Comparator to the end of the chain using the
given sort order
Parameters: comparator - Comparator to add to the end of the chain Parameters: reverse - false = forward sort order; true = reverse sort order |
equals | public boolean equals(Object object)(Code) | | Returns true iff that Object is
is a
Comparator whose ordering is known to be
equivalent to mine.
This implementation returns true
iff object.
Object.getClass getClass()
equals 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.
Parameters: object - the object to compare with true if equal since: Commons Collections 3.0 |
hashCode | public int hashCode()(Code) | | Implement a hash code for this comparator that is consistent with
ComparatorChain.equals(Object) equals .
a suitable hash code since: Commons Collections 3.0 |
isLocked | public boolean isLocked()(Code) | | Determine if modifications can still be made to the
ComparatorChain. ComparatorChains cannot be modified
once they have performed a comparison.
true = ComparatorChain cannot be modified; false = ComparatorChain can still be modified. |
setComparator | public void setComparator(int index, Comparator comparator) throws IndexOutOfBoundsException(Code) | | Replace the Comparator at the given index, maintaining
the existing sort order.
Parameters: index - index of the Comparator to replace Parameters: comparator - Comparator to place at the given index exception: IndexOutOfBoundsException - if index < 0 or index >= size() |
setComparator | public void setComparator(int index, Comparator comparator, boolean reverse)(Code) | | Replace the Comparator at the given index in the
ComparatorChain, using the given sort order
Parameters: index - index of the Comparator to replace Parameters: comparator - Comparator to set Parameters: reverse - false = forward sort order; true = reverse sort order |
setForwardSort | public void setForwardSort(int index)(Code) | | Change the sort order at the given index in the
ComparatorChain to a forward sort.
Parameters: index - Index of the ComparatorChain |
setReverseSort | public void setReverseSort(int index)(Code) | | Change the sort order at the given index in the
ComparatorChain to a reverse sort.
Parameters: index - Index of the ComparatorChain |
size | public int size()(Code) | | Number of Comparators in the current ComparatorChain.
Comparator count |
|
|