|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.collections.comparators.FixedOrderComparator<T>
public class FixedOrderComparator<T>
A Comparator which imposes a specific order on a specific set of Objects.
Objects are presented to the FixedOrderComparator in a specified order and
subsequent calls to compare
yield that order.
For example:
String[] planets = {"Mercury", "Venus", "Earth", "Mars"}; FixedOrderComparator distanceFromSun = new FixedOrderComparator(planets); Arrays.sort(planets); // Sort to alphabetical order Arrays.sort(planets, distanceFromSun); // Back to original orderOnce
compare
has been called, the FixedOrderComparator is locked
and attempts to modify it yield an UnsupportedOperationException.
Instances of FixedOrderComparator 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 | |
---|---|
static int |
UNKNOWN_AFTER
Behavior when comparing unknown Objects: unknown objects compare as after known Objects. |
static int |
UNKNOWN_BEFORE
Behavior when comparing unknown Objects: unknown objects compare as before known Objects. |
static int |
UNKNOWN_THROW_EXCEPTION
Behavior when comparing unknown Objects: unknown objects cause a IllegalArgumentException to be thrown. |
Constructor Summary | |
---|---|
FixedOrderComparator()
Constructs an empty FixedOrderComparator. |
|
FixedOrderComparator(List<T> items)
Constructs a FixedOrderComparator which uses the order of the given list to compare the objects. |
|
FixedOrderComparator(T[] items)
Constructs a FixedOrderComparator which uses the order of the given array to compare the objects. |
Method Summary | |
---|---|
boolean |
add(T obj)
Adds an item, which compares as after all items known to the Comparator. |
boolean |
addAsEqual(T existingObj,
T newObj)
Adds a new item, which compares as equal to the given existing item. |
protected void |
checkLocked()
Checks to see whether the comparator is now locked against further changes. |
int |
compare(T obj1,
T obj2)
Compares two objects according to the order of this Comparator. |
int |
getUnknownObjectBehavior()
Gets the behavior for comparing unknown objects. |
boolean |
isLocked()
Returns true if modifications cannot be made to the FixedOrderComparator. |
void |
setUnknownObjectBehavior(int unknownObjectBehavior)
Sets the behavior for comparing unknown objects. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface java.util.Comparator |
---|
equals |
Field Detail |
---|
public static final int UNKNOWN_BEFORE
public static final int UNKNOWN_AFTER
public static final int UNKNOWN_THROW_EXCEPTION
Constructor Detail |
---|
public FixedOrderComparator()
public FixedOrderComparator(T[] items)
items
- the items that the comparator can compare in order
IllegalArgumentException
- if the array is nullpublic FixedOrderComparator(List<T> items)
items
- the items that the comparator can compare in order
IllegalArgumentException
- if the list is nullMethod Detail |
---|
public boolean isLocked()
protected void checkLocked()
UnsupportedOperationException
- if the comparator is lockedpublic int getUnknownObjectBehavior()
public void setUnknownObjectBehavior(int unknownObjectBehavior)
unknownObjectBehavior
- the flag for unknown behaviour -
UNKNOWN_AFTER, UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTION
UnsupportedOperationException
- if a comparison has been performed
IllegalArgumentException
- if the unknown flag is not validpublic boolean add(T obj)
obj
- the item to be added to the Comparator.
UnsupportedOperationException
- if a comparison has already been madepublic boolean addAsEqual(T existingObj, T newObj)
existingObj
- an item already in the Comparator's set of
known objectsnewObj
- an item to be added to the Comparator's set of
known objects
IllegalArgumentException
- if existingObject is not in the
Comparator's set of known objects.
UnsupportedOperationException
- if a comparison has already been madepublic int compare(T obj1, T obj2)
compare
in interface Comparator<T>
obj1
- the first object to compareobj2
- the second object to compare
IllegalArgumentException
- if obj1 or obj2 are not known
to this Comparator and an alternative behavior has not been set
via setUnknownObjectBehavior(int)
.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |