OpenJDK / lambda / lambda / jdk
changeset 7623:9ba86b004c5a
Fix and test for 8009736: Comparator to allow narrowing type on combinator APIs
author | henryjen |
---|---|
date | Tue, 12 Mar 2013 20:01:36 -0700 |
parents | 85912d2b4773 |
children | b383ff65a209 |
files | src/share/classes/java/util/Comparator.java src/share/classes/java/util/Comparators.java test/java/util/Comparator/BasicTest.java test/java/util/Comparator/TypeTest.java test/java/util/Comparators/BasicTest.java test/java/util/ComparatorsTest.java |
diffstat | 6 files changed, 496 insertions(+), 357 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/java/util/Comparator.java Tue Mar 12 22:38:54 2013 -0400 +++ b/src/share/classes/java/util/Comparator.java Tue Mar 12 20:01:36 2013 -0700 @@ -180,8 +180,8 @@ * comparator. * @since 1.8 */ - default <S extends T> Comparator<S> reverseOrder() { - return (Comparator<S>) Collections.reverseOrder(this); + default Comparator<T> reverseOrder() { + return Collections.reverseOrder(this); } /** @@ -200,7 +200,7 @@ * @throws NullPointerException if the argument is null. * @since 1.8 */ - default <S extends T> Comparator<S> thenComparing(Comparator<? super T> other) { + default <S extends T> Comparator<S> thenComparing(Comparator<? super S> other) { return (Comparator<S> & Serializable) (c1, c2) -> { int res = compare(c1, c2); return (res != 0) ? res : other.compare(c1, c2); @@ -223,7 +223,7 @@ * @see #thenComparing(Comparator) * @since 1.8 */ - default <S extends T, U> Comparator<S> thenComparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> cmp) { + default <S extends T, U> Comparator<S> thenComparing(Function<? super S, ? extends U> keyExtractor, Comparator<? super U> cmp) { return thenComparing(Comparators.comparing(keyExtractor, cmp)); } @@ -242,7 +242,7 @@ * @see #thenComparing(Comparator) * @since 1.8 */ - default <S extends T, U extends Comparable<? super U>> Comparator<S> thenComparing(Function<? super T, ? extends U> keyExtractor) { + default <S extends T, U extends Comparable<? super U>> Comparator<S> thenComparing(Function<? super S, ? extends U> keyExtractor) { return thenComparing(Comparators.comparing(keyExtractor)); } @@ -260,7 +260,7 @@ * @see #thenComparing(Comparator) * @since 1.8 */ - default <S extends T> Comparator<S> thenComparing(ToIntFunction<? super T> keyExtractor) { + default <S extends T> Comparator<S> thenComparing(ToIntFunction<? super S> keyExtractor) { return thenComparing(Comparators.comparing(keyExtractor)); } @@ -278,7 +278,7 @@ * @see #thenComparing(Comparator) * @since 1.8 */ - default <S extends T> Comparator<S> thenComparing(ToLongFunction<? super T> keyExtractor) { + default <S extends T> Comparator<S> thenComparing(ToLongFunction<? super S> keyExtractor) { return thenComparing(Comparators.comparing(keyExtractor)); } @@ -296,7 +296,7 @@ * @see #thenComparing(Comparator) * @since 1.8 */ - default <S extends T> Comparator<S> thenComparing(ToDoubleFunction<? super T> keyExtractor) { + default <S extends T> Comparator<S> thenComparing(ToDoubleFunction<? super S> keyExtractor) { return thenComparing(Comparators.comparing(keyExtractor)); } }
--- a/src/share/classes/java/util/Comparators.java Tue Mar 12 22:38:54 2013 -0400 +++ b/src/share/classes/java/util/Comparators.java Tue Mar 12 20:01:36 2013 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/util/Comparator/BasicTest.java Tue Mar 12 20:01:36 2013 -0700 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @summary Comparator default method tests + * @run testng BasicTest + */ + +import java.util.TreeMap; +import java.util.Comparator; +import java.util.Comparators; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertTrue; + +@Test(groups = "unit") +public class BasicTest { + public void testComposeComparator() { + // Longer string in front + Comparator<String> first = (s1, s2) -> s2.length() - s1.length(); + Comparator<String> second = Comparators.naturalOrder(); + Comparator<String> composed = first.thenComparing(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); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/util/Comparator/TypeTest.java Tue Mar 12 20:01:36 2013 -0700 @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @summary Comparator API narrowing type test + * @run testng TypeTest + */ + +import java.util.function.Function; +import java.util.Map; +import java.util.TreeMap; +import java.util.Comparator; +import java.util.Comparators; +import org.testng.annotations.Test; + +@Test(groups = "unit") +public class TypeTest { + static class Person { + String name; + static Comparator<Person> C = (p1, p2) -> p1.name.compareTo(p2.name); + + Person(String name) { + this.name = name; + } + + String getName() { return name; } + } + + static class Employee extends Person { + int id; + static Comparator<Employee> C = (e1, e2) -> e1.id - e2.id; + + Employee(int id, String name) { + super(name); + this.id = id; + } + } + + static class Manager extends Employee { + long reports; + static Comparator<Manager> C = (e1, e2) -> (int) (e1.reports - e2.reports); + + Manager(String name, int id, long reports) { + super(id, name); + this.reports = reports; + } + } + + static <T> void assertOrder(T o1, T o2, Comparator<? super T> cmp) { + if (cmp.compare(o1, o2) > 0) { + System.out.println("Fail!!"); + } + if (cmp.compare(o1, o2) == 0) { + System.out.println("Equal!!"); + } + } + + public static void main(String[] args) { + Manager m1 = new Manager("Manager", 2, 2000); + Manager m2 = new Manager("Manager", 4, 1300); + + // Comparator<Employee> tmp = Person.C; + + Comparator<Manager> cmp = Employee.C.thenComparing(Person.C); + assertOrder(m1, m2, Employee.C.thenComparing(Person.C)); + assertOrder(m1, m2, cmp); + assertOrder(m1, new Employee(1, "Z"), Person.C); + assertOrder(new Employee(1, "Z"), m2, Employee.C); + + assertOrder(m1, m2, Comparators.comparing(Employee::getName, String.CASE_INSENSITIVE_ORDER)); + + Map<String, Integer> map = new TreeMap<>(); + map.entrySet().stream().sorted(Comparators.byKey(String.CASE_INSENSITIVE_ORDER)); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/util/Comparators/BasicTest.java Tue Mar 12 20:01:36 2013 -0700 @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8001667 + * @run testng BasicTest + */ + +import java.util.Comparator; +import java.util.Comparators; +import java.util.AbstractMap; +import java.util.Map; +import org.testng.annotations.Test; + +import java.util.function.Function; +import java.util.function.ToIntFunction; +import java.util.function.ToLongFunction; +import java.util.function.ToDoubleFunction; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.assertSame; + +/** + * Unit tests for helper methods in Comparators + */ +@Test(groups = "unit") +public class BasicTest { + private static class Thing { + public final int intField; + public final long longField; + public final double doubleField; + public final String stringField; + + private Thing(int intField, long longField, double doubleField, String stringField) { + this.intField = intField; + this.longField = longField; + this.doubleField = doubleField; + this.stringField = stringField; + } + + public int getIntField() { + return intField; + } + + public long getLongField() { + return longField; + } + + public double getDoubleField() { + return doubleField; + } + + public String getStringField() { + return stringField; + } + } + + private final int[] intValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 }; + private final long[] longValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 }; + private final double[] doubleValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 }; + private final String[] stringValues = { "a", "a", "b", "b", "c", "c", "d", "d", "e", "e" }; + private final int[] comparisons = { 0, -1, 0, -1, 0, -1, 0, -1, 0 }; + + private<T> void assertComparisons(T[] things, Comparator<T> comp, int[] comparisons) { + for (int i=0; i<comparisons.length; i++) { + assertEquals(comparisons.length + 1, things.length); + assertEquals(comparisons[i], comp.compare(things[i], things[i+1])); + assertEquals(-comparisons[i], comp.compare(things[i+1], things[i])); + } + } + + 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 = Comparators.comparing(new ToIntFunction<Thing>() { + @Override + public int applyAsInt(Thing thing) { + return thing.getIntField(); + } + }); + + assertComparisons(things, comp, comparisons); + } + + 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 = Comparators.comparing(new ToLongFunction<Thing>() { + @Override + public long applyAsLong(Thing thing) { + return thing.getLongField(); + } + }); + + assertComparisons(things, comp, comparisons); + } + + 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 = Comparators.comparing(new ToDoubleFunction<Thing>() { + @Override + public double applyAsDouble(Thing thing) { + return thing.getDoubleField(); + } + }); + + assertComparisons(things, comp, comparisons); + } + + public void testComparing() { + Thing[] things = new Thing[doubleValues.length]; + for (int i=0; i<doubleValues.length; i++) + things[i] = new Thing(0, 0L, 0.0, stringValues[i]); + Comparator<Thing> comp = Comparators.comparing(new Function<Thing, String>() { + @Override + public String apply(Thing thing) { + return thing.getStringField(); + } + }); + + assertComparisons(things, comp, comparisons); + } + + public void testNaturalOrderComparator() { + Comparator<String> comp = Comparators.naturalOrder(); + + assertComparisons(stringValues, comp, comparisons); + } + + public void testReverseComparator() { + Comparator<String> cmpr = Comparators.reverseOrder(); + Comparator<String> cmp = cmpr.reverseOrder(); + + assertEquals(cmp.reverseOrder(), cmpr); + assertEquals(0, cmp.compare("a", "a")); + assertEquals(0, cmpr.compare("a", "a")); + assertTrue(cmp.compare("a", "b") < 0); + assertTrue(cmpr.compare("a", "b") > 0); + assertTrue(cmp.compare("b", "a") > 0); + assertTrue(cmpr.compare("b", "a") < 0); + } + + public void testReverseComparator2() { + Comparator<String> cmp = (s1, s2) -> s1.length() - s2.length(); + Comparator<String> cmpr = cmp.reverseOrder(); + + assertEquals(cmpr.reverseOrder(), cmp); + assertEquals(0, cmp.compare("abc", "def")); + assertEquals(0, cmpr.compare("abc", "def")); + assertTrue(cmp.compare("abcd", "def") > 0); + assertTrue(cmpr.compare("abcd", "def") < 0); + assertTrue(cmp.compare("abc", "defg") < 0); + assertTrue(cmpr.compare("abc", "defg") > 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) { + final Map.Entry<K, V> p11 = new AbstractMap.SimpleImmutableEntry<>(k1, v1); + final Map.Entry<K, V> p12 = new AbstractMap.SimpleImmutableEntry<>(k1, v2); + final Map.Entry<K, V> p21 = new AbstractMap.SimpleImmutableEntry<>(k2, v1); + final Map.Entry<K, V> p22 = new AbstractMap.SimpleImmutableEntry<>(k2, v2); + + assertTrue(ck.compare(p11, p11) == 0); + assertTrue(ck.compare(p12, p11) == 0); + assertTrue(ck.compare(p11, p12) == 0); + assertTrue(ck.compare(p12, p22) < 0); + assertTrue(ck.compare(p12, p21) < 0); + assertTrue(ck.compare(p21, p11) > 0); + assertTrue(ck.compare(p21, p12) > 0); + + assertTrue(cv.compare(p11, p11) == 0); + assertTrue(cv.compare(p12, p11) > 0); + assertTrue(cv.compare(p11, p12) < 0); + assertTrue(cv.compare(p12, p22) == 0); + assertTrue(cv.compare(p12, p21) > 0); + assertTrue(cv.compare(p21, p11) == 0); + assertTrue(cv.compare(p21, p12) < 0); + + 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); + assertTrue(cmp.compare(p12, p22) < 0); + assertTrue(cmp.compare(p12, p21) < 0); + assertTrue(cmp.compare(p21, p11) > 0); + assertTrue(cmp.compare(p21, p12) > 0); + + cmp = cv.thenComparing(ck); + assertTrue(cmp.compare(p11, p11) == 0); + assertTrue(cmp.compare(p12, p11) > 0); + assertTrue(cmp.compare(p11, p12) < 0); + assertTrue(cmp.compare(p12, p22) < 0); + assertTrue(cmp.compare(p12, p21) > 0); + assertTrue(cmp.compare(p21, p11) > 0); + assertTrue(cmp.compare(p21, p12) < 0); + } + + public void testKVComparatorable() { + assertPairComparison(1, "ABC", 2, "XYZ", + Comparators.<Integer, String>naturalOrderKeys(), + Comparators.<Integer, String>naturalOrderValues()); + } + + private static class People { + final String firstName; + final String lastName; + final int age; + + People(String first, String last, int age) { + firstName = first; + lastName = last; + this.age = age; + } + + String getFirstName() { return firstName; } + String getLastName() { return lastName; } + int getAge() { return age; } + long getAgeAsLong() { return (long) age; }; + double getAgeAsDouble() { return (double) age; }; + } + + private final People people[] = { + new People("John", "Doe", 34), + new People("Mary", "Doe", 30), + new People("Maria", "Doe", 14), + new People("Jonah", "Doe", 10), + new People("John", "Cook", 54), + new People("Mary", "Cook", 50), + }; + + public void testKVComparators() { + // Comparator<People> cmp = Comparators.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 = Comparators.comparing((Function<People, String>) People::getFirstName); + Comparator<People> cmp2 = Comparators.comparing((Function<People, String>) People::getLastName); + Comparator<People> cmp = cmp1.thenComparing(cmp2); + + assertPairComparison(people[0], people[0], people[1], people[1], + Comparators.<People, People>byKey(cmp), + Comparators.<People, People>byValue(cmp)); + + } + + private <T> void assertComparison(Comparator<T> cmp, T less, T greater) { + assertTrue(cmp.compare(less, greater) < 0, "less"); + assertTrue(cmp.compare(less, less) == 0, "equal"); + assertTrue(cmp.compare(greater, less) > 0, "greater"); + } + + public void testComparatorDefaultMethods() { + Comparator<People> cmp = Comparators.comparing((Function<People, String>) People::getFirstName); + Comparator<People> cmp2 = Comparators.comparing((Function<People, String>) People::getLastName); + // reverseOrder + assertComparison(cmp.reverseOrder(), 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.<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((ToIntFunction<People>) People::getAge), people[0], people[1]); + assertComparison(cmp.thenComparing((ToIntFunction<People>) People::getAge), people[1], people[5]); + // thenComparing(ToLongFunction) + 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((ToDoubleFunction<People>) People::getAgeAsDouble), people[0], people[1]); + assertComparison(cmp.thenComparing((ToDoubleFunction<People>) People::getAgeAsDouble), people[1], people[5]); + } + + public void testGreaterOf() { + // lesser + assertSame(Comparators.greaterOf(Comparators.comparing( + (Function<People, String>) People::getFirstName)) + .apply(people[0], people[1]), + people[1]); + // euqal + assertSame(Comparators.greaterOf(Comparators.comparing( + (Function<People, String>) People::getLastName)) + .apply(people[0], people[1]), + people[0]); + // greater + assertSame(Comparators.greaterOf(Comparators.comparing( + (ToIntFunction<People>) People::getAge)) + .apply(people[0], people[1]), + people[0]); + } + + public void testLesserOf() { + // lesser + assertSame(Comparators.lesserOf(Comparators.comparing( + (Function<People, String>) People::getFirstName)) + .apply(people[0], people[1]), + people[0]); + // euqal + assertSame(Comparators.lesserOf(Comparators.comparing( + (Function<People, String>) People::getLastName)) + .apply(people[0], people[1]), + people[0]); + // greater + assertSame(Comparators.lesserOf(Comparators.comparing( + (ToIntFunction<People>) People::getAge)) + .apply(people[0], people[1]), + people[1]); + } +}
--- a/test/java/util/ComparatorsTest.java Tue Mar 12 22:38:54 2013 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,348 +0,0 @@ -/* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @bug 8001667 - * @run testng ComparatorsTest - */ - -import java.util.Comparator; -import java.util.Comparators; -import java.util.AbstractMap; -import java.util.Map; -import org.testng.annotations.Test; - -import java.util.function.Function; -import java.util.function.ToIntFunction; -import java.util.function.ToLongFunction; -import java.util.function.ToDoubleFunction; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.assertSame; - -/** - * Unit tests for helper methods in Comparators - */ -@Test(groups = "unit") -public class ComparatorsTest { - private static class Thing { - public final int intField; - public final long longField; - public final double doubleField; - public final String stringField; - - private Thing(int intField, long longField, double doubleField, String stringField) { - this.intField = intField; - this.longField = longField; - this.doubleField = doubleField; - this.stringField = stringField; - } - - public int getIntField() { - return intField; - } - - public long getLongField() { - return longField; - } - - public double getDoubleField() { - return doubleField; - } - - public String getStringField() { - return stringField; - } - } - - private final int[] intValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 }; - private final long[] longValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 }; - private final double[] doubleValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 }; - private final String[] stringValues = { "a", "a", "b", "b", "c", "c", "d", "d", "e", "e" }; - private final int[] comparisons = { 0, -1, 0, -1, 0, -1, 0, -1, 0 }; - - private<T> void assertComparisons(T[] things, Comparator<T> comp, int[] comparisons) { - for (int i=0; i<comparisons.length; i++) { - assertEquals(comparisons.length + 1, things.length); - assertEquals(comparisons[i], comp.compare(things[i], things[i+1])); - assertEquals(-comparisons[i], comp.compare(things[i+1], things[i])); - } - } - - 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 = Comparators.comparing(new ToIntFunction<ComparatorsTest.Thing>() { - @Override - public int applyAsInt(Thing thing) { - return thing.getIntField(); - } - }); - - assertComparisons(things, comp, comparisons); - } - - 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 = Comparators.comparing(new ToLongFunction<ComparatorsTest.Thing>() { - @Override - public long applyAsLong(Thing thing) { - return thing.getLongField(); - } - }); - - assertComparisons(things, comp, comparisons); - } - - 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 = Comparators.comparing(new ToDoubleFunction<ComparatorsTest.Thing>() { - @Override - public double applyAsDouble(Thing thing) { - return thing.getDoubleField(); - } - }); - - assertComparisons(things, comp, comparisons); - } - - public void testComparing() { - Thing[] things = new Thing[doubleValues.length]; - for (int i=0; i<doubleValues.length; i++) - things[i] = new Thing(0, 0L, 0.0, stringValues[i]); - Comparator<Thing> comp = Comparators.comparing(new Function<Thing, String>() { - @Override - public String apply(Thing thing) { - return thing.getStringField(); - } - }); - - assertComparisons(things, comp, comparisons); - } - - public void testNaturalOrderComparator() { - Comparator<String> comp = Comparators.naturalOrder(); - - assertComparisons(stringValues, comp, comparisons); - } - - public void testReverseComparator() { - Comparator<String> cmpr = Comparators.reverseOrder(); - Comparator<String> cmp = cmpr.reverseOrder(); - - assertEquals(cmp.reverseOrder(), cmpr); - assertEquals(0, cmp.compare("a", "a")); - assertEquals(0, cmpr.compare("a", "a")); - assertTrue(cmp.compare("a", "b") < 0); - assertTrue(cmpr.compare("a", "b") > 0); - assertTrue(cmp.compare("b", "a") > 0); - assertTrue(cmpr.compare("b", "a") < 0); - } - - public void testReverseComparator2() { - Comparator<String> cmp = (s1, s2) -> s1.length() - s2.length(); - Comparator<String> cmpr = cmp.reverseOrder(); - - assertEquals(cmpr.reverseOrder(), cmp); - assertEquals(0, cmp.compare("abc", "def")); - assertEquals(0, cmpr.compare("abc", "def")); - assertTrue(cmp.compare("abcd", "def") > 0); - assertTrue(cmpr.compare("abcd", "def") < 0); - assertTrue(cmp.compare("abc", "defg") < 0); - assertTrue(cmpr.compare("abc", "defg") > 0); - } - - public void testComposeComparator() { - // Longer string in front - Comparator<String> first = (s1, s2) -> s2.length() - s1.length(); - Comparator<String> second = Comparators.naturalOrder(); - Comparator<String> composed = first.thenComparing(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) { - final Map.Entry<K, V> p11 = new AbstractMap.SimpleImmutableEntry<>(k1, v1); - final Map.Entry<K, V> p12 = new AbstractMap.SimpleImmutableEntry<>(k1, v2); - final Map.Entry<K, V> p21 = new AbstractMap.SimpleImmutableEntry<>(k2, v1); - final Map.Entry<K, V> p22 = new AbstractMap.SimpleImmutableEntry<>(k2, v2); - - assertTrue(ck.compare(p11, p11) == 0); - assertTrue(ck.compare(p12, p11) == 0); - assertTrue(ck.compare(p11, p12) == 0); - assertTrue(ck.compare(p12, p22) < 0); - assertTrue(ck.compare(p12, p21) < 0); - assertTrue(ck.compare(p21, p11) > 0); - assertTrue(ck.compare(p21, p12) > 0); - - assertTrue(cv.compare(p11, p11) == 0); - assertTrue(cv.compare(p12, p11) > 0); - assertTrue(cv.compare(p11, p12) < 0); - assertTrue(cv.compare(p12, p22) == 0); - assertTrue(cv.compare(p12, p21) > 0); - assertTrue(cv.compare(p21, p11) == 0); - assertTrue(cv.compare(p21, p12) < 0); - - 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); - assertTrue(cmp.compare(p12, p22) < 0); - assertTrue(cmp.compare(p12, p21) < 0); - assertTrue(cmp.compare(p21, p11) > 0); - assertTrue(cmp.compare(p21, p12) > 0); - - cmp = cv.thenComparing(ck); - assertTrue(cmp.compare(p11, p11) == 0); - assertTrue(cmp.compare(p12, p11) > 0); - assertTrue(cmp.compare(p11, p12) < 0); - assertTrue(cmp.compare(p12, p22) < 0); - assertTrue(cmp.compare(p12, p21) > 0); - assertTrue(cmp.compare(p21, p11) > 0); - assertTrue(cmp.compare(p21, p12) < 0); - } - - public void testKVComparatorable() { - assertPairComparison(1, "ABC", 2, "XYZ", - Comparators.<Integer, String>naturalOrderKeys(), - Comparators.<Integer, String>naturalOrderValues()); - } - - private static class People { - final String firstName; - final String lastName; - final int age; - - People(String first, String last, int age) { - firstName = first; - lastName = last; - this.age = age; - } - - String getFirstName() { return firstName; } - String getLastName() { return lastName; } - int getAge() { return age; } - long getAgeAsLong() { return (long) age; }; - double getAgeAsDouble() { return (double) age; }; - } - - private final People people[] = { - new People("John", "Doe", 34), - new People("Mary", "Doe", 30), - new People("Maria", "Doe", 14), - new People("Jonah", "Doe", 10), - new People("John", "Cook", 54), - new People("Mary", "Cook", 50), - }; - - public void testKVComparators() { - // Comparator<People> cmp = Comparators.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 = Comparators.comparing((Function<People, String>) People::getFirstName); - Comparator<People> cmp2 = Comparators.comparing((Function<People, String>) People::getLastName); - Comparator<People> cmp = cmp1.thenComparing(cmp2); - - assertPairComparison(people[0], people[0], people[1], people[1], - Comparators.<People, People>byKey(cmp), - Comparators.<People, People>byValue(cmp)); - - } - - private <T> void assertComparison(Comparator<T> cmp, T less, T greater) { - assertTrue(cmp.compare(less, greater) < 0, "less"); - assertTrue(cmp.compare(less, less) == 0, "equal"); - assertTrue(cmp.compare(greater, less) > 0, "greater"); - } - - public void testComparatorDefaultMethods() { - Comparator<People> cmp = Comparators.comparing((Function<People, String>) People::getFirstName); - Comparator<People> cmp2 = Comparators.comparing((Function<People, String>) People::getLastName); - // reverseOrder - assertComparison(cmp.reverseOrder(), 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 testGreaterOf() { - // lesser - assertSame(Comparators.greaterOf(Comparators.comparing( - (Function<People, String>) People::getFirstName)) - .apply(people[0], people[1]), - people[1]); - // euqal - assertSame(Comparators.greaterOf(Comparators.comparing( - (Function<People, String>) People::getLastName)) - .apply(people[0], people[1]), - people[0]); - // greater - assertSame(Comparators.greaterOf(Comparators.comparing( - (ToIntFunction<People>) People::getAge)) - .apply(people[0], people[1]), - people[0]); - } - - public void testLesserOf() { - // lesser - assertSame(Comparators.lesserOf(Comparators.comparing( - (Function<People, String>) People::getFirstName)) - .apply(people[0], people[1]), - people[0]); - // euqal - assertSame(Comparators.lesserOf(Comparators.comparing( - (Function<People, String>) People::getLastName)) - .apply(people[0], people[1]), - people[0]); - // greater - assertSame(Comparators.lesserOf(Comparators.comparing( - (ToIntFunction<People>) People::getAge)) - .apply(people[0], people[1]), - people[1]); - } -}