Class CompareUtil


  • public abstract class CompareUtil
    extends Object
    Provides helper methods for object comparison.
    Author:
    Philipp Meinen
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected CompareUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> int compare​(Comparable<T> a, T b)
      A null-safe compare helper.
      static int diff​(boolean a, boolean b)
      Helper for compareTo implementations.
      Boolean version which considers false < true
      static int diff​(byte a, byte b)
      Helper for compareTo implementations.
      Byte version which masks the input values so that an unsigned comparison is performed.
      static int diff​(char a, char b)
      Helper for compareTo implementations.
      static int diff​(double a, double b)
      Helper for compareTo implementations.
      static int diff​(float a, float b)
      Helper for compareTo implementations.
      static int diff​(int a, int b)
      Helper for compareTo implementations.
      static int diff​(long a, long b)
      Helper for compareTo implementations.
      static int diff​(short a, short b)
      Helper for compareTo implementations.
      static boolean equals​(Object a, Object b)
      A null-safe equality helper.
      static int normalize​(int diff)
      Helper for compareTo implementations.
      static int normalize​(long diff)
      Helper for compareTo implementations.
    • Constructor Detail

      • CompareUtil

        protected CompareUtil()
    • Method Detail

      • equals

        public static boolean equals​(Object a,
                                     Object b)
        A null-safe equality helper.
         input                   return value
         ------------------------------------
         a == b               => true
         a == null, b != null => false
         a != null, b == null => false
         otherwise            => a.equals(b)
         
        Parameters:
        a - -
        b - -
        Returns:
        see above
      • compare

        public static <T> int compare​(Comparable<T> a,
                                      T b)
        A null-safe compare helper.
         input                   return value
         ------------------------------------
         a == b                =>  0
         a == null, b != null  => -1
         a != null, b == null  =>  1
         otherwise             =>  a.compareTo(b)
         
        Parameters:
        a - -
        b - -
        Returns:
        see above
      • normalize

        public static int normalize​(int diff)
        Helper for compareTo implementations. Normalized negative number to -1 and positive non-zero numbers to 1.
        Parameters:
        diff - the value which shall be normalized
        Returns:
        see above
      • normalize

        public static int normalize​(long diff)
        Helper for compareTo implementations. Normalized negative number to -1 and positive non-zero numbers to 1.
        Parameters:
        diff - the value which shall be normalized
        Returns:
        see above
      • diff

        public static int diff​(boolean a,
                               boolean b)
        Helper for compareTo implementations.
        Boolean version which considers false < true
         a     b
         -----------------------
         true  true    =>  0
         true  false   =>  1
         false false   =>  0
         false true    => -1
         
        Parameters:
        a - -
        b - -
        Returns:
        see above
      • diff

        public static int diff​(byte a,
                               byte b)
        Helper for compareTo implementations.
        Byte version which masks the input values so that an unsigned comparison is performed.
         Masks -128 -> 127 => 0 -> 255
         Maps { a<b, a==b, a>b } => { -1, 0, 1 }
         
        Parameters:
        a - -
        b - -
        Returns:
        see above
      • diff

        public static int diff​(char a,
                               char b)
        Helper for compareTo implementations.
         Maps { a<b, a==b, a>b } => { -1, 0, 1 }
         
        Parameters:
        a - -
        b - -
        Returns:
        see above
      • diff

        public static int diff​(short a,
                               short b)
        Helper for compareTo implementations.
         Maps { a<b, a==b, a>b } => { -1, 0, 1 }
         
        Parameters:
        a - -
        b - -
        Returns:
        see above
      • diff

        public static int diff​(int a,
                               int b)
        Helper for compareTo implementations.
         Maps { a<b, a==b, a>b } => { -1, 0, 1 }
         
        Parameters:
        a - -
        b - -
        Returns:
        see above
      • diff

        public static int diff​(long a,
                               long b)
        Helper for compareTo implementations.
         Maps { a<b, a==b, a>b } => { -1, 0, 1 }
         
        Parameters:
        a - -
        b - -
        Returns:
        see above
      • diff

        public static int diff​(float a,
                               float b)
        Helper for compareTo implementations.
         Maps { a<b, a==b, a>b } => { -1, 0, 1 }
         
        Parameters:
        a - -
        b - -
        Returns:
        see above
      • diff

        public static int diff​(double a,
                               double b)
        Helper for compareTo implementations.
         Maps { a<b, a==b, a>b } => { -1, 0, 1 }
         
        Parameters:
        a - -
        b - -
        Returns:
        see above