public interface ComparisonContext<T> extends Equator<T>, Comparator<T>
Represents a context for comparison because sometimes you order the same things differently. For instance, a class of students might be sorted by height for the yearbook picture (shortest in front), alphabetically for role call, and by GPA at honors ceremonies. Sometimes you need to sort non-compatible classes together for some reason. If you didn't define those classes, this provides an external means of ordering them.
A Comparison Context represents both ordering and equality, since the two often need to be
defined compatibly. Implement compare() and hash() and you get a compatible eq() for free!
If you don't want ordering, use Equator
instead.
Typical implementations of Comparator.compare(Object, Object)
throw an
IllegalArgumentExceptions if one argument is null because most objects cannot be meaningfully
be orderd with respect to null. It's also OK if you want to return 0 when both arguments are null
because null == null. Default implementations of eq(), gte(), and lte() check for nulls
first, before calling compare() so they will work either way you choose to implement
compare().
A common mistake is to implement a ComparisonContext, Equator, or Comparator as an anonymous
class or lambda, then be surprised when it can't be serialized, or is deserialized as null.
These one-off classes are often singletons, which are easiest to serialize as enums. If your
implementation requires generic type parameters, look at how defCompCtx()
tricks the type
system into using generic type parameters (correctly) with an enum.
Modifier and Type | Interface and Description |
---|---|
static class |
ComparisonContext.CompCtx
Please access this type-safely through
defCompCtx() instead of calling directly. |
Equator.Comp, Equator.Equat
Modifier and Type | Method and Description |
---|---|
static <T> ComparisonContext<T> |
defCompCtx()
Returns a typed, serializable ComparisonContext that works on any class that implements
Comparable . |
default boolean |
eq(T o1,
T o2)
The default implementation of this method returns false if only one parameter is null then
checks if compare() returns zero.
|
default boolean |
gt(T o1,
T o2)
Returns true if the first object is greater than the second.
|
default boolean |
gte(T o1,
T o2)
Returns true if the first object is greater than or equal to the second.
|
default boolean |
lt(T o1,
T o2)
Returns true if the first object is less than the second.
|
default boolean |
lte(T o1,
T o2)
Returns true if the first object is less than or equal to the second.
|
default T |
max(Iterable<T> is)
Returns the maximum (as defined by this Comparison Context).
|
default T |
min(Iterable<T> is)
Returns the minimum (as defined by this Comparison Context).
|
defaultComparator, defaultEquator, hash, neq
compare, comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
default boolean lte(T o1, T o2)
default boolean gte(T o1, T o2)
default boolean eq(T o1, T o2)
default T min(Iterable<T> is)
default T max(Iterable<T> is)
static <T> ComparisonContext<T> defCompCtx()
Comparable
.Copyright © 2017. All rights reserved.