1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @summary Comparator API narrowing type test 27 * @run testng TypeTest 28 */ 29 30 import java.util.function.Function; 31 import java.util.Map; 32 import java.util.TreeMap; 33 import java.util.Comparator; 34 import org.testng.annotations.Test; 35 36 @Test(groups = "unit") 37 public class TypeTest { 38 static class Person { 39 String name; 40 static Comparator<Person> C = (p1, p2) -> p1.name.compareTo(p2.name); 41 42 Person(String name) { 43 this.name = name; 44 } 45 46 String getName() { return name; } 47 } 48 49 static class Employee extends Person { 50 int id; 51 static Comparator<Employee> C = (e1, e2) -> e1.id - e2.id; 52 53 Employee(int id, String name) { 54 super(name); 55 this.id = id; 56 } 57 } 58 59 static class Manager extends Employee { 60 long reports; 61 static Comparator<Manager> C = (e1, e2) -> (int) (e1.reports - e2.reports); 62 63 Manager(String name, int id, long reports) { 64 super(id, name); 65 this.reports = reports; 66 } 67 } 68 69 static <T> void assertOrder(T o1, T o2, Comparator<? super T> cmp) { 70 if (cmp.compare(o1, o2) > 0) { 71 System.out.println("Fail!!"); 72 } 73 if (cmp.compare(o1, o2) == 0) { 74 System.out.println("Equal!!"); 75 } 76 } 77 78 public static void main(String[] args) { 79 Manager m1 = new Manager("Manager", 2, 2000); 80 Manager m2 = new Manager("Manager", 4, 1300); 81 82 // Comparator<Employee> tmp = Person.C; 83 84 // Comparator<Manager> cmp = Employee.C.thenComparing(Person.C); 85 Comparator<Employee> cmp = Employee.C.thenComparing(Person.C); 86 assertOrder(m1, m2, Employee.C.thenComparing(Person.C)); 87 assertOrder(m1, m2, cmp); 88 assertOrder(m1, new Employee(1, "Z"), Person.C); 89 assertOrder(new Employee(1, "Z"), m2, Employee.C); 90 91 assertOrder(m1, m2, Comparator.comparing(Employee::getName, String.CASE_INSENSITIVE_ORDER)); 92 93 Map<String, Integer> map = new TreeMap<>(); 94 map.entrySet().stream().sorted(Map.Entry.comparingByKey(String.CASE_INSENSITIVE_ORDER)); 95 } 96 }