Package com.helger.base.compare
Class CompareHelper
java.lang.Object
com.helger.base.compare.CompareHelper
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
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanBy defaultnullvalues come first -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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(@Nullable DATATYPE aObj1, @Nullable DATATYPE aObj2) Compare the passed items and handlenullvalues correctly.static <DATATYPE extends Comparable<? super DATATYPE>>
intcompare(@Nullable DATATYPE aObj1, @Nullable DATATYPE aObj2, boolean bNullValuesComeFirst) Compare the passed items and handlenullvalues correctly.static <DATATYPE> intcompare(@Nullable DATATYPE aObj1, @Nullable DATATYPE aObj2, @NonNull Comparator<? super DATATYPE> aComp) Compare the passed items and handlenullvalues correctly.static <DATATYPE> intcompare(@Nullable DATATYPE aObj1, @Nullable DATATYPE aObj2, @NonNull Comparator<? super DATATYPE> aComp, boolean bNullValuesComeFirst) Compare the passed items and handlenullvalues correctly.static intCompare the passed strings using the specifiedCollatorand handlenullvalues correctly.static intcompare(@Nullable String sStr1, @Nullable String sStr2, @NonNull Collator aCollator, boolean bNullValuesComeFirst) Compare the passed strings using the specifiedCollatorand handlenullvalues correctly.static intcompareFalseBeforeTrue(boolean b1, boolean b2) Special version for boolean,truebeforefalsestatic intcompareIgnoreCase(@Nullable String sStr1, @Nullable String sStr2) Compare the passed strings case insensitive and handlenullvalues correctly.static intcompareIgnoreCase(@Nullable String sStr1, @Nullable String sStr2, boolean bNullValuesComeFirst) Compare the passed strings case insensitive and handlenullvalues correctly.static intcompareTrueBeforeFalse(boolean b1, boolean b2) Special version for boolean,truebeforefalse
-
Field Details
-
DEFAULT_NULL_VALUES_COME_FIRST
public static final boolean DEFAULT_NULL_VALUES_COME_FIRSTBy defaultnullvalues come first- See Also:
-
-
Constructor Details
-
CompareHelper
protected CompareHelper()
-
-
Method Details
-
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 Collator aCollator) Compare the passed strings using the specifiedCollatorand handlenullvalues correctly. Anullvalue is always smaller than a non-nullvalue.- Parameters:
sStr1- First string to compare. May benull.sStr2- Second string to compare. May benull.aCollator- The collator to use for comparison. May not benull.- Returns:
- 0 if they are equal (or both
null), -1 or +1.
-
compare
public static int compare(@Nullable String sStr1, @Nullable String sStr2, @NonNull Collator aCollator, boolean bNullValuesComeFirst) Compare the passed strings using the specifiedCollatorand handlenullvalues correctly.- Parameters:
sStr1- First string to compare. May benull.sStr2- Second string to compare. May benull.aCollator- The collator to use for comparison. May not benull.bNullValuesComeFirst- iftruenullvalues are ordered before non-nullvalues- Returns:
- 0 if they are equal (or both
null), -1 or +1.
-
compareIgnoreCase
Compare the passed strings case insensitive and handlenullvalues correctly. Anullvalue is always smaller than a non-nullvalue.- Parameters:
sStr1- First string to compare. May benull.sStr2- Second string to compare. May benull.- Returns:
- 0 if they are equal (or both
null), -1 or +1.
-
compareIgnoreCase
public static int compareIgnoreCase(@Nullable String sStr1, @Nullable String sStr2, boolean bNullValuesComeFirst) Compare the passed strings case insensitive and handlenullvalues correctly.- Parameters:
sStr1- First string to compare. May benull.sStr2- Second string to compare. May benull.bNullValuesComeFirst- iftruenullvalues are ordered before non-nullvalues- Returns:
- 0 if they are equal (or both
null), -1 or +1.
-