public class FixedOrderComparator<T> extends Objectimplements Comparator <T>, Serializable
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.
This class is Serializable from Commons Collections 4.0.
| Modifier and Type | Class and Description |
|---|---|
static class |
FixedOrderComparator
Unknown object behavior enum.
|
| Constructor and Description |
|---|
FixedOrderComparator()
Constructs an empty FixedOrderComparator.
|
FixedOrderComparator(List
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.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
boolean |
equals(Object
Returns
true iff
that Object is is a
Comparator whose ordering is known to be equivalent to mine.
|
FixedOrderComparator |
getUnknownObjectBehavior()
Gets the behavior for comparing unknown objects.
|
int |
hashCode()
Implement a hash code for this comparator that is consistent with
equals.
|
boolean |
isLocked()
Returns true if modifications cannot be made to the FixedOrderComparator.
|
void |
setUnknownObjectBehavior(FixedOrderComparator
Sets the behavior for comparing unknown objects.
|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitcomparing, comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongpublic FixedOrderComparator()
public FixedOrderComparator(T... items)
The array is copied, so later changes will not affect the comparator.
items - the items that the comparator can compare in order
NullPointerException - if the array is null
public FixedOrderComparator(List<T> items)
The list is copied, so later changes will not affect the comparator.
items - the items that the comparator can compare in order
NullPointerException - if the list is null
public boolean isLocked()
protected void checkLocked()
UnsupportedOperationException - if the comparator is locked
public FixedOrderComparator.UnknownObjectBehavior getUnknownObjectBehavior()
public void setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior unknownObjectBehavior)
unknownObjectBehavior - the flag for unknown behaviour - UNKNOWN_AFTER, UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTION
UnsupportedOperationException - if a comparison has been performed
NullPointerException - if unknownObjectBehavior is null
public boolean add(T obj)
obj - the item to be added to the Comparator.
UnsupportedOperationException - if a comparison has already been made
public boolean addAsEqual(T existingObj, T newObj)
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
public int compare(T obj1, T obj2)
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.
compare in interface
Comparator<T>
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(UnknownObjectBehavior).
public int hashCode()
equals.
public boolean equals(Objectobject)
true iff
that Object is is a
Comparator whose ordering is known to be equivalent to mine.
This implementation returns true iff that is a FixedOrderComparator whose attributes are equal to mine.
equals in interface
Comparator<T>
equals in class
Object
object - the object to compare to