T - the type of object to comparepublic class RankedComparator<T> extends Object implements Comparator<T>, Serializable
Comparator that ranks other objects for comparison
purposes.
A rank for the purposes of this class is a positive
integer. One integer representing a rank outranks another
integer representing a rank if the first integer is greater than
the second. Hence, e.g., 1 outranks 0. A
negative integer cannot represent a rank and in the context of
ranking means, effectively, that any rank it might otherwise
represent is unknown.
A RankedComparator can accept an optional List
at construction time that can
be used for ranking purposes in case the type of objects the RankedComparator compares are not instances of Ranked. In
such a case, the index of an
object in a reversed copy of
the supplied List is used as its rank.
RankedComparator(List),
compare(Object, Object),
Serialized Form| Constructor and Description |
|---|
RankedComparator()
Creates a new
RankedComparator. |
RankedComparator(List<?> items)
Creates a new
RankedComparator. |
| Modifier and Type | Method and Description |
|---|---|
int |
compare(T one,
T two)
Compares two objects, returning
-1 if one
outranks two, 1 if two outranks one, and 0 if the two objects are either ranked equally
or their ranks could not be determined. |
protected Object |
getComparisonObject(T realObject)
Given an object to be compared, returns the
Object that should be used to
determine ranking (as determined by the List supplied at construction time). |
boolean |
ranks(T object)
Returns
true if this RankedComparator explicitly
ranks the supplied object. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcomparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongpublic RankedComparator()
RankedComparator.RankedComparator(List)public RankedComparator(List<?> items)
RankedComparator.items - a List of Objects that will be used
for ranking purposes; may be null; the item at position
0 is deemed to outrank all others; copied by valuegetComparisonObject(Object)public final boolean ranks(T object)
true if this RankedComparator explicitly
ranks the supplied object.object - the object to test; may be nulltrue if this RankedComparator explicitly
ranks the supplied object; false otherwisegetComparisonObject(Object)public final int compare(T one, T two)
-1 if one
outranks two, 1 if two outranks one, and 0 if the two objects are either ranked equally
or their ranks could not be determined.
This method makes use of the List supplied at construction time for
ranking information.
Non-null objects outrank null objects.
compare in interface Comparator<T>one - the first object; may be nulltwo - the second object; may be null-1 if one outranks two, 1
if two outranks one, and 0 if the two
objects are either ranked equally or their ranks could not be
determinedgetComparisonObject(Object),
RankedComparator(List)protected Object getComparisonObject(T realObject)
Object that should be used to
determine ranking (as determined by the List supplied at construction time).
This method may return null.
Overrides of this method may return null.
The default implementation of this method simply returns the
supplied realObject parameter value.
realObject - the object to be compared; may be nullObject that should be used to determine
ranking (as determined by the List supplied at construction time), possibly
nullRankedComparator(List),
compare(Object, Object)Copyright © 2017–2019, microBean. All rights reserved.