# HG changeset patch
# User henryjen
# Date 1377142895 25200
# Node ID 0f98f8e3bdcbe97fd9559b41eedc75895e25ca8a
# Parent f96e3aef2081b5a8036f83115867b255ba8e7fe5
8023528: Rename Comparator combinators to disambiguate overloading methods
Reviewed-by: mduigou, smarks
diff -r f96e3aef2081 -r 0f98f8e3bdcb jdk/src/share/classes/java/util/Comparator.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,
*
*
{@code
- * Comparator cmp = Comparator.comparing(String::length)
+ * Comparator cmp = Comparator.comparingInt(String::length)
* .thenComparing(String.CASE_INSENSITIVE_ORDER);
* }
*
@@ -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 thenComparing(ToIntFunction super T> keyExtractor) {
- return thenComparing(comparing(keyExtractor));
+ default Comparator 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 thenComparing(ToLongFunction super T> keyExtractor) {
- return thenComparing(comparing(keyExtractor));
+ default Comparator 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 thenComparing(ToDoubleFunction super T> keyExtractor) {
- return thenComparing(comparing(keyExtractor));
+ default Comparator 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 Comparator comparing(ToIntFunction super T> keyExtractor) {
+ public static Comparator comparingInt(ToIntFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator & 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 Comparator comparing(ToLongFunction super T> keyExtractor) {
+ public static Comparator comparingLong(ToLongFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator & 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 Comparator comparing(ToDoubleFunction super T> keyExtractor) {
+ public static Comparator comparingDouble(ToDoubleFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator & Serializable)
(c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
diff -r f96e3aef2081 -r 0f98f8e3bdcb jdk/test/java/util/Comparator/BasicTest.java
--- 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 comp = Comparator.comparing(new ToIntFunction() {
+ Comparator comp = Comparator.comparingInt(new ToIntFunction() {
@Override
public int applyAsInt(Thing thing) {
return thing.getIntField();
@@ -104,7 +104,7 @@
Thing[] things = new Thing[longValues.length];
for (int i=0; i comp = Comparator.comparing(new ToLongFunction() {
+ Comparator comp = Comparator.comparingLong(new ToLongFunction() {
@Override
public long applyAsLong(Thing thing) {
return thing.getLongField();
@@ -118,7 +118,7 @@
Thing[] things = new Thing[doubleValues.length];
for (int i=0; i comp = Comparator.comparing(new ToDoubleFunction() {
+ Comparator comp = Comparator.comparingDouble(new ToDoubleFunction() {
@Override
public double applyAsDouble(Thing thing) {
return thing.getDoubleField();
@@ -211,8 +211,8 @@
};
public void testComparatorDefaultMethods() {
- Comparator cmp = Comparator.comparing((Function) People::getFirstName);
- Comparator cmp2 = Comparator.comparing((Function) People::getLastName);
+ Comparator cmp = Comparator.comparing(People::getFirstName);
+ Comparator 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 strcmp = Comparator.nullsFirst(Comparator.naturalOrder());
- Comparator cmp = Comparator.comparing(People::getLastName, strcmp)
+ Comparator 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::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 strcmp = Comparator.nullsLast(Comparator.naturalOrder());
- Comparator cmp = Comparator.comparing(People::getLastName, strcmp)
+ Comparator 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::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 cmp = Comparator.comparing((Function) null, Comparator.naturalOrder());
+ Comparator cmp = Comparator.comparing(null, Comparator.naturalOrder());
fail("comparing(null, cmp) should throw NPE");
} catch (NullPointerException npe) {}
try {
- Comparator cmp = Comparator.comparing((Function) People::getFirstName, null);
+ Comparator cmp = Comparator.comparing(People::getFirstName, null);
fail("comparing(f, null) should throw NPE");
} catch (NullPointerException npe) {}
try {
- Comparator cmp = Comparator.comparing((Function) null);
+ Comparator cmp = Comparator.comparing(null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
try {
- Comparator cmp = Comparator.comparing((ToIntFunction) null);
+ Comparator cmp = Comparator.comparingInt(null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
try {
- Comparator cmp = Comparator.comparing((ToLongFunction) null);
+ Comparator cmp = Comparator.comparingLong(null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
try {
- Comparator cmp = Comparator.comparing((ToDoubleFunction) null);
+ Comparator cmp = Comparator.comparingDouble(null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
}
diff -r f96e3aef2081 -r 0f98f8e3bdcb jdk/test/java/util/Map/EntryComparators.java
--- 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 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 cmp1 = Comparator.comparing((Function) People::getFirstName);
- Comparator cmp2 = Comparator.comparing((Function) People::getLastName);
+ Comparator cmp1 = Comparator.comparing(People::getFirstName);
+ Comparator cmp2 = Comparator.comparing(People::getLastName);
Comparator cmp = cmp1.thenComparing(cmp2);
assertPairComparison(people[0], people[0], people[1], people[1],
diff -r f96e3aef2081 -r 0f98f8e3bdcb jdk/test/java/util/function/BinaryOperator/BasicTest.java
--- 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 cmp = Comparator.comparing((Function) People::getFirstName);
+ Comparator cmp = Comparator.comparing(People::getFirstName);
// lesser
assertSame(maxBy(cmp).apply(people[0], people[1]), people[1]);
// euqal
- cmp = Comparator.comparing((Function) People::getLastName);
+ cmp = Comparator.comparing(People::getLastName);
assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
// greater
- cmp = Comparator.comparing((ToIntFunction) People::getAge);
+ cmp = Comparator.comparingInt(People::getAge);
assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
}
- public void testLesserOf() {
- Comparator cmp = Comparator.comparing((Function) People::getFirstName);
+ public void testMinBy() {
+ Comparator cmp = Comparator.comparing(People::getFirstName);
// lesser
assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
// euqal
- cmp = Comparator.comparing((Function) People::getLastName);
+ cmp = Comparator.comparing(People::getLastName);
assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
// greater
- cmp = Comparator.comparing((ToIntFunction) People::getAge);
+ cmp = Comparator.comparingInt(People::getAge);
assertSame(minBy(cmp).apply(people[0], people[1]), people[1]);
}