LambdaTest2_SAM3.java revision 2933:49d207bf704d
1/*
2 * Copyright (c) 2011, 2015, 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 * @bug 8003280
27 * @summary Add lambda tests
28 *   This test is for identifying SAM types #5 and instantiating non-SAM types #7 through inner class,
29             see Helper.java for SAM types
30 * @modules java.sql
31 * @compile LambdaTest2_SAM3.java Helper.java
32 * @run main LambdaTest2_SAM3
33 */
34
35import java.util.Collection;
36import java.util.List;
37import java.util.ArrayList;
38
39public class LambdaTest2_SAM3 {
40    private static List<String> strs = new ArrayList<String>();
41    private static List<Integer> integers = new ArrayList<Integer>();
42
43    public static void main(String[] args) {
44        LambdaTest2_SAM3 test = new LambdaTest2_SAM3();
45
46        //type #7, Not SAM-convertible, through inner class only:
47        test.methodFooBar(new FooBar() {
48                public int getAge(Number n) {
49                    System.out.println("getAge(Number n) called");
50                    return 100;
51                }
52                public int getAge(Integer i) {
53                    System.out.println("getAge(Integer i) called");
54                    return 200;
55                }
56            }
57        );
58
59        //type #7:
60        test.methodDE(new DE(){
61                public int getOldest(List<Integer > list) {
62                    System.out.println("getOldest(List<Integer> list) called");
63                    return 100;
64                }
65                public int getOldest(Collection<?> collection) {
66                    System.out.println("getOldest(Collection<?> collection) called");
67                    return 200;
68                }
69            }
70        );
71
72    }
73
74    //type #7: Not SAM type
75    void methodFooBar(FooBar fb) {
76        System.out.println("methodFooBar(): interface FooBar object instantiated: " + fb);
77        System.out.println("result=" + fb.getAge(new Byte("10")));
78        System.out.println("result=" + fb.getAge(new Integer(10)));
79    }
80
81    //type #7: Not SAM type
82    void methodDE (DE de) {
83        System.out.println("methodDE(): interface DE object instantiated: " + de);
84        System.out.println(de.getOldest(integers));
85        System.out.println(de.getOldest(strs));
86    }
87}
88