|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.collections15.comparators.ComparatorChain<E>
public class ComparatorChain<E>
A ComparatorChain
is a Comparator
that wraps one or
more Comparator
s in sequence. The ComparatorChain
calls each Comparator
in sequence until either
Comparator
returns a non-zero result (and that result is
then returned), orComparatorChain
is exhausted
(and zero is returned).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
.
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.
Field Summary | |
---|---|
protected java.util.List<java.util.Comparator<E>> |
comparatorChain
The list of comparators in the chain. |
protected boolean |
isLocked
Whether the chain has been "locked". |
protected java.util.BitSet |
orderingBits
Order - false (clear) = ascend; true (set) = descend. |
Constructor Summary | |
---|---|
protected |
ComparatorChain(int initialCapacity)
Construct a ComparatorChain with no Comparator s
and the specified initial capacity. |
Method Summary | ||
---|---|---|
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 specified
sort order. |
|
int |
compare(E o1,
E o2)
Compares its two arguments for order. |
|
boolean |
equals(java.lang.Object object)
Indicates whether or not the specified object is equal to this ComparatorChain instance. |
|
static
|
getInstance()
Construct an empty ComparatorChain with the default initial
capacity. |
|
static
|
getInstance(java.util.Collection<java.util.Comparator<T>> comparators)
Returns a ComparatorChain from the Comparators
in the List . |
|
static
|
getInstance(java.util.Comparator<T> comparator)
Construct a ComparatorChain with a single
Comparator , sorting in the forward order. |
|
static
|
getInstance(java.util.Comparator<T> comparator,
boolean reverse)
Construct a ComparatorChain with a single
Comparator , sorting in the specified order. |
|
static
|
getInstance(java.util.List<java.util.Comparator<T>> comparators,
java.util.BitSet orderingBits)
Returns a ComparatorChain from the Comparators
in the List . |
|
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, using the forward
sort order for the new Comparator . |
|
void |
setComparator(int index,
java.util.Comparator<E> comparator,
boolean reverse)
Replace the Comparator at the given index, using the
specified sort order for the new Comparator . |
|
void |
setForwardSort(int index)
Set the sort order at the given index in the ComparatorChain
to the forward sort order of the Comparator at that
position. |
|
void |
setReverseSort(int index)
Set the sort order at the given index in the ComparatorChain
to the reverse sort order of the Comparator at that
position. |
|
int |
size()
Returns the number of Comparator s in the
ComparatorChain . |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected java.util.List<java.util.Comparator<E>> comparatorChain
protected java.util.BitSet orderingBits
protected boolean isLocked
Constructor Detail |
---|
protected ComparatorChain(int initialCapacity)
ComparatorChain
with no Comparator
s
and the specified initial capacity.
initialCapacity
- The initial capacity of the internal list of
Comparators
and ordering bits
BitSet
. A negative value indicates
that the default initial capacity of the
underlying storage elements should be used.Method Detail |
---|
public static <T> ComparatorChain<T> getInstance()
ComparatorChain
with the default initial
capacity.
public static <T> ComparatorChain<T> getInstance(java.util.Comparator<T> comparator)
ComparatorChain
with a single
Comparator
, sorting in the forward order.
comparator
- The first comparator in the ComparatorChain
.
java.lang.IllegalArgumentException
- Thrown if comparator
is
null
.public static <T> ComparatorChain<T> getInstance(java.util.Comparator<T> comparator, boolean reverse)
ComparatorChain
with a single
Comparator
, sorting in the specified order.
comparator
- The first comparator in the ComparatorChain
.reverse
- If false
, the natural order of
comparator
is used. If true
,
the reverse order of comparator
is used.
java.lang.IllegalArgumentException
- Thrown if comparator
is
null
.public static <T> ComparatorChain<T> getInstance(java.util.Collection<java.util.Comparator<T>> comparators)
ComparatorChain
from the Comparators
in the List
. All Comparators
will default to
the forward sort order.
comparators
- The Collection
of Comparators
to chain. The Comparator
are added to the
chain in the order that they are returned by the
iterator
method of the Collection
.
java.lang.IllegalArgumentException
- Thrown if comparators
is
null
, or contains any
null
elements. It may be
empty, however.public static <T> ComparatorChain<T> getInstance(java.util.List<java.util.Comparator<T>> comparators, java.util.BitSet orderingBits)
ComparatorChain
from the Comparators
in the List
. All Comparators
will default to
the forward sort order.
comparators
- The List
of Comparators
to
chain.orderingBits
- Indicates the sort order for each of the
Comparator
s in comparators
.
java.lang.IllegalArgumentException
- Thrown if comparators
is
null
, or contains any
null
elements. It may be
empty, however.
java.lang.IllegalArgumentException
- Thrown if orderingBits
is
null
, or if comparators
and orderingBits
are of
unequal lengths.public void addComparator(java.util.Comparator<E> comparator)
Comparator
to the end of the chain using the forward
sort order.
comparator
- The Comparator
to add to the end of the
chain, with the forward sort order.
java.lang.IllegalArgumentException
- Thrown if comparator
is
null
.public void addComparator(java.util.Comparator<E> comparator, boolean reverse)
Comparator
to the end of the chain using the specified
sort order.
comparator
- The Comparator
to add to the end of the
chain, with the forward sort order.reverse
- If false
, the natural order of
comparator
is used. If true
,
the reverse order of comparator
is used.
java.lang.IllegalArgumentException
- Thrown if comparator
is
null
.public void setComparator(int index, java.util.Comparator<E> comparator) throws java.lang.IndexOutOfBoundsException
Comparator
at the given index, using the forward
sort order for the new Comparator
.
index
- The index of the Comparator
to replace.comparator
- The Comparator
to place at the given
index.
java.lang.IndexOutOfBoundsException
- Thrown if index < 0
or
index >= size()
.
java.lang.IllegalArgumentException
- Thrown if comparator
is
null
.public void setComparator(int index, java.util.Comparator<E> comparator, boolean reverse)
Comparator
at the given index, using the
specified sort order for the new Comparator
.
index
- The index of the Comparator
to replace.comparator
- The Comparator
to place at the given
index.reverse
- If false
, the natural order of
comparator
is used. If true
,
the reverse order of comparator
is used.
java.lang.IndexOutOfBoundsException
- Thrown if index < 0
or
index >= size()
.
java.lang.IllegalArgumentException
- Thrown if comparator
is
null
.public void setForwardSort(int index)
ComparatorChain
to the forward sort order of the Comparator
at that
position.
index
- Index of the Comparator
in the
ComparatorChain
.
java.lang.IndexOutOfBoundsException
- Thrown if index < 0
or
index >= size()
.public void setReverseSort(int index)
ComparatorChain
to the reverse sort order of the Comparator
at that
position.
index
- Index of the Comparator
in the
ComparatorChain
.
java.lang.IndexOutOfBoundsException
- Thrown if index < 0
or
index >= size()
.public int size()
Comparator
s in the
ComparatorChain
.
public boolean isLocked()
ComparatorChain
. ComparatorChain
s cannot be
modified once they have performed a comparison.
true
if the ComparatorChain
cannot be
modified; false
if the ComparatorChain
can still be modified.public int compare(E o1, E o2)
compare
in interface java.util.Comparator<E>
o1
- The first object to compare.o2
- The second object to compare.
public int hashCode()
equals
.
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object object)
ComparatorChain
instance.
This implementation returns true
only if
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.
equals
in interface java.util.Comparator<E>
equals
in class java.lang.Object
object
- The object to compare to.
true
if object
is equal to this
instance.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |