Interface Comparators
public interface Comparators
Some utilities to make comparisons easier to do, especially with null
handling
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intWhen you compare, the nulls can be quite annoying to handle. -
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic <T> intcompare(T a, T b) Compare two objects.static <T> intcompare(T a, T b, int maxDepth) Compare two objects.static <T> intcompare(T a, T b, Comparator<T> nonNullComparator) Protect a comparator from being called with null if they are both null, 0 if a and !b -> 1 if !a and b -> -1 Otherwise result of the nonNulComparator.compare(a,b)static <T> intcompare(T a, T b, IntSupplier nonNullComparator) Shield the nonNullComparator from null objects.static intCompare sequential fields in a Map withcompare(Object, Object).static <T> intcomparePresent(T a, T b) Compare two objects for null.static <T> intcomparePresentEquals(T a, T b) Compare two objects for null and equality.static booleanisFinal(int compare) Check if compare value is final, that it decides the comparison and can be used as return value in a compare operation.
-
Field Details
-
COMPARISON_REQUIRED
static final int COMPARISON_REQUIREDWhen you compare, the nulls can be quite annoying to handle. For this interface, we use a magic value to mark outside the normal 1,0,-1 values for compare to indicate that both values are not null and should be compared with traditional means.- See Also:
-
-
Method Details
-
compare
Protect a comparator from being called with null- if they are both null, 0
- if a and !b -> 1
- if !a and b -> -1
- Otherwise result of the nonNulComparator.compare(a,b)
- Type Parameters:
T- the shared type- Parameters:
a-b-nonNullComparator-- Returns:
- the result
-
compare
Shield the nonNullComparator from null objects. This is a convenience method so you do not have to invent new parameter names but can use the direct parameters from the invoked method.- if they are both null, 0
- if a and !b -> 1
- if !a and b -> -1
- Otherwise nonNullComparator.getAsInt()
- Type Parameters:
T- the shared type- Parameters:
a-b-- Returns:
- the result
-
comparePresent
static <T> int comparePresent(T a, T b) Compare two objects for null.- if they are both null, 0
- if a and !b -> 1
- if !a and b -> -1
- Otherwise Integer.MAXVALUE
To use this, compare the result with invalid input: '<'=1 and return if so.
- Type Parameters:
T- the shared type- Parameters:
a-b-- Returns:
- the result
-
comparePresentEquals
static <T> int comparePresentEquals(T a, T b) Compare two objects for null and equality.- if they are both null, 0
- if a and !b -> 1
- if !a and b -> -1
- if a.equals(b) -> 0
- Otherwise Integer.MAXVALUE
To use this, compare the result with invalid input: '<'=1 and return if so.
- Type Parameters:
T- the shared type- Parameters:
a-b-- Returns:
- the result
-
isFinal
static boolean isFinal(int compare) Check if compare value is final, that it decides the comparison and can be used as return value in a compare operation. -
compare
static <T> int compare(T a, T b) Compare two objects.- Test for null as in
comparePresent(Object, Object) - Test for same class, if not class names are compared
- Test if they are comparable, use the object comparison
- If array, compare the fields of the array with this method in order, if all match, the longer is higher.
- If Collection, compare the members of the Collection with this method in order, longer is higher
- returns 0 in a desperate attempt. This can make objects return 0 that are not equals.
- Type Parameters:
T- the shared type of the objects- Parameters:
a-b-- Returns:
- 0, 1 a>b, -1 ainvalid input: '<'b
- Test for null as in
-
compare
static <T> int compare(T a, T b, int maxDepth) Compare two objects.- Test for null as in
comparePresent(Object, Object) - Test for same class, if not class names are compared
- Test if they are comparable, use the object comparison
- If array, compare the fields of the array with this method in order, if all match, the longer is higher.
- If Collection, compare the members of the Collection with this method in order, longer is higher
- returns 0 in a desperate attempt. This can make objects return 0 that are not equals.
- Type Parameters:
T- the shared type of the objects- Parameters:
a-b-maxDepth- max number of recursions- Returns:
- 0, 1 a>b, -1 ainvalid input: '<'b
- Test for null as in
-
compareMapsByKeys
Compare sequential fields in a Map withcompare(Object, Object). The given fields are retrieved from the map and then compared. The first non-0 value defines the result. If no keys are given, all keys in both maps in sorted order are used instead.- Parameters:
a- the a value or nullb- the b value or nullkeys- the array of keys to use- Returns:
- 0, 1 a>b, -1 ainvalid input: '<'b
-