Class FixedOrderComparator
java.lang.Object
org.apache.commons.collections.comparators.FixedOrderComparator
- All Implemented Interfaces:
- Comparator
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.
- Since:
- Commons Collections 3.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- David Leppik, Stephen Colebourne, Janek Bogucki
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intBehavior when comparing unknown Objects: unknown objects compare as after known Objects.static final intBehavior when comparing unknown Objects: unknown objects compare as before known Objects.static final intBehavior when comparing unknown Objects: unknown objects cause a IllegalArgumentException to be thrown.
- 
Constructor SummaryConstructorsConstructorDescriptionConstructs an empty FixedOrderComparator.FixedOrderComparator(Object[] items) Constructs a FixedOrderComparator which uses the order of the given array to compare the objects.FixedOrderComparator(List items) Constructs a FixedOrderComparator which uses the order of the given list to compare the objects.
- 
Method SummaryModifier and TypeMethodDescriptionbooleanAdds an item, which compares as after all items known to the Comparator.booleanaddAsEqual(Object existingObj, Object newObj) Adds a new item, which compares as equal to the given existing item.protected voidChecks to see whether the comparator is now locked against further changes.intCompares two objects according to the order of this Comparator.intGets the behavior for comparing unknown objects.booleanisLocked()Returns true if modifications cannot be made to the FixedOrderComparator.voidsetUnknownObjectBehavior(int unknownObjectBehavior) Sets the behavior for comparing unknown objects.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Comparatorequals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
- 
Field Details- 
UNKNOWN_BEFOREpublic static final int UNKNOWN_BEFOREBehavior when comparing unknown Objects: unknown objects compare as before known Objects.- See Also:
 
- 
UNKNOWN_AFTERpublic static final int UNKNOWN_AFTERBehavior when comparing unknown Objects: unknown objects compare as after known Objects.- See Also:
 
- 
UNKNOWN_THROW_EXCEPTIONpublic static final int UNKNOWN_THROW_EXCEPTIONBehavior when comparing unknown Objects: unknown objects cause a IllegalArgumentException to be thrown. This is the default behavior.- See Also:
 
 
- 
- 
Constructor Details- 
FixedOrderComparatorpublic FixedOrderComparator()Constructs an empty FixedOrderComparator.
- 
FixedOrderComparatorConstructs 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. - Parameters:
- items- the items that the comparator can compare in order
- Throws:
- IllegalArgumentException- if the array is null
 
- 
FixedOrderComparatorConstructs 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. - Parameters:
- items- the items that the comparator can compare in order
- Throws:
- IllegalArgumentException- if the list is null
 
 
- 
- 
Method Details- 
isLockedpublic boolean isLocked()Returns true if modifications cannot be made to the FixedOrderComparator. FixedOrderComparators cannot be modified once they have performed a comparison.- Returns:
- true if attempts to change the FixedOrderComparator yield an UnsupportedOperationException, false if it can be changed.
 
- 
checkLockedprotected void checkLocked()Checks to see whether the comparator is now locked against further changes.- Throws:
- UnsupportedOperationException- if the comparator is locked
 
- 
getUnknownObjectBehaviorpublic int getUnknownObjectBehavior()Gets the behavior for comparing unknown objects.- Returns:
- the flag for unknown behaviour - UNKNOWN_AFTER, UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTION
 
- 
setUnknownObjectBehaviorpublic void setUnknownObjectBehavior(int unknownObjectBehavior) Sets the behavior for comparing unknown objects.- Parameters:
- unknownObjectBehavior- the flag for unknown behaviour - UNKNOWN_AFTER, UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTION
- Throws:
- UnsupportedOperationException- if a comparison has been performed
- IllegalArgumentException- if the unknown flag is not valid
 
- 
addAdds 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.- Parameters:
- obj- the item to be added to the Comparator.
- Returns:
- true if obj has been added for the first time, false if it was already known to the Comparator.
- Throws:
- UnsupportedOperationException- if a comparison has already been made
 
- 
addAsEqualAdds a new item, which compares as equal to the given existing item.- Parameters:
- 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
- Returns:
- true if newObj has been added for the first time, false if it was already known to the Comparator.
- Throws:
- IllegalArgumentException- if existingObject is not in the Comparator's set of known objects.
- UnsupportedOperationException- if a comparison has already been made
 
- 
compareCompares 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. - Specified by:
- comparein interface- Comparator
- Parameters:
- obj1- the first object to compare
- obj2- the second object to compare
- Returns:
- negative if obj1 is less, positive if greater, zero if equal
- Throws:
- IllegalArgumentException- if obj1 or obj2 are not known to this Comparator and an alternative behavior has not been set via- setUnknownObjectBehavior(int).
 
 
-