Class CmpUtil


  • public final class CmpUtil
    extends Object
    Various methods for dealing with the comparison of objects.
    • Method Detail

      • canonicalCompare

        public static <U> int canonicalCompare​(List<? extends U> o1,
                                               List<? extends U> o2,
                                               Comparator<? super U> elemComparator)
        Compares two Lists with respect to canonical ordering.

        In canonical ordering, a sequence o1 is less than a sequence o2 if o1 is shorter than o2, or if they have the same length and o1 is lexicographically smaller than o2.

        Parameters:
        o1 - the first list
        o2 - the second list
        elemComparator - the comparator for comparing the single elements
        Returns:
        the result of the comparison
      • canonicalCompare

        public static int canonicalCompare​(int[] a1,
                                           int[] a2)
      • canonicalCompare

        public static <U extends Comparable<? super U>> int canonicalCompare​(List<? extends U> o1,
                                                                             List<? extends U> o2)
        Compares two Lists of Comparable elements with respect to canonical ordering.

        In canonical ordering, a sequence o1 is less than a sequence o2 if o1 is shorter than o2, or if they have the same length and o1 is lexicographically smaller than o2.

        Parameters:
        o1 - the first list
        o2 - the second list
        Returns:
        the result of the comparison
      • lexCompare

        public static <U> int lexCompare​(Iterable<? extends U> o1,
                                         Iterable<? extends U> o2,
                                         Comparator<? super U> elemComparator)
        Lexicographically compares two Iterables. Comparison of the elements is done using the specified comparator.
        Parameters:
        o1 - the first iterable.
        o2 - the second iterable.
        elemComparator - the comparator.
        Returns:
        < 0 iff o1 is lexicographically smaller, 0 if o1 equals o2 and > 0 otherwise.
      • lexCompare

        public static int lexCompare​(int[] a1,
                                     int[] a2)
      • lexCompare

        public static <U extends Comparable<? super U>> int lexCompare​(Iterable<? extends U> o1,
                                                                       Iterable<? extends U> o2)
        Lexicographically compares two Iterables, whose element types are comparable.
      • lexComparator

        public static <T extends Iterable<U>,​U> Comparator<T> lexComparator​(Comparator<U> elemComp)
        Retrieves a lexicographical comparator for the given type.
        Parameters:
        elemComp - the comparator to use for comparing the elements.
        Returns:
        a comparator for comparing objects of type T based on lexicographical ordering.
      • lexComparator

        public static <U extends Comparable<U>,​T extends Iterable<U>> Comparator<T> lexComparator()
        Retrieves a lexicographical comparator for the given type, which has to be an Iterable of Comparable types.
        Returns:
        the lexicographical comparator.
      • canonicalComparator

        public static <T extends List<? extends U>,​U> Comparator<T> canonicalComparator​(Comparator<? super U> elemComp)
        Retrieves a canonical comparator for the given list type.
        Parameters:
        elemComp - the comparator to use for comparing the elements.
        Returns:
        a comparator for comparing objects of type T based on canonical ordering.