Fri, 30 Nov 2012 15:14:25 +0000
8004102: Add support for generic functional descriptors
Summary: Method references are allowed to have a generic functional interface descriptor target
Reviewed-by: jjg
1 /*
2 * Copyright (c) 2011, 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 */
24 /**
25 * @test
26 * @bug 8003280
27 * @summary Add lambda tests
28 * Test method reference with SAM interface Comparator<T>
29 * @compile MethodRef5.java
30 * @run main MethodRef5
31 */
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.ArrayList;
37 public class MethodRef5 {
39 static class Person {
41 private String firstName;
42 private String lastName;
43 private int age;
45 public Person() { }
47 public Person(String fn, String ln, int a) {
48 firstName = fn;
49 lastName = ln;
50 age = a;
51 }
53 public String getLastName() {
54 return lastName;
55 }
57 public int getAge() {
58 return age;
59 }
61 //the following 2 methods are signature-compatible with Comparator<Person>.compare():
62 public static int compareByAge(Person a, Person b) {
63 return a.age - b.age;
64 }
66 public int compareByLastName(Person a, Person b) {
67 return a.lastName.compareToIgnoreCase(b.lastName);
68 }
69 }
71 private static void assertTrue(boolean cond) {
72 if (!cond)
73 throw new AssertionError();
74 }
76 public static void main(String[] args) {
78 List<Person> persons = new ArrayList<Person>();
79 persons.add(new Person("John", "Smith", 49));
80 persons.add(new Person("Abraham", "Lincoln", 30));
81 persons.add(new Person("George", "Washington", 29));
82 persons.add(new Person("Peter", "Derby", 50));
83 Collections.sort(persons, Person::compareByAge);//static method reference to compareByAge(Person, Person)
84 String age = "";
85 for (Person p : persons) {
86 age += p.getAge() + " ";
87 }
88 assertTrue( (age.equals("29 30 49 50 ")) );
89 Collections.sort(persons, new Person()::compareByLastName);//instance method reference to compareByLastName(Person, Person)
90 String lastName = "";
91 for (Person p : persons) {
92 lastName += p.getLastName() + " ";
93 }
94 assertTrue( lastName.equals("Derby Lincoln Smith Washington ") );
95 }
96 }