jdk/src/share/classes/java/util/Comparator.java
changeset 16064 ef93558b0d63
parent 15647 314007859004
child 18571 8e3cb3c46ae8
--- a/jdk/src/share/classes/java/util/Comparator.java	Fri Mar 08 19:51:09 2013 +0000
+++ b/jdk/src/share/classes/java/util/Comparator.java	Fri Mar 08 15:45:06 2013 -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 <i>total ordering</i> 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<T> reverseOrder() {
+        return Collections.reverseOrder(this);
+    }
+
+    /**
+     * Constructs a lexicographic order comparator with another comparator.
+     * For example, a {@code Comparator<Person> byLastName} can be composed
+     * with another {@code Comparator<Person> byFirstName}, then {@code
+     * byLastName.thenComparing(byFirstName)} creates a {@code
+     * Comparator<Person>} 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<T> thenComparing(Comparator<? super T> 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 <U> 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 <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T, ? extends U> 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<T> thenComparing(ToIntFunction<? super T> 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<T> thenComparing(ToLongFunction<? super T> 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<T> thenComparing(ToDoubleFunction<? super T> keyExtractor) {
+        return thenComparing(Comparators.comparing(keyExtractor));
+    }
 }