java.lang.Object | |
↳ | org.apache.commons.collections.comparators.FixedOrderComparator |
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 order
Once 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.
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | UNKNOWN_AFTER | Behavior when comparing unknown Objects: unknown objects compare as after known Objects. | |||||||||
int | UNKNOWN_BEFORE | Behavior when comparing unknown Objects: unknown objects compare as before known Objects. | |||||||||
int | UNKNOWN_THROW_EXCEPTION | Behavior when comparing unknown Objects: unknown objects cause a IllegalArgumentException to be thrown. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs an empty FixedOrderComparator.
| |||||||||||
Constructs a FixedOrderComparator which uses the order of the given array
to compare the objects.
| |||||||||||
Constructs a FixedOrderComparator which uses the order of the given list
to compare the objects.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds an item, which compares as after all items known to the Comparator.
| |||||||||||
Adds a new item, which compares as equal to the given existing item.
| |||||||||||
Compares two objects according to the order of this Comparator.
| |||||||||||
Gets the behavior for comparing unknown objects.
| |||||||||||
Returns true if modifications cannot be made to the FixedOrderComparator.
| |||||||||||
Sets the behavior for comparing unknown objects.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Checks to see whether the comparator is now locked against further changes.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.util.Comparator
|
Behavior when comparing unknown Objects: unknown objects compare as after known Objects.
Behavior when comparing unknown Objects: unknown objects compare as before known Objects.
Behavior when comparing unknown Objects: unknown objects cause a IllegalArgumentException to be thrown. This is the default behavior.
Constructs an empty FixedOrderComparator.
Constructs a FixedOrderComparator which uses the order of the given array to compare the objects.
The array is copied, so later changes will not affect the comparator.
items | the items that the comparator can compare in order |
---|
IllegalArgumentException | if the array is null |
---|
Constructs a FixedOrderComparator which uses the order of the given list to compare the objects.
The list is copied, so later changes will not affect the comparator.
items | the items that the comparator can compare in order |
---|
IllegalArgumentException | if the list is null |
---|
Adds an item, which compares as after all items known to the Comparator. If the item is already known to the Comparator, its old position is replaced with the new position.
obj | the item to be added to the Comparator. |
---|
UnsupportedOperationException | if a comparison has already been made |
---|
Adds a new item, which compares as equal to the given existing item.
existingObj | an item already in the Comparator's set of known objects |
---|---|
newObj | 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 made |
Compares two objects according to the order of this Comparator.
It is important to note that this class will throw an IllegalArgumentException in the case of an unrecognised object. This is not specified in the Comparator interface, but is the most appropriate exception.
obj1 | the first object to compare |
---|---|
obj2 | 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) .
|
---|
Gets the behavior for comparing unknown objects.
Returns true if modifications cannot be made to the FixedOrderComparator. FixedOrderComparators cannot be modified once they have performed a comparison.
Sets the behavior for comparing unknown objects.
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 valid |
Checks to see whether the comparator is now locked against further changes.
UnsupportedOperationException | if the comparator is locked |
---|