public final class Comparators extends Object
Comparator-related utility methods.| Modifier and Type | Method and Description |
|---|---|
static <T extends Comparable<T>> |
compareNullSafe(T c1,
T c2) |
static <T extends Comparable<T>> |
naturalOrderComparator() |
static <T> Comparator<T> |
nullSafeComparator(Comparator<T> delegate)
Wraps the delegate comparator such that the cases "o1
== null" and/or
"o2 == null" are detected and handled as follows:
o1 == null && o2 == null
0 (as if o1.compareTo(o2) == 0)
{@code o1 == null && o2 ! |
public static <T extends Comparable<T>> Comparator<T> naturalOrderComparator()
Comparator that compares subjects by their Comparable.compareTo(Object) method (and
is thus not null-safe)public static <T extends Comparable<T>> int compareNullSafe(@Nullable T c1, @Nullable T c2)
0 if both arguments are null,
or -1 if only o1 is null,
or 1 if only o2 is null,
and otherwise o1.compareTo(o2)public static <T> Comparator<T> nullSafeComparator(Comparator<T> delegate)
== null" and/or
"o2 == null" are detected and handled as follows:
o1 == null && o2 == null0 (as if o1.compareTo(o2) == 0)o1 == null && o2 != null-1 (as if o1.compareTo(o2) < 0)o1 != null && o2 == null1 (as if o1.compareTo(o2) > 0)o1 != null && o2 != null.compare(o1, o2)
The delegate is only invoked iff o1 != null && o2 != null.
Copyright © 2018 Arno Unkrig. All rights reserved.