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 |
|---|---|
| public static enum | FixedOrderComparator.UnknownObjectBehavior
Unknown object behavior enum. |
| Modifier and Type | Field and Description |
|---|---|
| private int | counter
Counter used in determining the position in the map |
| private boolean | isLocked
Is the comparator locked against further change |
| private final Map | map
Internal map of object to position |
| private static final long | serialVersionUID
Serialization version from Collections 4.0. |
| private FixedOrderComparator. | unknownObjectBehavior
The behaviour in the case of an unknown object |
| Access | Constructor and Description |
|---|---|
| public | |
| public | FixedOrderComparator(final T...
the items that the comparator can compare in order items)Constructs a FixedOrderComparator which uses the order of the given array to compare the objects. |
| public | FixedOrderComparator(final List<T>
the items that the comparator can compare in order items)Constructs a FixedOrderComparator which uses the order of the given list to compare the objects. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: true if obj has been added for the first time, false if it was already known to the Comparator.the item to be added to the Comparator. obj)Adds an item, which compares as after all items known to the Comparator. |
| public boolean | Returns: true if newObj has been added for the first time, false if it was already known to the Comparator.an item already in the Comparator's set of
known objects existingObj, final T an item to be added to the Comparator's set of
known objects newObj)Adds a new item, which compares as equal to the given existing item. |
| protected void | |
| public int | Returns: negative if obj1 is less, positive if greater, zero if equalthe first object to compare obj1, final T the second object to compare obj2)Implements java. |
| public boolean | Returns: true if equalthe object to compare to object)Overrides java. Implements java. true iff that Object is
is a Comparator whose ordering is known to be
equivalent to mine.
|
| public FixedOrderComparator. | Returns: UnknownObjectBehaviorGets the behavior for comparing unknown objects. |
| public int | Returns: a hash code for this comparator.Overrides java. equals.
|
| public boolean | Returns: true if attempts to change the FixedOrderComparator yield an UnsupportedOperationException, false if it can be changed.Returns true if modifications cannot be made to the FixedOrderComparator. |
| public void | setUnknownObjectBehavior(final FixedOrderComparator.
the flag for unknown behaviour -
UNKNOWN_AFTER, UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTION unknownObjectBehaviorSets the behavior for comparing unknown objects. |
| counter | back to summary |
|---|---|
| private int counter Counter used in determining the position in the map | |
| isLocked | back to summary |
|---|---|
| private boolean isLocked Is the comparator locked against further change | |
| map | back to summary |
|---|---|
| private final Map<T, Integer> map Internal map of object to position | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialization version from Collections 4.0. | |
| unknownObjectBehavior | back to summary |
|---|---|
| private FixedOrderComparator. The behaviour in the case of an unknown object | |
| FixedOrderComparator | back to summary |
|---|---|
| public FixedOrderComparator() Constructs an empty FixedOrderComparator. | |
| FixedOrderComparator | back to summary |
|---|---|
| public FixedOrderComparator(final T... items) 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.
| |
| FixedOrderComparator | back to summary |
|---|---|
| public FixedOrderComparator(final List<T> items) 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.
| |
| add | back to summary |
|---|---|
| public boolean add(final T obj) 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.
| |
| addAsEqual | back to summary |
|---|---|
| public boolean addAsEqual(final T existingObj, final T newObj) Adds a new item, which compares as equal to the given existing item.
| |
| checkLocked | back to summary |
|---|---|
| protected void checkLocked() Checks to see whether the comparator is now locked against further changes.
| |
| compare | back to summary |
|---|---|
| public int compare(final T obj1, final T obj2) Implements java. 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 unrecognized object. This is not specified in the Comparator interface, but is the most appropriate exception.
| |
| equals | back to summary |
|---|---|
| public boolean equals(final Object object) Overrides java. Implements java. Returns
This implementation returns | |
| getUnknownObjectBehavior | back to summary |
|---|---|
| public FixedOrderComparator. Gets the behavior for comparing unknown objects. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Implement a hash code for this comparator that is consistent with
| |
| isLocked | back to summary |
|---|---|
| public boolean isLocked() Returns true if modifications cannot be made to the FixedOrderComparator. FixedOrderComparators cannot be modified once they have performed a comparison.
| |
| setUnknownObjectBehavior | back to summary |
|---|---|
| public void setUnknownObjectBehavior(final FixedOrderComparator. Sets the behavior for comparing unknown objects.
| |
| Modifier and Type | Field and Description |
|---|---|
| public static final FixedOrderComparator. | |
| public static final FixedOrderComparator. | |
| public static final FixedOrderComparator. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static FixedOrderComparator. | |
| public static FixedOrderComparator. |
| AFTER | back to summary |
|---|---|
| public static final FixedOrderComparator. | |
| BEFORE | back to summary |
|---|---|
| public static final FixedOrderComparator. | |
| EXCEPTION | back to summary |
|---|---|
| public static final FixedOrderComparator. | |
| UnknownObjectBehavior | back to summary |
|---|---|
| private UnknownObjectBehavior() | |
| valueOf | back to summary |
|---|---|
| public static FixedOrderComparator. | |
| values | back to summary |
|---|---|
| public static FixedOrderComparator. | |