Top Description Fields Constructors Methods
org.apache.commons.collections4.comparators

public Class ComparatorChain<E>

extends Object
implements Comparator<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.Comparator
Type Parameters
<E>
the type of objects compared by this comparator
Imports
java.io.Serializable, java.util.ArrayList, .BitSet, .Comparator, .Iterator, .List

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
2.0

Field Summary

Modifier and TypeField and Description
private final List<Comparator<E>>
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.

Constructor Summary

AccessConstructor and Description
public
ComparatorChain()

Construct a ComparatorChain with no Comparators.

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.

Note

This constructor does not perform a defensive copy of the list

list
,
final BitSet
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.

Method Summary

Modifier and TypeMethod 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
checkChainIntegrity()

Throws an exception if the ComparatorChain is empty.

private void
checkLocked()

Throws an exception if the ComparatorChain is locked.

public int

Returns:

-1, 0, or 1
compare
(final E
the first object to compare
o1
,
final E
the second object to compare
o2
)

Implements java.util.Comparator.compare.

Perform comparisons on the Objects as per Comparator.compare(o1,o2).
public boolean

Returns:

true if equal
equals
(final Object
the object to compare with
object
)

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Returns true iff that Object is is a Comparator whose ordering is known to be equivalent to mine.
public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

Implement a hash code for this comparator that is consistent with equals.
public boolean

Returns:

true = ComparatorChain cannot be modified; false = ComparatorChain can still be modified.
isLocked
()

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

Returns:

Comparator count
size
()

Number of Comparators in the current ComparatorChain.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

comparatorChainback to summary
private final List<Comparator<E>> comparatorChain

The list of comparators in the chain.

isLockedback to summary
private boolean isLocked

Whether the chain has been "locked".

orderingBitsback to summary
private BitSet orderingBits

Order - false (clear) = ascend; true (set) = descend.

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version from Collections 2.0.

Constructor Detail

ComparatorChainback 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

ComparatorChainback to summary
public ComparatorChain(final Comparator<E> comparator)

Construct a ComparatorChain with a single Comparator, sorting in the forward order

Parameters
comparator:Comparator<E>

First comparator in the Comparator chain

ComparatorChainback to summary
public ComparatorChain(final Comparator<E> comparator, final boolean reverse)

Construct a Comparator chain with a single Comparator, sorting in the given order

Parameters
comparator:Comparator<E>

First Comparator in the ComparatorChain

reverse:boolean

false = forward sort; true = reverse sort

ComparatorChainback 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.

Parameters
list:List<Comparator<E>>

List of Comparators

See Also
ComparatorChain(List, BitSet)
ComparatorChainback 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.

Parameters
list:List<Comparator<E>>

List of Comparators.

Note

This constructor does not perform a defensive copy of the list

bits:BitSet

Sort order for each Comparator. Extra bits are ignored, unless extra Comparators are added by another method.

Method Detail

addComparatorback to summary
public void addComparator(final Comparator<E> comparator)

Add a Comparator to the end of the chain using the forward sort order

Parameters
comparator:Comparator<E>

Comparator with the forward sort order

addComparatorback 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

Parameters
comparator:Comparator<E>

Comparator to add to the end of the chain

reverse:boolean

false = forward sort order; true = reverse sort order

checkChainIntegrityback to summary
private void checkChainIntegrity()

Throws an exception if the ComparatorChain is empty.

Exceptions
UnsupportedOperationException:
if the ComparatorChain is empty
checkLockedback to summary
private void checkLocked()

Throws an exception if the ComparatorChain is locked.

Exceptions
UnsupportedOperationException:
if the ComparatorChain is locked
compareback to summary
public int compare(final E o1, final E o2) throws UnsupportedOperationException

Implements java.util.Comparator.compare.

Perform comparisons on the Objects as per Comparator.compare(o1,o2).

Parameters
o1:E

the first object to compare

o2:E

the second object to compare

Returns:int

-1, 0, or 1

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the ComparatorChain does not contain at least one Comparator
equalsback to summary
public boolean equals(final Object object)

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Returns true iff that Object is is a Comparator whose ordering is known to be equivalent to mine.

This implementation returns true iff object.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:Object

the object to compare with

Returns:boolean

true if equal

Annotations
@Override
Since
3.0
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implement a hash code for this comparator that is consistent with equals.

Returns:int

a suitable hash code

Annotations
@Override
Since
3.0
isLockedback 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.

Returns:boolean

true = ComparatorChain cannot be modified; false = ComparatorChain can still be modified.

setComparatorback 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.

Parameters
index:int

index of the Comparator to replace

comparator:Comparator<E>

Comparator to place at the given index

Exceptions
IndexOutOfBoundsException:
if index < 0 or index >= size()
setComparatorback 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

Parameters
index:int

index of the Comparator to replace

comparator:Comparator<E>

Comparator to set

reverse:boolean

false = forward sort order; true = reverse sort order

setForwardSortback to summary
public void setForwardSort(final int index)

Change the sort order at the given index in the ComparatorChain to a forward sort.

Parameters
index:int

Index of the ComparatorChain

setReverseSortback to summary
public void setReverseSort(final int index)

Change the sort order at the given index in the ComparatorChain to a reverse sort.

Parameters
index:int

Index of the ComparatorChain

sizeback to summary
public int size()

Number of Comparators in the current ComparatorChain.

Returns:int

Comparator count