Class Comparators


  • public final class Comparators
    extends java.lang.Object
    • Field Detail

      • REVERSE_NATURAL_ORDER_COMPARATOR

        private static final SerializableComparator<?> REVERSE_NATURAL_ORDER_COMPARATOR
      • ASCENDING_COLLECTION_SIZE_COMPARATOR

        private static final SerializableComparator<java.util.Collection<?>> ASCENDING_COLLECTION_SIZE_COMPARATOR
      • DESCENDING_COLLECTION_SIZE_COMPARATOR

        private static final SerializableComparator<java.util.Collection<?>> DESCENDING_COLLECTION_SIZE_COMPARATOR
    • Constructor Detail

      • Comparators

        private Comparators()
    • Method Detail

      • comparableComparatorIfNull

        public static <T> java.util.Comparator<? super T> comparableComparatorIfNull​(java.util.Comparator<? super T> comparator)
        Since:
        10.0
      • naturalOrder

        public static <T> SerializableComparator<T> naturalOrder()
        Uses the natural compareTo methods of the objects which will throw if there are any nulls.
      • originalNaturalOrder

        @Deprecated
        public static <T> SerializableComparator<T> originalNaturalOrder()
        Deprecated.
        This method exists in order to guarantee serialization compatibility in tests.
        Since:
        11.1
      • reverseNaturalOrder

        public static <T> SerializableComparator<T> reverseNaturalOrder()
        Uses the natural compareTo methods of the objects which will throw if there are any nulls.
      • originalReverseNaturalOrder

        @Deprecated
        public static <T> SerializableComparator<T> originalReverseNaturalOrder()
        Deprecated.
        This method exists in order to guarantee serialization compatibility in tests.
        Since:
        11.1
      • reverse

        public static <T> SerializableComparator<T> reverse​(java.util.Comparator<T> comparator)
        Parameters:
        comparator - original comparator whose order will be reversed
        Returns:
        A comparator that reverses the order of any other Serializable Comparator.
      • safeNullsLow

        public static <T> SerializableComparator<T> safeNullsLow​(java.util.Comparator<T> notNullSafeComparator)
      • safeNullsHigh

        public static <T> SerializableComparator<T> safeNullsHigh​(java.util.Comparator<T> notNullSafeComparator)
      • chain

        @SafeVarargs
        public static <T> SerializableComparator<T> chain​(java.util.Comparator<T>... comparators)
      • fromFunctions

        public static <T,​V extends java.lang.Comparable<? super V>> SerializableComparator<T> fromFunctions​(Function<? super T,​? extends V> one)
      • fromFunctions

        public static <T,​V1 extends java.lang.Comparable<? super V1>,​V2 extends java.lang.Comparable<? super V2>> SerializableComparator<T> fromFunctions​(Function<? super T,​? extends V1> one,
                                                                                                                                                                      Function<? super T,​? extends V2> two)
      • fromFunctions

        public static <T,​V1 extends java.lang.Comparable<? super V1>,​V2 extends java.lang.Comparable<? super V2>,​V3 extends java.lang.Comparable<? super V3>> SerializableComparator<T> fromFunctions​(Function<? super T,​? extends V1> one,
                                                                                                                                                                                                                        Function<? super T,​? extends V2> two,
                                                                                                                                                                                                                        Function<? super T,​? extends V3> three)
      • ascendingCollectionSizeComparator

        public static SerializableComparator<java.util.Collection<?>> ascendingCollectionSizeComparator()
      • descendingCollectionSizeComparator

        public static SerializableComparator<java.util.Collection<?>> descendingCollectionSizeComparator()
      • byFirstOfPair

        public static <T> SerializableComparator<Pair<T,​?>> byFirstOfPair​(java.util.Comparator<? super T> comparator)
        Creates a comparator for pairs by using an existing comparator that only compares the first element of the pair
        Parameters:
        comparator - original comparator that compares the first element of the pair
        Returns:
        A comparator that compares pairs only by their first element
      • bySecondOfPair

        public static <T> SerializableComparator<Pair<?,​T>> bySecondOfPair​(java.util.Comparator<? super T> comparator)
        Creates a comparator for pairs by using an existing comparator that only compares the second element of the pair
        Parameters:
        comparator - original comparator that compares the second element of the pair
        Returns:
        A comparator that compares pairs only by their second element
      • byFunction

        public static <T,​V extends java.lang.Comparable<? super V>> SerializableComparator<T> byFunction​(Function<? super T,​? extends V> function)
      • originalByFunction

        @Deprecated
        public static <T,​V extends java.lang.Comparable<? super V>> SerializableComparator<T> originalByFunction​(Function<? super T,​? extends V> function)
        Deprecated.
        This signature is kept for serialization backwards compatibility.
        Since:
        11.1
      • getPrimitiveFunctionComparator

        private static <T,​V extends java.lang.Comparable<? super V>> SerializableComparator<T> getPrimitiveFunctionComparator​(Function<? super T,​? extends V> function)
      • byFunctionNullsLast

        public static <T,​V extends java.lang.Comparable<? super V>> SerializableComparator<T> byFunctionNullsLast​(Function<? super T,​? extends V> function)
      • byFunctionNullsFirst

        public static <T,​V extends java.lang.Comparable<? super V>> SerializableComparator<T> byFunctionNullsFirst​(Function<? super T,​? extends V> function)
      • byFunction

        public static <T,​V> SerializableComparator<T> byFunction​(Function<? super T,​? extends V> function,
                                                                       java.util.Comparator<V> comparator)
        Original signature accepted a Comparator which may not be Serializable. This signature is kept for backwards compatibility.
      • originalByFunction

        @Deprecated
        public static <T,​V> SerializableComparator<T> originalByFunction​(Function<? super T,​? extends V> function,
                                                                               java.util.Comparator<V> comparator)
        Deprecated.
        Original signature accepted a Comparator which may not be Serializable. This signature is kept for backwards compatibility.
      • nullSafeEquals

        public static boolean nullSafeEquals​(java.lang.Object value1,
                                             java.lang.Object value2)
      • nullSafeCompare

        public static <T extends java.lang.Comparable<T>> int nullSafeCompare​(T value1,
                                                                              T value2)