changeset 8241:bb563e90c4fb

Corret merge problem
author henryjen
date Wed, 17 Apr 2013 08:52:31 -0700
parents bd37c6d3f3d0
children 49c020792613
files test/java/util/Comparators/BasicTest.java
diffstat 1 files changed, 23 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/util/Comparators/BasicTest.java	Wed Apr 17 11:39:48 2013 +0200
+++ b/test/java/util/Comparators/BasicTest.java	Wed Apr 17 08:52:31 2013 -0700
@@ -97,7 +97,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 = Comparators.comparing(new ToIntFunction<BasicTest.Thing>() {
+        Comparator<Thing> comp = Comparators.comparing(new ToIntFunction<Thing>() {
             @Override
             public int applyAsInt(Thing thing) {
                 return thing.getIntField();
@@ -111,7 +111,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 = Comparators.comparing(new ToLongFunction<BasicTest.Thing>() {
+        Comparator<Thing> comp = Comparators.comparing(new ToLongFunction<Thing>() {
             @Override
             public long applyAsLong(Thing thing) {
                 return thing.getLongField();
@@ -125,7 +125,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 = Comparators.comparing(new ToDoubleFunction<BasicTest.Thing>() {
+        Comparator<Thing> comp = Comparators.comparing(new ToDoubleFunction<Thing>() {
             @Override
             public double applyAsDouble(Thing thing) {
                 return thing.getDoubleField();
@@ -181,24 +181,6 @@
         assertTrue(cmpr.compare("abc", "defg") > 0);
     }
 
-    @Test(expectedExceptions=NullPointerException.class)
-    public void testReverseComparatorNPE() {
-        Comparator<String> cmp = Comparators.reverseOrder(null);
-    }
-
-    public void testComposeComparator() {
-        // Longer string in front
-        Comparator<String> first = (s1, s2) -> s2.length() - s1.length();
-        Comparator<String> second = Comparators.naturalOrder();
-        Comparator<String> composed = Comparators.compose(first, second);
-
-        assertTrue(composed.compare("abcdefg", "abcdef") < 0);
-        assertTrue(composed.compare("abcdef", "abcdefg") > 0);
-        assertTrue(composed.compare("abcdef", "abcdef") == 0);
-        assertTrue(composed.compare("abcdef", "ghijkl") < 0);
-        assertTrue(composed.compare("ghijkl", "abcdefg") > 0);
-    }
-
     private <K, V> void assertPairComparison(K k1, V v1, K k2, V v2,
                                         Comparator<Map.Entry<K, V>> ck,
                                         Comparator<Map.Entry<K, V>> cv) {
@@ -223,7 +205,7 @@
         assertTrue(cv.compare(p21, p11) == 0);
         assertTrue(cv.compare(p21, p12) < 0);
 
-        Comparator<Map.Entry<K, V>> cmp = Comparators.compose(ck, cv);
+        Comparator<Map.Entry<K, V>> cmp = ck.thenComparing(cv);
         assertTrue(cmp.compare(p11, p11) == 0);
         assertTrue(cmp.compare(p12, p11) > 0);
         assertTrue(cmp.compare(p11, p12) < 0);
@@ -232,7 +214,7 @@
         assertTrue(cmp.compare(p21, p11) > 0);
         assertTrue(cmp.compare(p21, p12) > 0);
 
-        cmp = Comparators.compose(cv, ck);
+        cmp = cv.thenComparing(ck);
         assertTrue(cmp.compare(p11, p11) == 0);
         assertTrue(cmp.compare(p12, p11) > 0);
         assertTrue(cmp.compare(p11, p12) < 0);
@@ -281,7 +263,7 @@
         // Thus choose to do compose for some level of interation.
         Comparator<People> cmp1 = Comparators.comparing((Function<People, String>) People::getFirstName);
         Comparator<People> cmp2 = Comparators.comparing((Function<People, String>) People::getLastName);
-        Comparator<People> cmp = Comparators.compose(cmp1, cmp2);
+        Comparator<People> cmp = cmp1.thenComparing(cmp2);
 
         assertPairComparison(people[0], people[0], people[1], people[1],
                          Comparators.<People, People>byKey(cmp),
@@ -304,17 +286,17 @@
         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]);
+        assertComparison(cmp.<People, String>thenComparing(People::getLastName), people[0], people[1]);
+        assertComparison(cmp.<People, String>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.thenComparing((ToIntFunction<People>) People::getAge), people[0], people[1]);
+        assertComparison(cmp.thenComparing((ToIntFunction<People>) 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.thenComparing((ToLongFunction<People>) People::getAgeAsLong), people[0], people[1]);
+        assertComparison(cmp.thenComparing((ToLongFunction<People>) 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.thenComparing((ToDoubleFunction<People>) People::getAgeAsDouble), people[0], people[1]);
+        assertComparison(cmp.thenComparing((ToDoubleFunction<People>) People::getAgeAsDouble), people[1], people[5]);
     }
 
     public void testGreaterOf() {
@@ -383,6 +365,15 @@
         } catch (NullPointerException npe) {}
 
         try {
+            Comparator<People> cmp = Comparators.comparing((Function<People, String>) null, Comparators.<String>naturalOrder());
+            fail("comparing(null, cmp) should throw NPE");
+        } catch (NullPointerException npe) {}
+        try {
+            Comparator<People> cmp = Comparators.comparing((Function<People, String>) People::getFirstName, null);
+            fail("comparing(f, null) should throw NPE");
+        } catch (NullPointerException npe) {}
+
+        try {
             Comparator<People> cmp = Comparators.comparing((Function<People, String>) null);
             fail("comparing(null) should throw NPE");
         } catch (NullPointerException npe) {}