Package com.helger.commons.compare
Class CompareHelper
- java.lang.Object
-
- com.helger.commons.compare.CompareHelper
-
@Immutable public final class CompareHelper extends Object
A small helper class that provides comparison methods that arenullsafe. Also provides secure ways to compare double and float values.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEFAULT_NULL_VALUES_COME_FIRSTBy defaultnullvalues come first
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intcompare(byte n1, byte n2)Special version for bytestatic intcompare(char n1, char n2)Special version for bytestatic intcompare(double d1, double d2)Special version for doublesstatic intcompare(float f1, float f2)Special version for floatsstatic intcompare(int n1, int n2)Special version for intstatic intcompare(long n1, long n2)Special version for longstatic intcompare(short n1, short n2)Special version for shortstatic <DATATYPE extends Comparable<? super DATATYPE>>
intcompare(DATATYPE aObj1, DATATYPE aObj2)Compare the passed items and handlenullvalues correctly.static <DATATYPE extends Comparable<? super DATATYPE>>
intcompare(DATATYPE aObj1, DATATYPE aObj2, boolean bNullValuesComeFirst)Compare the passed items and handlenullvalues correctly.static <DATATYPE> intcompare(DATATYPE aObj1, DATATYPE aObj2, Comparator<? super DATATYPE> aComp)Compare the passed items and handlenullvalues correctly.static <DATATYPE> intcompare(DATATYPE aObj1, DATATYPE aObj2, Comparator<? super DATATYPE> aComp, boolean bNullValuesComeFirst)Compare the passed items and handlenullvalues correctly.static intcompare(String sStr1, String sStr2, Collator aCollator)static intcompare(String sStr1, String sStr2, Collator aCollator, boolean bNullValuesComeFirst)static intcompare(String sStr1, String sStr2, Locale aSortLocale)static intcompare(String sStr1, String sStr2, Locale aSortLocale, boolean bNullValuesComeFirst)static intcompareFalseBeforeTrue(boolean b1, boolean b2)Special version for boolean,truebeforefalsestatic intcompareIgnoreCase(String sStr1, String sStr2)static intcompareIgnoreCase(String sStr1, String sStr2, boolean bNullValuesComeFirst)static intcompareTrueBeforeFalse(boolean b1, boolean b2)Special version for boolean,truebeforefalse
-
-
-
Field Detail
-
DEFAULT_NULL_VALUES_COME_FIRST
public static final boolean DEFAULT_NULL_VALUES_COME_FIRST
By defaultnullvalues come first- See Also:
- Constant Field Values
-
-
Method Detail
-
compareTrueBeforeFalse
public static int compareTrueBeforeFalse(boolean b1, boolean b2)Special version for boolean,truebeforefalse- Parameters:
b1- First valueb2- Second value- Returns:
- -1, 0 or + 1
-
compareFalseBeforeTrue
public static int compareFalseBeforeTrue(boolean b1, boolean b2)Special version for boolean,truebeforefalse- Parameters:
b1- First valueb2- Second value- Returns:
- -1, 0 or + 1
-
compare
public static int compare(byte n1, byte n2)Special version for byte- Parameters:
n1- First valuen2- Second value- Returns:
- value <0, 0 or >0.
-
compare
public static int compare(char n1, char n2)Special version for byte- Parameters:
n1- First valuen2- Second value- Returns:
- value <0, 0 or >0.
-
compare
public static int compare(double d1, double d2)Special version for doubles- Parameters:
d1- First valued2- Second value- Returns:
- value <0, 0 or >0.
-
compare
public static int compare(float f1, float f2)Special version for floats- Parameters:
f1- First valuef2- Second value- Returns:
- value <0, 0 or >0.
-
compare
public static int compare(int n1, int n2)Special version for int- Parameters:
n1- First valuen2- Second value- Returns:
- value <0, 0 or >0.
-
compare
public static int compare(long n1, long n2)Special version for long- Parameters:
n1- First valuen2- Second value- Returns:
- value <0, 0 or >0.
-
compare
public static int compare(short n1, short n2)Special version for short- Parameters:
n1- First valuen2- 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 handlenullvalues correctly. Anullvalue is always smaller than a non-nullvalue.- Type Parameters:
DATATYPE- Any comparable object to be used. Both need to be of the same type.- Parameters:
aObj1- First object to compare. May benull.aObj2- Second object to compare. May benull.- 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 handlenullvalues correctly. Anullvalue is always smaller than a non-nullvalue.- Type Parameters:
DATATYPE- Any comparable object to be used. Both need to be of the same type.- Parameters:
aObj1- First object to compare. May benull.aObj2- Second object to compare. May benull.bNullValuesComeFirst- iftruenullvalues are ordered before non-nullvalues- 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 handlenullvalues correctly. Anullvalue is always smaller than a non-nullvalue.- Type Parameters:
DATATYPE- Any object to be used. Both need to be of the same type.- Parameters:
aObj1- First object to compare. May benull.aObj2- Second object to compare. May benull.aComp- The comparator to be used if both parameters are notnull. The comparator itself may not benull.- 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 handlenullvalues correctly. Anullvalue is always smaller than a non-nullvalue.- Type Parameters:
DATATYPE- Any object to be used. Both need to be of the same type.- Parameters:
aObj1- First object to compare. May benull.aObj2- Second object to compare. May benull.aComp- The comparator to be used if both parameters are notnull. The comparator itself may not benull.bNullValuesComeFirst- iftruenullvalues are ordered before non-nullvalues- Returns:
- 0 if they are equal (or both
null), -1 or +1.
-
compare
public static int compare(@Nullable String sStr1, @Nullable String sStr2, @Nonnull Locale aSortLocale)
-
compare
public static int compare(@Nullable String sStr1, @Nullable String sStr2, @Nonnull Locale aSortLocale, boolean bNullValuesComeFirst)
-
compare
public static int compare(@Nullable String sStr1, @Nullable String sStr2, @Nonnull Collator aCollator)
-
compare
public static int compare(@Nullable String sStr1, @Nullable String sStr2, @Nonnull Collator aCollator, boolean bNullValuesComeFirst)
-
compareIgnoreCase
public static int compareIgnoreCase(@Nullable String sStr1, @Nullable String sStr2)
-
-