Interface ComparisonContext<T>
-
- All Superinterfaces:
java.util.Comparator<T>,Equator<T>
- All Known Implementing Classes:
ComparisonContext.CompCtx
public interface ComparisonContext<T> extends Equator<T>, java.util.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
Equatorinstead.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classComparisonContext.CompCtxPlease access this type-safely throughdefCompCtx()instead of calling directly.-
Nested classes/interfaces inherited from interface org.organicdesign.fp.collections.Equator
Equator.Comp, Equator.Equat
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description static <T> ComparisonContext<T>defCompCtx()Returns a typed, serializable ComparisonContext that works on any class that implementsComparable.default booleaneq(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 booleangt(T o1, T o2)Returns true if the first object is greater than the second.default booleangte(T o1, T o2)Returns true if the first object is greater than or equal to the second.default booleanlt(T o1, T o2)Returns true if the first object is less than the second.default booleanlte(T o1, T o2)Returns true if the first object is less than or equal to the second.default Tmax(java.lang.Iterable<T> is)Returns the maximum (as defined by this Comparison Context).default Tmin(java.lang.Iterable<T> is)Returns the minimum (as defined by this Comparison Context).
-
-
-
Method Detail
-
lte
default boolean lte(T o1, T o2)
Returns true if the first object is less than or equal to the second.
-
gte
default boolean gte(T o1, T o2)
Returns true if the first object is greater than or equal to the second.
-
eq
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.
-
min
default T min(java.lang.Iterable<T> is)
Returns the minimum (as defined by this Comparison Context). Nulls are skipped. If there are duplicate minimum values, the first one is returned.
-
max
default T max(java.lang.Iterable<T> is)
Returns the maximum (as defined by this Comparison Context). Nulls are skipped. If there are duplicate maximum values, the first one is returned.
-
defCompCtx
static <T> ComparisonContext<T> defCompCtx()
Returns a typed, serializable ComparisonContext that works on any class that implementsComparable.
-
-