Class Compare


  • public class Compare
    extends java.lang.Object
    Some handy static methods to compare two Comparables even if one or both are null-pointers.
    Author:
    harald
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends java.lang.Comparable<? super T>>
      int
      compare​(T o1, T o2)
      Compares two objects.
      null is being treated as smallest value.
      static <T extends java.lang.Comparable<? super T>>
      int
      compareWithNullLargest​(T o1, T o2)
      Compares two objects.
      null is being treated as largest value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • compare

        public static <T extends java.lang.Comparable<? super T>> int compare​(T o1,
                                                                              T o2)
        Compares two objects.
        null is being treated as smallest value.

        Notice the different semantics compared to Objects.compare(java.lang.Object, java.lang.Object, java.util.Comparator) for null-values.

        Type Parameters:
        T - the class type
        Parameters:
        o1 - the first object
        o2 - the second object
        Returns:
        a negative integer, zero, or a positive integer as o1 is less than, equal to, or greater than o2.
      • compareWithNullLargest

        public static <T extends java.lang.Comparable<? super T>> int compareWithNullLargest​(T o1,
                                                                                             T o2)
        Compares two objects.
        null is being treated as largest value.
        Type Parameters:
        T - the class type
        Parameters:
        o1 - the first object
        o2 - the second object
        Returns:
        a negative integer, zero, or a positive integer as o1 is less than, equal to, or greater than o2.