public class FixedOrderComparator
extends java.lang.Object
implements java.util.Comparator
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.
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
FixedOrderComparator()
Constructs an empty FixedOrderComparator.
|
FixedOrderComparator(java.util.List items)
Constructs a FixedOrderComparator which uses the order of the given list
to compare the objects.
|
FixedOrderComparator(java.lang.Object[] items)
Constructs a FixedOrderComparator which uses the order of the given array
to compare the objects.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(java.lang.Object obj)
Adds an item, which compares as after all items known to the Comparator.
|
boolean |
addAsEqual(java.lang.Object existingObj,
java.lang.Object newObj)
Adds a new item, which compares as equal to the given existing item.
|
int |
compare(java.lang.Object obj1,
java.lang.Object 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.
|
public static final int UNKNOWN_BEFORE
public static final int UNKNOWN_AFTER
public static final int UNKNOWN_THROW_EXCEPTION
public FixedOrderComparator()
public FixedOrderComparator(java.lang.Object[] items)
The array is copied, so later changes will not affect the comparator.
items
- the items that the comparator can compare in orderjava.lang.IllegalArgumentException
- if the array is nullpublic FixedOrderComparator(java.util.List items)
The list is copied, so later changes will not affect the comparator.
items
- the items that the comparator can compare in orderjava.lang.IllegalArgumentException
- if the list is nullpublic boolean isLocked()
public int getUnknownObjectBehavior()
public void setUnknownObjectBehavior(int unknownObjectBehavior)
unknownObjectBehavior
- the flag for unknown behaviour -
UNKNOWN_AFTER, UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTIONjava.lang.UnsupportedOperationException
- if a comparison has been performedjava.lang.IllegalArgumentException
- if the unknown flag is not validpublic boolean add(java.lang.Object obj)
obj
- the item to be added to the Comparator.java.lang.UnsupportedOperationException
- if a comparison has already been madepublic boolean addAsEqual(java.lang.Object existingObj, java.lang.Object 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 objectsjava.lang.IllegalArgumentException
- if existingObject is not in the
Comparator's set of known objects.java.lang.UnsupportedOperationException
- if a comparison has already been madepublic int compare(java.lang.Object obj1, java.lang.Object 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 java.util.Comparator
obj1
- the first object to compareobj2
- the second object to comparejava.lang.IllegalArgumentException
- if obj1 or obj2 are not known
to this Comparator and an alternative behavior has not been set
via setUnknownObjectBehavior(int)
."Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"