1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/methodReference/MethodRef5.java Sat Nov 17 19:01:03 2012 +0000 1.3 @@ -0,0 +1,96 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * @test 1.29 + * @bug 8003280 1.30 + * @summary Add lambda tests 1.31 + * Test method reference with SAM interface Comparator<T> 1.32 + * @compile MethodRef5.java 1.33 + * @run main MethodRef5 1.34 + */ 1.35 + 1.36 +import java.util.Collections; 1.37 +import java.util.List; 1.38 +import java.util.ArrayList; 1.39 + 1.40 +public class MethodRef5 { 1.41 + 1.42 + static class Person { 1.43 + 1.44 + private String firstName; 1.45 + private String lastName; 1.46 + private int age; 1.47 + 1.48 + public Person() { } 1.49 + 1.50 + public Person(String fn, String ln, int a) { 1.51 + firstName = fn; 1.52 + lastName = ln; 1.53 + age = a; 1.54 + } 1.55 + 1.56 + public String getLastName() { 1.57 + return lastName; 1.58 + } 1.59 + 1.60 + public int getAge() { 1.61 + return age; 1.62 + } 1.63 + 1.64 + //the following 2 methods are signature-compatible with Comparator<Person>.compare(): 1.65 + public static int compareByAge(Person a, Person b) { 1.66 + return a.age - b.age; 1.67 + } 1.68 + 1.69 + public int compareByLastName(Person a, Person b) { 1.70 + return a.lastName.compareToIgnoreCase(b.lastName); 1.71 + } 1.72 + } 1.73 + 1.74 + private static void assertTrue(boolean cond) { 1.75 + if (!cond) 1.76 + throw new AssertionError(); 1.77 + } 1.78 + 1.79 + public static void main(String[] args) { 1.80 + 1.81 + List<Person> persons = new ArrayList<Person>(); 1.82 + persons.add(new Person("John", "Smith", 49)); 1.83 + persons.add(new Person("Abraham", "Lincoln", 30)); 1.84 + persons.add(new Person("George", "Washington", 29)); 1.85 + persons.add(new Person("Peter", "Derby", 50)); 1.86 + Collections.sort(persons, Person::compareByAge);//static method reference to compareByAge(Person, Person) 1.87 + String age = ""; 1.88 + for (Person p : persons) { 1.89 + age += p.getAge() + " "; 1.90 + } 1.91 + assertTrue( (age.equals("29 30 49 50 ")) ); 1.92 + Collections.sort(persons, new Person()::compareByLastName);//instance method reference to compareByLastName(Person, Person) 1.93 + String lastName = ""; 1.94 + for (Person p : persons) { 1.95 + lastName += p.getLastName() + " "; 1.96 + } 1.97 + assertTrue( lastName.equals("Derby Lincoln Smith Washington ") ); 1.98 + } 1.99 +}