Interface Comparators


public interface Comparators
Some utilities to make comparisons easier to do, especially with null handling
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    When you compare, the nulls can be quite annoying to handle.
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static <T> int
    compare(T a, T b)
    Compare two objects.
    static <T> int
    compare(T a, T b, int maxDepth)
    Compare two objects.
    static <T> int
    compare(T a, T b, Comparator<T> nonNullComparator)
    Protect a comparator from being called with null if they are both null, 0 if a and !b -> 1 if !a and b -> -1 Otherwise result of the nonNulComparator.compare(a,b)
    static <T> int
    compare(T a, T b, IntSupplier nonNullComparator)
    Shield the nonNullComparator from null objects.
    static int
    Compare sequential fields in a Map with compare(Object, Object).
    static <T> int
    comparePresent(T a, T b)
    Compare two objects for null.
    static <T> int
    Compare two objects for null and equality.
    static boolean
    isFinal(int compare)
    Check if compare value is final, that it decides the comparison and can be used as return value in a compare operation.
  • Field Details

    • COMPARISON_REQUIRED

      static final int COMPARISON_REQUIRED
      When you compare, the nulls can be quite annoying to handle. For this interface, we use a magic value to mark outside the normal 1,0,-1 values for compare to indicate that both values are not null and should be compared with traditional means.
      See Also:
  • Method Details

    • compare

      static <T> int compare(T a, T b, Comparator<T> nonNullComparator)
      Protect a comparator from being called with null
      • if they are both null, 0
      • if a and !b -> 1
      • if !a and b -> -1
      • Otherwise result of the nonNulComparator.compare(a,b)
      Type Parameters:
      T - the shared type
      Parameters:
      a -
      b -
      nonNullComparator -
      Returns:
      the result
    • compare

      static <T> int compare(T a, T b, IntSupplier nonNullComparator)
      Shield the nonNullComparator from null objects. This is a convenience method so you do not have to invent new parameter names but can use the direct parameters from the invoked method.
      • if they are both null, 0
      • if a and !b -> 1
      • if !a and b -> -1
      • Otherwise nonNullComparator.getAsInt()
      Type Parameters:
      T - the shared type
      Parameters:
      a -
      b -
      Returns:
      the result
    • comparePresent

      static <T> int comparePresent(T a, T b)
      Compare two objects for null.
      • if they are both null, 0
      • if a and !b -> 1
      • if !a and b -> -1
      • Otherwise Integer.MAXVALUE

      To use this, compare the result with invalid input: '<'=1 and return if so.

      Type Parameters:
      T - the shared type
      Parameters:
      a -
      b -
      Returns:
      the result
    • comparePresentEquals

      static <T> int comparePresentEquals(T a, T b)
      Compare two objects for null and equality.
      • if they are both null, 0
      • if a and !b -> 1
      • if !a and b -> -1
      • if a.equals(b) -> 0
      • Otherwise Integer.MAXVALUE

      To use this, compare the result with invalid input: '<'=1 and return if so.

      Type Parameters:
      T - the shared type
      Parameters:
      a -
      b -
      Returns:
      the result
    • isFinal

      static boolean isFinal(int compare)
      Check if compare value is final, that it decides the comparison and can be used as return value in a compare operation.
    • compare

      static <T> int compare(T a, T b)
      Compare two objects.
      • Test for null as in comparePresent(Object, Object)
      • Test for same class, if not class names are compared
      • Test if they are comparable, use the object comparison
      • If array, compare the fields of the array with this method in order, if all match, the longer is higher.
      • If Collection, compare the members of the Collection with this method in order, longer is higher
      • returns 0 in a desperate attempt. This can make objects return 0 that are not equals.
      The method compares to a depth of 4
      Type Parameters:
      T - the shared type of the objects
      Parameters:
      a -
      b -
      Returns:
      0, 1 a>b, -1 ainvalid input: '<'b
    • compare

      static <T> int compare(T a, T b, int maxDepth)
      Compare two objects.
      • Test for null as in comparePresent(Object, Object)
      • Test for same class, if not class names are compared
      • Test if they are comparable, use the object comparison
      • If array, compare the fields of the array with this method in order, if all match, the longer is higher.
      • If Collection, compare the members of the Collection with this method in order, longer is higher
      • returns 0 in a desperate attempt. This can make objects return 0 that are not equals.
      The method compares to a depth of 4
      Type Parameters:
      T - the shared type of the objects
      Parameters:
      a -
      b -
      maxDepth - max number of recursions
      Returns:
      0, 1 a>b, -1 ainvalid input: '<'b
    • compareMapsByKeys

      static int compareMapsByKeys(Map<String,Object> a, Map<String,Object> b, String... keys)
      Compare sequential fields in a Map with compare(Object, Object). The given fields are retrieved from the map and then compared. The first non-0 value defines the result. If no keys are given, all keys in both maps in sorted order are used instead.
      Parameters:
      a - the a value or null
      b - the b value or null
      keys - the array of keys to use
      Returns:
      0, 1 a>b, -1 ainvalid input: '<'b