提交 07df0b0a 编写于 作者: H henryjen

8023528: Rename Comparator combinators to disambiguate overloading methods

Reviewed-by: mduigou, smarks
上级 ed3b16b9
...@@ -199,7 +199,7 @@ public interface Comparator<T> { ...@@ -199,7 +199,7 @@ public interface Comparator<T> {
* composed using following code, * composed using following code,
* *
* <pre>{@code * <pre>{@code
* Comparator<String> cmp = Comparator.comparing(String::length) * Comparator<String> cmp = Comparator.comparingInt(String::length)
* .thenComparing(String.CASE_INSENSITIVE_ORDER); * .thenComparing(String.CASE_INSENSITIVE_ORDER);
* }</pre> * }</pre>
* *
...@@ -270,18 +270,18 @@ public interface Comparator<T> { ...@@ -270,18 +270,18 @@ public interface Comparator<T> {
* extracts a {@code int} sort key. * extracts a {@code int} sort key.
* *
* @implSpec This default implementation behaves as if {@code * @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 * @param keyExtractor the function used to extract the integer sort key
* @return a lexicographic-order comparator composed of this and then the * @return a lexicographic-order comparator composed of this and then the
* {@code int} sort key * {@code int} sort key
* @throws NullPointerException if the argument is null. * @throws NullPointerException if the argument is null.
* @see #comparing(ToIntFunction) * @see #comparingInt(ToIntFunction)
* @see #thenComparing(Comparator) * @see #thenComparing(Comparator)
* @since 1.8 * @since 1.8
*/ */
default Comparator<T> thenComparing(ToIntFunction<? super T> keyExtractor) { default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) {
return thenComparing(comparing(keyExtractor)); return thenComparing(comparingInt(keyExtractor));
} }
/** /**
...@@ -289,18 +289,18 @@ public interface Comparator<T> { ...@@ -289,18 +289,18 @@ public interface Comparator<T> {
* extracts a {@code long} sort key. * extracts a {@code long} sort key.
* *
* @implSpec This default implementation behaves as if {@code * @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 * @param keyExtractor the function used to extract the long sort key
* @return a lexicographic-order comparator composed of this and then the * @return a lexicographic-order comparator composed of this and then the
* {@code long} sort key * {@code long} sort key
* @throws NullPointerException if the argument is null. * @throws NullPointerException if the argument is null.
* @see #comparing(ToLongFunction) * @see #comparingLong(ToLongFunction)
* @see #thenComparing(Comparator) * @see #thenComparing(Comparator)
* @since 1.8 * @since 1.8
*/ */
default Comparator<T> thenComparing(ToLongFunction<? super T> keyExtractor) { default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) {
return thenComparing(comparing(keyExtractor)); return thenComparing(comparingLong(keyExtractor));
} }
/** /**
...@@ -308,18 +308,18 @@ public interface Comparator<T> { ...@@ -308,18 +308,18 @@ public interface Comparator<T> {
* extracts a {@code double} sort key. * extracts a {@code double} sort key.
* *
* @implSpec This default implementation behaves as if {@code * @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 * @param keyExtractor the function used to extract the double sort key
* @return a lexicographic-order comparator composed of this and then the * @return a lexicographic-order comparator composed of this and then the
* {@code double} sort key * {@code double} sort key
* @throws NullPointerException if the argument is null. * @throws NullPointerException if the argument is null.
* @see #comparing(ToDoubleFunction) * @see #comparingDouble(ToDoubleFunction)
* @see #thenComparing(Comparator) * @see #thenComparing(Comparator)
* @since 1.8 * @since 1.8
*/ */
default Comparator<T> thenComparing(ToDoubleFunction<? super T> keyExtractor) { default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) {
return thenComparing(comparing(keyExtractor)); return thenComparing(comparingDouble(keyExtractor));
} }
/** /**
...@@ -484,7 +484,7 @@ public interface Comparator<T> { ...@@ -484,7 +484,7 @@ public interface Comparator<T> {
* @throws NullPointerException if the argument is null * @throws NullPointerException if the argument is null
* @since 1.8 * @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); Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable) return (Comparator<T> & Serializable)
(c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
...@@ -505,7 +505,7 @@ public interface Comparator<T> { ...@@ -505,7 +505,7 @@ public interface Comparator<T> {
* @throws NullPointerException if the argument is null * @throws NullPointerException if the argument is null
* @since 1.8 * @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); Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable) return (Comparator<T> & Serializable)
(c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
...@@ -526,7 +526,7 @@ public interface Comparator<T> { ...@@ -526,7 +526,7 @@ public interface Comparator<T> {
* @throws NullPointerException if the argument is null * @throws NullPointerException if the argument is null
* @since 1.8 * @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); Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable) return (Comparator<T> & Serializable)
(c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
......
...@@ -90,7 +90,7 @@ public class BasicTest { ...@@ -90,7 +90,7 @@ public class BasicTest {
Thing[] things = new Thing[intValues.length]; Thing[] things = new Thing[intValues.length];
for (int i=0; i<intValues.length; i++) for (int i=0; i<intValues.length; i++)
things[i] = new Thing(intValues[i], 0L, 0.0, null); 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 @Override
public int applyAsInt(Thing thing) { public int applyAsInt(Thing thing) {
return thing.getIntField(); return thing.getIntField();
...@@ -104,7 +104,7 @@ public class BasicTest { ...@@ -104,7 +104,7 @@ public class BasicTest {
Thing[] things = new Thing[longValues.length]; Thing[] things = new Thing[longValues.length];
for (int i=0; i<longValues.length; i++) for (int i=0; i<longValues.length; i++)
things[i] = new Thing(0, longValues[i], 0.0, null); 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 @Override
public long applyAsLong(Thing thing) { public long applyAsLong(Thing thing) {
return thing.getLongField(); return thing.getLongField();
...@@ -118,7 +118,7 @@ public class BasicTest { ...@@ -118,7 +118,7 @@ public class BasicTest {
Thing[] things = new Thing[doubleValues.length]; Thing[] things = new Thing[doubleValues.length];
for (int i=0; i<doubleValues.length; i++) for (int i=0; i<doubleValues.length; i++)
things[i] = new Thing(0, 0L, doubleValues[i], null); 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 @Override
public double applyAsDouble(Thing thing) { public double applyAsDouble(Thing thing) {
return thing.getDoubleField(); return thing.getDoubleField();
...@@ -211,8 +211,8 @@ public class BasicTest { ...@@ -211,8 +211,8 @@ public class BasicTest {
}; };
public void testComparatorDefaultMethods() { public void testComparatorDefaultMethods() {
Comparator<People> cmp = Comparator.comparing((Function<People, String>) People::getFirstName); Comparator<People> cmp = Comparator.comparing(People::getFirstName);
Comparator<People> cmp2 = Comparator.comparing((Function<People, String>) People::getLastName); Comparator<People> cmp2 = Comparator.comparing(People::getLastName);
// reverseOrder // reverseOrder
assertComparison(cmp.reversed(), people[1], people[0]); assertComparison(cmp.reversed(), people[1], people[0]);
// thenComparing(Comparator) // thenComparing(Comparator)
...@@ -222,20 +222,20 @@ public class BasicTest { ...@@ -222,20 +222,20 @@ public class BasicTest {
assertComparison(cmp.thenComparing(People::getLastName), people[0], people[1]); assertComparison(cmp.thenComparing(People::getLastName), people[0], people[1]);
assertComparison(cmp.thenComparing(People::getLastName), people[4], people[0]); assertComparison(cmp.thenComparing(People::getLastName), people[4], people[0]);
// thenComparing(ToIntFunction) // thenComparing(ToIntFunction)
assertComparison(cmp.thenComparing(People::getAge), people[0], people[1]); assertComparison(cmp.thenComparingInt(People::getAge), people[0], people[1]);
assertComparison(cmp.thenComparing(People::getAge), people[1], people[5]); assertComparison(cmp.thenComparingInt(People::getAge), people[1], people[5]);
// thenComparing(ToLongFunction) // thenComparing(ToLongFunction)
assertComparison(cmp.thenComparing(People::getAgeAsLong), people[0], people[1]); assertComparison(cmp.thenComparingLong(People::getAgeAsLong), people[0], people[1]);
assertComparison(cmp.thenComparing(People::getAgeAsLong), people[1], people[5]); assertComparison(cmp.thenComparingLong(People::getAgeAsLong), people[1], people[5]);
// thenComparing(ToDoubleFunction) // thenComparing(ToDoubleFunction)
assertComparison(cmp.thenComparing(People::getAgeAsDouble), people[0], people[1]); assertComparison(cmp.thenComparingDouble(People::getAgeAsDouble), people[0], people[1]);
assertComparison(cmp.thenComparing(People::getAgeAsDouble), people[1], people[5]); assertComparison(cmp.thenComparingDouble(People::getAgeAsDouble), people[1], people[5]);
} }
public void testNullsFirst() { public void testNullsFirst() {
Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder()); 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); .thenComparing(People::getFirstName, strcmp);
// Mary.null vs Mary.Cook - solve by last name // Mary.null vs Mary.Cook - solve by last name
assertComparison(cmp, people[6], people[5]); assertComparison(cmp, people[6], people[5]);
...@@ -243,7 +243,7 @@ public class BasicTest { ...@@ -243,7 +243,7 @@ public class BasicTest {
assertComparison(cmp, people[7], people[6]); assertComparison(cmp, people[7], people[6]);
// More than one thenComparing // 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)); .thenComparing(String.CASE_INSENSITIVE_ORDER));
assertComparison(strcmp, null, "abc"); assertComparison(strcmp, null, "abc");
assertComparison(strcmp, "ab", "abc"); assertComparison(strcmp, "ab", "abc");
...@@ -273,7 +273,7 @@ public class BasicTest { ...@@ -273,7 +273,7 @@ public class BasicTest {
public void testNullsLast() { public void testNullsLast() {
Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder()); 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); .thenComparing(People::getFirstName, strcmp);
// Mary.null vs Mary.Cook - solve by last name // Mary.null vs Mary.Cook - solve by last name
assertComparison(cmp, people[5], people[6]); assertComparison(cmp, people[5], people[6]);
...@@ -281,7 +281,7 @@ public class BasicTest { ...@@ -281,7 +281,7 @@ public class BasicTest {
assertComparison(cmp, people[7], people[6]); assertComparison(cmp, people[7], people[6]);
// More than one thenComparing // 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)); .thenComparing(String.CASE_INSENSITIVE_ORDER));
assertComparison(strcmp, "abc", null); assertComparison(strcmp, "abc", null);
assertComparison(strcmp, "ab", "abc"); assertComparison(strcmp, "ab", "abc");
...@@ -341,28 +341,28 @@ public class BasicTest { ...@@ -341,28 +341,28 @@ public class BasicTest {
} catch (NullPointerException npe) {} } catch (NullPointerException npe) {}
try { 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"); fail("comparing(null, cmp) should throw NPE");
} catch (NullPointerException npe) {} } catch (NullPointerException npe) {}
try { 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"); fail("comparing(f, null) should throw NPE");
} catch (NullPointerException npe) {} } catch (NullPointerException npe) {}
try { try {
Comparator<People> cmp = Comparator.comparing((Function<People, String>) null); Comparator<People> cmp = Comparator.comparing(null);
fail("comparing(null) should throw NPE"); fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {} } catch (NullPointerException npe) {}
try { try {
Comparator<People> cmp = Comparator.comparing((ToIntFunction<People>) null); Comparator<People> cmp = Comparator.comparingInt(null);
fail("comparing(null) should throw NPE"); fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {} } catch (NullPointerException npe) {}
try { try {
Comparator<People> cmp = Comparator.comparing((ToLongFunction<People>) null); Comparator<People> cmp = Comparator.comparingLong(null);
fail("comparing(null) should throw NPE"); fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {} } catch (NullPointerException npe) {}
try { try {
Comparator<People> cmp = Comparator.comparing((ToDoubleFunction<People>) null); Comparator<People> cmp = Comparator.comparingDouble(null);
fail("comparing(null) should throw NPE"); fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {} } catch (NullPointerException npe) {}
} }
......
...@@ -115,8 +115,8 @@ public class EntryComparators { ...@@ -115,8 +115,8 @@ public class EntryComparators {
// Comparator<People> cmp = Comparator.naturalOrder(); // Should fail to compiler as People is not comparable // 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. // We can use simple comparator, but those have been tested above.
// Thus choose to do compose for some level of interation. // Thus choose to do compose for some level of interation.
Comparator<People> cmp1 = Comparator.comparing((Function<People, String>) People::getFirstName); Comparator<People> cmp1 = Comparator.comparing(People::getFirstName);
Comparator<People> cmp2 = Comparator.comparing((Function<People, String>) People::getLastName); Comparator<People> cmp2 = Comparator.comparing(People::getLastName);
Comparator<People> cmp = cmp1.thenComparing(cmp2); Comparator<People> cmp = cmp1.thenComparing(cmp2);
assertPairComparison(people[0], people[0], people[1], people[1], assertPairComparison(people[0], people[0], people[1], people[1],
......
...@@ -67,26 +67,26 @@ public class BasicTest { ...@@ -67,26 +67,26 @@ public class BasicTest {
}; };
public void testMaxBy() { public void testMaxBy() {
Comparator<People> cmp = Comparator.comparing((Function<People, String>) People::getFirstName); Comparator<People> cmp = Comparator.comparing(People::getFirstName);
// lesser // lesser
assertSame(maxBy(cmp).apply(people[0], people[1]), people[1]); assertSame(maxBy(cmp).apply(people[0], people[1]), people[1]);
// euqal // euqal
cmp = Comparator.comparing((Function<People, String>) People::getLastName); cmp = Comparator.comparing(People::getLastName);
assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]); assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
// greater // greater
cmp = Comparator.comparing((ToIntFunction<People>) People::getAge); cmp = Comparator.comparingInt(People::getAge);
assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]); assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
} }
public void testLesserOf() { public void testMinBy() {
Comparator<People> cmp = Comparator.comparing((Function<People, String>) People::getFirstName); Comparator<People> cmp = Comparator.comparing(People::getFirstName);
// lesser // lesser
assertSame(minBy(cmp).apply(people[0], people[1]), people[0]); assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
// euqal // euqal
cmp = Comparator.comparing((Function<People, String>) People::getLastName); cmp = Comparator.comparing(People::getLastName);
assertSame(minBy(cmp).apply(people[0], people[1]), people[0]); assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
// greater // greater
cmp = Comparator.comparing((ToIntFunction<People>) People::getAge); cmp = Comparator.comparingInt(People::getAge);
assertSame(minBy(cmp).apply(people[0], people[1]), people[1]); assertSame(minBy(cmp).apply(people[0], people[1]), people[1]);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册