--- old/src/share/classes/java/util/Comparator.java 2013-02-26 13:07:22.927275646 -0800 +++ new/src/share/classes/java/util/Comparator.java 2013-02-26 13:07:22.795275647 -0800 @@ -25,6 +25,11 @@ package java.util; +import java.util.function.Function; +import java.util.function.ToIntFunction; +import java.util.function.ToLongFunction; +import java.util.function.ToDoubleFunction; + /** * A comparison function, which imposes a total ordering on some * collection of objects. Comparators can be passed to a sort method (such @@ -165,4 +170,93 @@ * @see Object#hashCode() */ boolean equals(Object obj); + + /** + * Returns a comparator that imposes the reverse ordering of this + * comparator. + * + * @return A comparator that imposes the reverse ordering of this + * comparator. + * @since 1.8 + */ + default Comparator reverseOrder() { + return Collections.reverseOrder(this); + } + + /** + * Constructs a lexicographic order comparator with another comparator. + * For example, a {@code Comparator byLastName} can be composed + * with another {@code Comparator byFirstName}, then {@code + * byLastName.thenComparing(byFirstName)} creates a {@code + * Comparator} which sorts by last name, and for equal last names + * sorts by first name. + * + * @param other the other comparator used when equals on this. + * @throws NullPointerException if the argument is null. + * @since 1.8 + */ + default Comparator thenComparing(Comparator other) { + return Comparators.compose(this, other); + } + + /** + * Constructs a lexicographic order comparator with a function that + * extracts a {@code Comparable} key. This default implementation calls + * {@code thenComparing(this, Comparators.comparing(keyExtractor))}. + * + * @param the {@link Comparable} type for comparison + * @param keyExtractor the function used to extract the {@link Comparable} sort key + * @throws NullPointerException if the argument is null. + * @see Comparators#comparing(Function) + * @see #thenComparing(Comparator) + * @since 1.8 + */ + default > Comparator thenComparing(Function keyExtractor) { + return thenComparing(Comparators.comparing(keyExtractor)); + } + + /** + * Constructs a lexicographic order comparator with a function that + * extracts a {@code int} value. This default implementation calls {@code + * thenComparing(this, Comparators.comparing(keyExtractor))}. + * + * @param keyExtractor the function used to extract the integer value + * @throws NullPointerException if the argument is null. + * @see Comparators#comparing(ToIntFunction) + * @see #thenComparing(Comparator) + * @since 1.8 + */ + default Comparator thenComparing(ToIntFunction keyExtractor) { + return thenComparing(Comparators.comparing(keyExtractor)); + } + + /** + * Constructs a lexicographic order comparator with a function that + * extracts a {@code long} value. This default implementation calls + * {@code thenComparing(this, Comparators.comparing(keyExtractor))}. + * + * @param keyExtractor the function used to extract the long value + * @throws NullPointerException if the argument is null. + * @see Comparators#comparing(ToLongFunction) + * @see #thenComparing(Comparator) + * @since 1.8 + */ + default Comparator thenComparing(ToLongFunction keyExtractor) { + return thenComparing(Comparators.comparing(keyExtractor)); + } + + /** + * Constructs a lexicographic order comparator with a function that + * extracts a {@code double} value. This default implementation calls + * {@code thenComparing(this, Comparators.comparing(keyExtractor))}. + * + * @param keyExtractor the function used to extract the double value + * @throws NullPointerException if the argument is null. + * @see Comparators#comparing(ToDoubleFunction) + * @see #thenComparing(Comparator) + * @since 1.8 + */ + default Comparator thenComparing(ToDoubleFunction keyExtractor) { + return thenComparing(Comparators.comparing(keyExtractor)); + } }