Class CompareHelper


  • @Immutable
    public final class CompareHelper
    extends Object
    A small helper class that provides comparison methods that are null safe. Also provides secure ways to compare double and float values.
    Author:
    Philip Helger
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int compare​(byte n1, byte n2)
      Special version for byte
      static int compare​(char n1, char n2)
      Special version for byte
      static int compare​(double d1, double d2)
      Special version for doubles
      static int compare​(float f1, float f2)
      Special version for floats
      static int compare​(int n1, int n2)
      Special version for int
      static int compare​(long n1, long n2)
      Special version for long
      static int compare​(short n1, short n2)
      Special version for short
      static <DATATYPE extends Comparable<? super DATATYPE>>
      int
      compare​(DATATYPE aObj1, DATATYPE aObj2)
      Compare the passed items and handle null values correctly.
      static <DATATYPE extends Comparable<? super DATATYPE>>
      int
      compare​(DATATYPE aObj1, DATATYPE aObj2, boolean bNullValuesComeFirst)
      Compare the passed items and handle null values correctly.
      static <DATATYPE> int compare​(DATATYPE aObj1, DATATYPE aObj2, Comparator<? super DATATYPE> aComp)
      Compare the passed items and handle null values correctly.
      static <DATATYPE> int compare​(DATATYPE aObj1, DATATYPE aObj2, Comparator<? super DATATYPE> aComp, boolean bNullValuesComeFirst)
      Compare the passed items and handle null values correctly.
      static int compare​(String sStr1, String sStr2, Collator aCollator)  
      static int compare​(String sStr1, String sStr2, Collator aCollator, boolean bNullValuesComeFirst)  
      static int compare​(String sStr1, String sStr2, Locale aSortLocale)  
      static int compare​(String sStr1, String sStr2, Locale aSortLocale, boolean bNullValuesComeFirst)  
      static int compareFalseBeforeTrue​(boolean b1, boolean b2)
      Special version for boolean, true before false
      static int compareIgnoreCase​(String sStr1, String sStr2)  
      static int compareIgnoreCase​(String sStr1, String sStr2, boolean bNullValuesComeFirst)  
      static int compareTrueBeforeFalse​(boolean b1, boolean b2)
      Special version for boolean, true before false
    • Field Detail

      • DEFAULT_NULL_VALUES_COME_FIRST

        public static final boolean DEFAULT_NULL_VALUES_COME_FIRST
        By default null values come first
        See Also:
        Constant Field Values
    • Method Detail

      • compareTrueBeforeFalse

        public static int compareTrueBeforeFalse​(boolean b1,
                                                 boolean b2)
        Special version for boolean, true before false
        Parameters:
        b1 - First value
        b2 - Second value
        Returns:
        -1, 0 or + 1
      • compareFalseBeforeTrue

        public static int compareFalseBeforeTrue​(boolean b1,
                                                 boolean b2)
        Special version for boolean, true before false
        Parameters:
        b1 - First value
        b2 - Second value
        Returns:
        -1, 0 or + 1
      • compare

        public static int compare​(byte n1,
                                  byte n2)
        Special version for byte
        Parameters:
        n1 - First value
        n2 - Second value
        Returns:
        value <0, 0 or >0.
      • compare

        public static int compare​(char n1,
                                  char n2)
        Special version for byte
        Parameters:
        n1 - First value
        n2 - Second value
        Returns:
        value <0, 0 or >0.
      • compare

        public static int compare​(double d1,
                                  double d2)
        Special version for doubles
        Parameters:
        d1 - First value
        d2 - Second value
        Returns:
        value <0, 0 or >0.
      • compare

        public static int compare​(float f1,
                                  float f2)
        Special version for floats
        Parameters:
        f1 - First value
        f2 - Second value
        Returns:
        value <0, 0 or >0.
      • compare

        public static int compare​(int n1,
                                  int n2)
        Special version for int
        Parameters:
        n1 - First value
        n2 - Second value
        Returns:
        value <0, 0 or >0.
      • compare

        public static int compare​(long n1,
                                  long n2)
        Special version for long
        Parameters:
        n1 - First value
        n2 - Second value
        Returns:
        value <0, 0 or >0.
      • compare

        public static int compare​(short n1,
                                  short n2)
        Special version for short
        Parameters:
        n1 - First value
        n2 - Second value
        Returns:
        value <0, 0 or >0.
      • compare

        public static <DATATYPE extends Comparable<? super DATATYPE>> int compare​(@Nullable
                                                                                  DATATYPE aObj1,
                                                                                  @Nullable
                                                                                  DATATYPE aObj2)
        Compare the passed items and handle null values correctly. A null value is always smaller than a non-null value.
        Type Parameters:
        DATATYPE - Any comparable object to be used. Both need to be of the same type.
        Parameters:
        aObj1 - First object to compare. May be null.
        aObj2 - Second object to compare. May be null.
        Returns:
        0 if they are equal (or both null), -1 or +1.
      • compare

        public static <DATATYPE extends Comparable<? super DATATYPE>> int compare​(@Nullable
                                                                                  DATATYPE aObj1,
                                                                                  @Nullable
                                                                                  DATATYPE aObj2,
                                                                                  boolean bNullValuesComeFirst)
        Compare the passed items and handle null values correctly. A null value is always smaller than a non-null value.
        Type Parameters:
        DATATYPE - Any comparable object to be used. Both need to be of the same type.
        Parameters:
        aObj1 - First object to compare. May be null.
        aObj2 - Second object to compare. May be null.
        bNullValuesComeFirst - if true null values are ordered before non-null values
        Returns:
        0 if they are equal (or both null), -1 or +1.
      • compare

        public static <DATATYPE> int compare​(@Nullable
                                             DATATYPE aObj1,
                                             @Nullable
                                             DATATYPE aObj2,
                                             @Nonnull
                                             Comparator<? super DATATYPE> aComp)
        Compare the passed items and handle null values correctly. A null value is always smaller than a non-null value.
        Type Parameters:
        DATATYPE - Any object to be used. Both need to be of the same type.
        Parameters:
        aObj1 - First object to compare. May be null.
        aObj2 - Second object to compare. May be null.
        aComp - The comparator to be used if both parameters are not null. The comparator itself may not be null.
        Returns:
        0 if they are equal (or both null), -1 or +1.
      • compare

        public static <DATATYPE> int compare​(@Nullable
                                             DATATYPE aObj1,
                                             @Nullable
                                             DATATYPE aObj2,
                                             @Nonnull
                                             Comparator<? super DATATYPE> aComp,
                                             boolean bNullValuesComeFirst)
        Compare the passed items and handle null values correctly. A null value is always smaller than a non-null value.
        Type Parameters:
        DATATYPE - Any object to be used. Both need to be of the same type.
        Parameters:
        aObj1 - First object to compare. May be null.
        aObj2 - Second object to compare. May be null.
        aComp - The comparator to be used if both parameters are not null. The comparator itself may not be null.
        bNullValuesComeFirst - if true null values are ordered before non-null values
        Returns:
        0 if they are equal (or both null), -1 or +1.