- java.lang.Object
-
- ch.bind.philib.lang.CompareUtil
-
public abstract class CompareUtil extends Object
Provides helper methods for object comparison.- Author:
- Philipp Meinen
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedCompareUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> intcompare(Comparable<T> a, T b)A null-safe compare helper.static intdiff(boolean a, boolean b)Helper for compareTo implementations.
Boolean version which considersfalse < truestatic intdiff(byte a, byte b)Helper for compareTo implementations.
Byte version which masks the input values so that an unsigned comparison is performed.static intdiff(char a, char b)Helper for compareTo implementations.static intdiff(double a, double b)Helper for compareTo implementations.static intdiff(float a, float b)Helper for compareTo implementations.static intdiff(int a, int b)Helper for compareTo implementations.static intdiff(long a, long b)Helper for compareTo implementations.static intdiff(short a, short b)Helper for compareTo implementations.static booleanequals(Object a, Object b)A null-safe equality helper.static intnormalize(int diff)Helper for compareTo implementations.static intnormalize(long diff)Helper for compareTo implementations.
-
-
-
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 considersfalse < truea 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
-
-