test/java/util/Comparator/BasicTest.java
Print this page
rev 7914 : 8023528: Rename Comparator combinators to disambiguate overloading methods
Reviewed-by:
*** 88,98 ****
public void testIntComparator() {
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>() {
@Override
public int applyAsInt(Thing thing) {
return thing.getIntField();
}
});
--- 88,98 ----
public void testIntComparator() {
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.comparingInt(new ToIntFunction<Thing>() {
@Override
public int applyAsInt(Thing thing) {
return thing.getIntField();
}
});
*** 102,112 ****
public void testLongComparator() {
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>() {
@Override
public long applyAsLong(Thing thing) {
return thing.getLongField();
}
});
--- 102,112 ----
public void testLongComparator() {
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.comparingLong(new ToLongFunction<Thing>() {
@Override
public long applyAsLong(Thing thing) {
return thing.getLongField();
}
});
*** 116,126 ****
public void testDoubleComparator() {
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>() {
@Override
public double applyAsDouble(Thing thing) {
return thing.getDoubleField();
}
});
--- 116,126 ----
public void testDoubleComparator() {
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.comparingDouble(new ToDoubleFunction<Thing>() {
@Override
public double applyAsDouble(Thing thing) {
return thing.getDoubleField();
}
});
*** 209,251 ****
new People("Mary", null, 25),
new People("John", null, 27)
};
public void testComparatorDefaultMethods() {
! Comparator<People> cmp = Comparator.comparing((Function<People, String>) People::getFirstName);
! Comparator<People> cmp2 = Comparator.comparing((Function<People, String>) People::getLastName);
// reverseOrder
assertComparison(cmp.reversed(), people[1], people[0]);
// thenComparing(Comparator)
assertComparison(cmp.thenComparing(cmp2), people[0], people[1]);
assertComparison(cmp.thenComparing(cmp2), people[4], people[0]);
// thenComparing(Function)
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]);
// thenComparing(ToLongFunction)
! assertComparison(cmp.thenComparing(People::getAgeAsLong), people[0], people[1]);
! assertComparison(cmp.thenComparing(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]);
}
public void testNullsFirst() {
Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder());
! Comparator<People> cmp = Comparator.<People, String>comparing(People::getLastName, strcmp)
.thenComparing(People::getFirstName, strcmp);
// Mary.null vs Mary.Cook - solve by last name
assertComparison(cmp, people[6], people[5]);
// John.null vs Mary.null - solve by first name
assertComparison(cmp, people[7], people[6]);
// More than one thenComparing
! strcmp = Comparator.nullsFirst(Comparator.comparing((ToIntFunction<String>) String::length)
.thenComparing(String.CASE_INSENSITIVE_ORDER));
assertComparison(strcmp, null, "abc");
assertComparison(strcmp, "ab", "abc");
assertComparison(strcmp, "abc", "def");
assertEquals(0, strcmp.compare("abc", "ABC"));
--- 209,251 ----
new People("Mary", null, 25),
new People("John", null, 27)
};
public void testComparatorDefaultMethods() {
! Comparator<People> cmp = Comparator.comparing(People::getFirstName);
! Comparator<People> cmp2 = Comparator.comparing(People::getLastName);
// reverseOrder
assertComparison(cmp.reversed(), people[1], people[0]);
// thenComparing(Comparator)
assertComparison(cmp.thenComparing(cmp2), people[0], people[1]);
assertComparison(cmp.thenComparing(cmp2), people[4], people[0]);
// thenComparing(Function)
assertComparison(cmp.thenComparing(People::getLastName), people[0], people[1]);
assertComparison(cmp.thenComparing(People::getLastName), people[4], people[0]);
// thenComparing(ToIntFunction)
! assertComparison(cmp.thenComparingInt(People::getAge), people[0], people[1]);
! assertComparison(cmp.thenComparingInt(People::getAge), people[1], people[5]);
// thenComparing(ToLongFunction)
! assertComparison(cmp.thenComparingLong(People::getAgeAsLong), people[0], people[1]);
! assertComparison(cmp.thenComparingLong(People::getAgeAsLong), people[1], people[5]);
// thenComparing(ToDoubleFunction)
! 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.comparing(People::getLastName, strcmp)
.thenComparing(People::getFirstName, strcmp);
// Mary.null vs Mary.Cook - solve by last name
assertComparison(cmp, people[6], people[5]);
// John.null vs Mary.null - solve by first name
assertComparison(cmp, people[7], people[6]);
// More than one thenComparing
! strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length)
.thenComparing(String.CASE_INSENSITIVE_ORDER));
assertComparison(strcmp, null, "abc");
assertComparison(strcmp, "ab", "abc");
assertComparison(strcmp, "abc", "def");
assertEquals(0, strcmp.compare("abc", "ABC"));
*** 271,289 ****
assertComparison(strcmp, "abc", "def");
}
public void testNullsLast() {
Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder());
! Comparator<People> cmp = Comparator.<People, String>comparing(People::getLastName, strcmp)
.thenComparing(People::getFirstName, strcmp);
// Mary.null vs Mary.Cook - solve by last name
assertComparison(cmp, people[5], people[6]);
// John.null vs Mary.null - solve by first name
assertComparison(cmp, people[7], people[6]);
// More than one thenComparing
! strcmp = Comparator.nullsLast(Comparator.comparing((ToIntFunction<String>) String::length)
.thenComparing(String.CASE_INSENSITIVE_ORDER));
assertComparison(strcmp, "abc", null);
assertComparison(strcmp, "ab", "abc");
assertComparison(strcmp, "abc", "def");
--- 271,289 ----
assertComparison(strcmp, "abc", "def");
}
public void testNullsLast() {
Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder());
! 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]);
// John.null vs Mary.null - solve by first name
assertComparison(cmp, people[7], people[6]);
// More than one thenComparing
! strcmp = Comparator.nullsLast(Comparator.comparingInt(String::length)
.thenComparing(String.CASE_INSENSITIVE_ORDER));
assertComparison(strcmp, "abc", null);
assertComparison(strcmp, "ab", "abc");
assertComparison(strcmp, "abc", "def");
*** 339,369 ****
Comparator.<String>reverseOrder().compare((String) null, "abc");
fail("expected NPE with naturalOrder");
} catch (NullPointerException npe) {}
try {
! Comparator<People> cmp = Comparator.comparing((Function<People, String>) 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);
fail("comparing(f, null) should throw NPE");
} catch (NullPointerException npe) {}
try {
! Comparator<People> cmp = Comparator.comparing((Function<People, String>) null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
try {
! Comparator<People> cmp = Comparator.comparing((ToIntFunction<People>) null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
try {
! Comparator<People> cmp = Comparator.comparing((ToLongFunction<People>) null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
try {
! Comparator<People> cmp = Comparator.comparing((ToDoubleFunction<People>) null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
}
}
--- 339,369 ----
Comparator.<String>reverseOrder().compare((String) null, "abc");
fail("expected NPE with naturalOrder");
} catch (NullPointerException npe) {}
try {
! 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(People::getFirstName, null);
fail("comparing(f, null) should throw NPE");
} catch (NullPointerException npe) {}
try {
! Comparator<People> cmp = Comparator.comparing(null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
try {
! Comparator<People> cmp = Comparator.comparingInt(null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
try {
! Comparator<People> cmp = Comparator.comparingLong(null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
try {
! Comparator<People> cmp = Comparator.comparingDouble(null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
}
}