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 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.

    • 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 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​(java.lang.Iterable<T> is)
      Returns the maximum (as defined by this Comparison Context).
      default T min​(java.lang.Iterable<T> is)
      Returns the minimum (as defined by this Comparison Context).
      • Methods inherited from interface java.util.Comparator

        compare, equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
      • Methods inherited from interface org.organicdesign.fp.collections.Equator

        hash, neq
    • Method Detail

      • lt

        default boolean lt​(T o1,
                           T o2)
        Returns true if the first object is less than the second.
      • lte

        default boolean lte​(T o1,
                            T o2)
        Returns true if the first object is less than or equal to the second.
      • gt

        default boolean gt​(T o1,
                           T o2)
        Returns true if the first object is greater than 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.
        Specified by:
        eq in interface Equator<T>
        Returns:
        true if this Equator considers the two objects to be equal.
      • 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 implements Comparable.