|
1 /* |
|
2 * Copyright (c) 2011, 2014, 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 * Test lambda expressions for existing SAM interfaces like Runnable and Comparator<T> |
|
29 * @compile LambdaTest1.java |
|
30 * @run main LambdaTest1 |
|
31 */ |
|
32 |
|
33 import java.util.Collections; |
|
34 import java.util.List; |
|
35 import java.util.ArrayList; |
|
36 |
|
37 public class LambdaTest1 { |
|
38 |
|
39 private static String assertionStr = ""; |
|
40 |
|
41 private static void assertTrue(boolean b) { |
|
42 if(!b) |
|
43 throw new AssertionError(); |
|
44 } |
|
45 |
|
46 private static void test1(Runnable r) { |
|
47 r.run(); |
|
48 } |
|
49 |
|
50 void test2(Object o) { |
|
51 if(o instanceof Runnable) |
|
52 ((Runnable)o).run(); |
|
53 } |
|
54 |
|
55 Runnable test3() { |
|
56 return ()-> { assertionStr += "Runnable6"; }; |
|
57 } |
|
58 |
|
59 public static void main(String[] args) { |
|
60 |
|
61 //lambda expressions for SAM interface Runnable: |
|
62 //assign: |
|
63 Runnable r = ()-> { assertionStr += "Runnable1 "; }; |
|
64 r.run(); |
|
65 |
|
66 //cast: |
|
67 ((Runnable)()-> { assertionStr += "Runnable2 "; }).run(); |
|
68 |
|
69 Object o = (Runnable)()-> {}; |
|
70 |
|
71 o = (Runnable)()-> { |
|
72 switch (assertionStr) { |
|
73 case "Runnable1 Runnable2 ": |
|
74 assertionStr += "Runnable3 "; |
|
75 break; |
|
76 default: |
|
77 throw new AssertionError(); |
|
78 } |
|
79 return; |
|
80 }; |
|
81 |
|
82 //method parameter: |
|
83 test1(()-> { assertionStr += "Runnable4 "; return; }); |
|
84 |
|
85 LambdaTest1 test = new LambdaTest1(); |
|
86 test.test2((Runnable)()-> { assertionStr += "Runnable5 "; }); |
|
87 |
|
88 //return type: |
|
89 r = test.test3(); |
|
90 r.run(); |
|
91 |
|
92 assertTrue(assertionStr.equals("Runnable1 Runnable2 Runnable4 Runnable5 Runnable6")); |
|
93 |
|
94 //lambda expressions for SAM interface Comparator<T>: |
|
95 List<Integer> list = new ArrayList<Integer>(); |
|
96 list.add(4); |
|
97 list.add(10); |
|
98 list.add(-5); |
|
99 list.add(100); |
|
100 list.add(9); |
|
101 Collections.sort(list, (Integer i1, Integer i2)-> i2 - i1); |
|
102 String result = ""; |
|
103 for(int i : list) |
|
104 result += i + " "; |
|
105 assertTrue(result.equals("100 10 9 4 -5 ")); |
|
106 |
|
107 Collections.sort(list, |
|
108 (i1, i2) -> { |
|
109 String s1 = i1.toString(); |
|
110 String s2 = i2.toString(); |
|
111 return s1.length() - s2.length(); |
|
112 }); |
|
113 result = ""; |
|
114 for(int i : list) |
|
115 result += i + " "; |
|
116 assertTrue(result.equals("9 4 10 -5 100 ")); |
|
117 } |
|
118 } |