8023528: Rename Comparator combinators to disambiguate overloading methods
authorhenryjen
Wed, 21 Aug 2013 20:41:35 -0700
changeset 19605 0f98f8e3bdcb
parent 19604 f96e3aef2081
child 19606 6c846d61ba2f
8023528: Rename Comparator combinators to disambiguate overloading methods Reviewed-by: mduigou, smarks
jdk/src/share/classes/java/util/Comparator.java
jdk/test/java/util/Comparator/BasicTest.java
jdk/test/java/util/Map/EntryComparators.java
jdk/test/java/util/function/BinaryOperator/BasicTest.java
--- a/jdk/src/share/classes/java/util/Comparator.java	Tue Aug 27 12:54:44 2013 -0700
+++ b/jdk/src/share/classes/java/util/Comparator.java	Wed Aug 21 20:41:35 2013 -0700
@@ -199,7 +199,7 @@
      * composed using following code,
      *
      * <pre>{@code
-     *     Comparator<String> cmp = Comparator.comparing(String::length)
+     *     Comparator<String> cmp = Comparator.comparingInt(String::length)
      *             .thenComparing(String.CASE_INSENSITIVE_ORDER);
      * }</pre>
      *
@@ -270,18 +270,18 @@
      * extracts a {@code int} sort key.
      *
      * @implSpec This default implementation behaves as if {@code
-     *           thenComparing(comparing(keyExtractor))}.
+     *           thenComparing(comparingInt(keyExtractor))}.
      *
      * @param  keyExtractor the function used to extract the integer sort key
      * @return a lexicographic-order comparator composed of this and then the
      *         {@code int} sort key
      * @throws NullPointerException if the argument is null.
-     * @see #comparing(ToIntFunction)
+     * @see #comparingInt(ToIntFunction)
      * @see #thenComparing(Comparator)
      * @since 1.8
      */
-    default Comparator<T> thenComparing(ToIntFunction<? super T> keyExtractor) {
-        return thenComparing(comparing(keyExtractor));
+    default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) {
+        return thenComparing(comparingInt(keyExtractor));
     }
 
     /**
@@ -289,18 +289,18 @@
      * extracts a {@code long} sort key.
      *
      * @implSpec This default implementation behaves as if {@code
-     *           thenComparing(comparing(keyExtractor))}.
+     *           thenComparing(comparingLong(keyExtractor))}.
      *
      * @param  keyExtractor the function used to extract the long sort key
      * @return a lexicographic-order comparator composed of this and then the
      *         {@code long} sort key
      * @throws NullPointerException if the argument is null.
-     * @see #comparing(ToLongFunction)
+     * @see #comparingLong(ToLongFunction)
      * @see #thenComparing(Comparator)
      * @since 1.8
      */
-    default Comparator<T> thenComparing(ToLongFunction<? super T> keyExtractor) {
-        return thenComparing(comparing(keyExtractor));
+    default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) {
+        return thenComparing(comparingLong(keyExtractor));
     }
 
     /**
@@ -308,18 +308,18 @@
      * extracts a {@code double} sort key.
      *
      * @implSpec This default implementation behaves as if {@code
-     *           thenComparing(comparing(keyExtractor))}.
+     *           thenComparing(comparingDouble(keyExtractor))}.
      *
      * @param  keyExtractor the function used to extract the double sort key
      * @return a lexicographic-order comparator composed of this and then the
      *         {@code double} sort key
      * @throws NullPointerException if the argument is null.
-     * @see #comparing(ToDoubleFunction)
+     * @see #comparingDouble(ToDoubleFunction)
      * @see #thenComparing(Comparator)
      * @since 1.8
      */
-    default Comparator<T> thenComparing(ToDoubleFunction<? super T> keyExtractor) {
-        return thenComparing(comparing(keyExtractor));
+    default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) {
+        return thenComparing(comparingDouble(keyExtractor));
     }
 
     /**
@@ -484,7 +484,7 @@
      * @throws NullPointerException if the argument is null
      * @since 1.8
      */
-    public static <T> Comparator<T> comparing(ToIntFunction<? super T> keyExtractor) {
+    public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) {
         Objects.requireNonNull(keyExtractor);
         return (Comparator<T> & Serializable)
             (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
@@ -505,7 +505,7 @@
      * @throws NullPointerException if the argument is null
      * @since 1.8
      */
-    public static <T> Comparator<T> comparing(ToLongFunction<? super T> keyExtractor) {
+    public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) {
         Objects.requireNonNull(keyExtractor);
         return (Comparator<T> & Serializable)
             (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
@@ -526,7 +526,7 @@
      * @throws NullPointerException if the argument is null
      * @since 1.8
      */
-    public static<T> Comparator<T> comparing(ToDoubleFunction<? super T> keyExtractor) {
+    public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) {
         Objects.requireNonNull(keyExtractor);
         return (Comparator<T> & Serializable)
             (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
--- a/jdk/test/java/util/Comparator/BasicTest.java	Tue Aug 27 12:54:44 2013 -0700
+++ b/jdk/test/java/util/Comparator/BasicTest.java	Wed Aug 21 20:41:35 2013 -0700
@@ -90,7 +90,7 @@
         Thing[] things = new Thing[intValues.length];
         for (int i=0; i<intValues.length; i++)
             things[i] = new Thing(intValues[i], 0L, 0.0, null);
-        Comparator<Thing> comp = Comparator.comparing(new ToIntFunction<Thing>() {
+        Comparator<Thing> comp = Comparator.comparingInt(new ToIntFunction<Thing>() {
             @Override
             public int applyAsInt(Thing thing) {
                 return thing.getIntField();
@@ -104,7 +104,7 @@
         Thing[] things = new Thing[longValues.length];
         for (int i=0; i<longValues.length; i++)
             things[i] = new Thing(0, longValues[i], 0.0, null);
-        Comparator<Thing> comp = Comparator.comparing(new ToLongFunction<Thing>() {
+        Comparator<Thing> comp = Comparator.comparingLong(new ToLongFunction<Thing>() {
             @Override
             public long applyAsLong(Thing thing) {
                 return thing.getLongField();
@@ -118,7 +118,7 @@
         Thing[] things = new Thing[doubleValues.length];
         for (int i=0; i<doubleValues.length; i++)
             things[i] = new Thing(0, 0L, doubleValues[i], null);
-        Comparator<Thing> comp = Comparator.comparing(new ToDoubleFunction<Thing>() {
+        Comparator<Thing> comp = Comparator.comparingDouble(new ToDoubleFunction<Thing>() {
             @Override
             public double applyAsDouble(Thing thing) {
                 return thing.getDoubleField();
@@ -211,8 +211,8 @@
     };
 
     public void testComparatorDefaultMethods() {
-        Comparator<People> cmp = Comparator.comparing((Function<People, String>) People::getFirstName);
-        Comparator<People> cmp2 = Comparator.comparing((Function<People, String>) People::getLastName);
+        Comparator<People> cmp = Comparator.comparing(People::getFirstName);
+        Comparator<People> cmp2 = Comparator.comparing(People::getLastName);
         // reverseOrder
         assertComparison(cmp.reversed(), people[1], people[0]);
         // thenComparing(Comparator)
@@ -222,20 +222,20 @@
         assertComparison(cmp.thenComparing(People::getLastName), people[0], people[1]);
         assertComparison(cmp.thenComparing(People::getLastName), people[4], people[0]);
         // thenComparing(ToIntFunction)
-        assertComparison(cmp.thenComparing(People::getAge), people[0], people[1]);
-        assertComparison(cmp.thenComparing(People::getAge), people[1], people[5]);
+        assertComparison(cmp.thenComparingInt(People::getAge), people[0], people[1]);
+        assertComparison(cmp.thenComparingInt(People::getAge), people[1], people[5]);
         // thenComparing(ToLongFunction)
-        assertComparison(cmp.thenComparing(People::getAgeAsLong), people[0], people[1]);
-        assertComparison(cmp.thenComparing(People::getAgeAsLong), people[1], people[5]);
+        assertComparison(cmp.thenComparingLong(People::getAgeAsLong), people[0], people[1]);
+        assertComparison(cmp.thenComparingLong(People::getAgeAsLong), people[1], people[5]);
         // thenComparing(ToDoubleFunction)
-        assertComparison(cmp.thenComparing(People::getAgeAsDouble), people[0], people[1]);
-        assertComparison(cmp.thenComparing(People::getAgeAsDouble), people[1], people[5]);
+        assertComparison(cmp.thenComparingDouble(People::getAgeAsDouble), people[0], people[1]);
+        assertComparison(cmp.thenComparingDouble(People::getAgeAsDouble), people[1], people[5]);
     }
 
 
     public void testNullsFirst() {
         Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder());
-        Comparator<People> cmp = Comparator.<People, String>comparing(People::getLastName, strcmp)
+        Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                            .thenComparing(People::getFirstName, strcmp);
         // Mary.null vs Mary.Cook - solve by last name
         assertComparison(cmp, people[6], people[5]);
@@ -243,7 +243,7 @@
         assertComparison(cmp, people[7], people[6]);
 
         // More than one thenComparing
-        strcmp = Comparator.nullsFirst(Comparator.comparing((ToIntFunction<String>) String::length)
+        strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length)
                                                  .thenComparing(String.CASE_INSENSITIVE_ORDER));
         assertComparison(strcmp, null, "abc");
         assertComparison(strcmp, "ab", "abc");
@@ -273,7 +273,7 @@
 
     public void testNullsLast() {
         Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder());
-        Comparator<People> cmp = Comparator.<People, String>comparing(People::getLastName, strcmp)
+        Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                            .thenComparing(People::getFirstName, strcmp);
         // Mary.null vs Mary.Cook - solve by last name
         assertComparison(cmp, people[5], people[6]);
@@ -281,7 +281,7 @@
         assertComparison(cmp, people[7], people[6]);
 
         // More than one thenComparing
-        strcmp = Comparator.nullsLast(Comparator.comparing((ToIntFunction<String>) String::length)
+        strcmp = Comparator.nullsLast(Comparator.comparingInt(String::length)
                                                 .thenComparing(String.CASE_INSENSITIVE_ORDER));
         assertComparison(strcmp, "abc", null);
         assertComparison(strcmp, "ab", "abc");
@@ -341,28 +341,28 @@
         } catch (NullPointerException npe) {}
 
         try {
-            Comparator<People> cmp = Comparator.comparing((Function<People, String>) null, Comparator.<String>naturalOrder());
+            Comparator<People> cmp = Comparator.comparing(null, Comparator.<String>naturalOrder());
             fail("comparing(null, cmp) should throw NPE");
         } catch (NullPointerException npe) {}
         try {
-            Comparator<People> cmp = Comparator.comparing((Function<People, String>) People::getFirstName, null);
+            Comparator<People> cmp = Comparator.comparing(People::getFirstName, null);
             fail("comparing(f, null) should throw NPE");
         } catch (NullPointerException npe) {}
 
         try {
-            Comparator<People> cmp = Comparator.comparing((Function<People, String>) null);
+            Comparator<People> cmp = Comparator.comparing(null);
             fail("comparing(null) should throw NPE");
         } catch (NullPointerException npe) {}
         try {
-            Comparator<People> cmp = Comparator.comparing((ToIntFunction<People>) null);
+            Comparator<People> cmp = Comparator.comparingInt(null);
             fail("comparing(null) should throw NPE");
         } catch (NullPointerException npe) {}
         try {
-            Comparator<People> cmp = Comparator.comparing((ToLongFunction<People>) null);
+            Comparator<People> cmp = Comparator.comparingLong(null);
             fail("comparing(null) should throw NPE");
         } catch (NullPointerException npe) {}
         try {
-            Comparator<People> cmp = Comparator.comparing((ToDoubleFunction<People>) null);
+            Comparator<People> cmp = Comparator.comparingDouble(null);
             fail("comparing(null) should throw NPE");
         } catch (NullPointerException npe) {}
     }
--- a/jdk/test/java/util/Map/EntryComparators.java	Tue Aug 27 12:54:44 2013 -0700
+++ b/jdk/test/java/util/Map/EntryComparators.java	Wed Aug 21 20:41:35 2013 -0700
@@ -115,8 +115,8 @@
         // Comparator<People> cmp = Comparator.naturalOrder(); // Should fail to compiler as People is not comparable
         // We can use simple comparator, but those have been tested above.
         // Thus choose to do compose for some level of interation.
-        Comparator<People> cmp1 = Comparator.comparing((Function<People, String>) People::getFirstName);
-        Comparator<People> cmp2 = Comparator.comparing((Function<People, String>) People::getLastName);
+        Comparator<People> cmp1 = Comparator.comparing(People::getFirstName);
+        Comparator<People> cmp2 = Comparator.comparing(People::getLastName);
         Comparator<People> cmp = cmp1.thenComparing(cmp2);
 
         assertPairComparison(people[0], people[0], people[1], people[1],
--- a/jdk/test/java/util/function/BinaryOperator/BasicTest.java	Tue Aug 27 12:54:44 2013 -0700
+++ b/jdk/test/java/util/function/BinaryOperator/BasicTest.java	Wed Aug 21 20:41:35 2013 -0700
@@ -67,26 +67,26 @@
     };
 
     public void testMaxBy() {
-        Comparator<People> cmp = Comparator.comparing((Function<People, String>) People::getFirstName);
+        Comparator<People> cmp = Comparator.comparing(People::getFirstName);
         // lesser
         assertSame(maxBy(cmp).apply(people[0], people[1]), people[1]);
         // euqal
-        cmp = Comparator.comparing((Function<People, String>) People::getLastName);
+        cmp = Comparator.comparing(People::getLastName);
         assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
         // greater
-        cmp = Comparator.comparing((ToIntFunction<People>) People::getAge);
+        cmp = Comparator.comparingInt(People::getAge);
         assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
     }
 
-    public void testLesserOf() {
-        Comparator<People> cmp = Comparator.comparing((Function<People, String>) People::getFirstName);
+    public void testMinBy() {
+        Comparator<People> cmp = Comparator.comparing(People::getFirstName);
         // lesser
         assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
         // euqal
-        cmp = Comparator.comparing((Function<People, String>) People::getLastName);
+        cmp = Comparator.comparing(People::getLastName);
         assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
         // greater
-        cmp = Comparator.comparing((ToIntFunction<People>) People::getAge);
+        cmp = Comparator.comparingInt(People::getAge);
         assertSame(minBy(cmp).apply(people[0], people[1]), people[1]);
     }