LambdaTest1.java revision 1414:01c9d4161882
1/*
2 * Copyright (c) 2012, 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 lambda expressions
29 * @compile LambdaTest1.java
30 * @run main LambdaTest1
31 */
32
33import java.util.Collections;
34import java.util.List;
35import java.util.ArrayList;
36
37public class LambdaTest1 {
38    public static void main(String[] args) {
39
40        LambdaTest1 test = new LambdaTest1();
41
42        test.method2((int n) -> { });
43        test.method2((int n) -> { });
44        test.method2((int n) -> { return; }); // ";" is mandatory here
45        test.method2((int n) -> { System.out.println(n); }); // ";" is optional here
46        test.method2(n -> { System.out.println(n); }); //warning, explict type required for n?
47
48        test.method3(()-> { System.out.println("implementing VoidVoid.vvMethod()"); });
49        test.method3(() -> {});
50
51        test.method4(()-> 42);
52        test.method4(()-> { return 42; });//";" is mandatory here
53
54        test.method5((int n)-> n+1);
55        test.method5((int n) -> 42);
56        test.method5((int n) -> { return 42; });
57        test.method5(
58            (int n) -> { //"{" optional here
59                if(n > 0)
60                    return n++;
61                else
62                    return n--;
63            }
64        );
65
66        Runnable r = ()-> { System.out.println("Runnable.run() method implemented"); };
67        r.run();
68        ((Runnable)()-> { System.out.println("Runnable.run() method implemented"); }).run();
69    }
70
71    void method2(VoidInt a) {
72        System.out.println("method2()");
73        final int N = 1;
74        int n = 2; //effectively final variable
75        System.out.println("method2() \"this\":" + this);
76        ((Runnable)
77            ()->{
78                System.out.println("inside lambda \"this\":" + this);
79                System.out.println("inside lambda accessing final variable N:" + N);
80                System.out.println("inside lambda accessing effectively final variable n:" + n);
81            }
82        ).run();
83        //n++; //compile error if n is modified
84        a.viMethod(2);
85    }
86
87    void method3(VoidVoid a) {
88        System.out.println("method3()");
89        a.vvMethod();
90    }
91
92    void method4(IntVoid a) {
93        System.out.println("method4()");
94        System.out.println(a.ivMethod());
95    }
96
97    void method5(IntInt a) {
98        System.out.println("method5()");
99        System.out.println(a.iiMethod(5));
100    }
101
102
103    //SAM type interfaces
104    interface VoidInt {
105        void viMethod(int n);
106    }
107
108    interface VoidVoid {
109        void vvMethod();
110    }
111
112    interface IntVoid {
113        int ivMethod();
114    }
115
116    interface IntInt {
117        int iiMethod(int n);
118    }
119}
120