1 /*
2  * Copyright (c) 2012, 2016, 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 import java.util.function.Consumer;
25 
26 import org.testng.annotations.Test;
27 
28 import static org.testng.Assert.assertEquals;
29 
30 /**
31  * @author Robert Field
32  */
33 
34 @Test
35 public class LambdaTranslationTest1 extends LT1Sub {
36 
37     String cntxt = "blah";
38 
39     private static final ThreadLocal<Object> result = new ThreadLocal<>();
40 
setResult(Object s)41     private static void setResult(Object s) { result.set(s); }
appendResult(Object s)42     private static void appendResult(Object s) { result.set(result.get().toString() + s); }
43 
assertResult(String expected)44     private static void assertResult(String expected) {
45         assertEquals(result.get().toString(), expected);
46     }
47 
count(String s)48     static Integer count(String s) {
49         return s.length();
50     }
51 
icount(String s)52     static int icount(String s) {
53         return s.length();
54     }
55 
eye(Integer i)56     static void eye(Integer i) {
57         setResult(String.format("I:%d", i));
58     }
59 
ieye(int i)60     static void ieye(int i) {
61         setResult(String.format("i:%d", i));
62     }
63 
deye(double d)64     static void deye(double d) {
65         setResult(String.format("d:%f", d));
66     }
67 
testLambdas()68     public void testLambdas() {
69         Consumer<Object> b = t -> {setResult("Sink0::" + t);};
70         b.accept("Howdy");
71         assertResult("Sink0::Howdy");
72 
73         Consumer<String> b1 = t -> {setResult("Sink1::" + t);};
74         b1.accept("Rowdy");
75         assertResult("Sink1::Rowdy");
76 
77         for (int i = 5; i < 10; ++i) {
78             Consumer<Integer> b2 = t -> {setResult("Sink2::" + t);};
79             b2.accept(i);
80             assertResult("Sink2::" + i);
81         }
82 
83         Consumer<Integer> b3 = t -> {setResult("Sink3::" + t);};
84         for (int i = 900; i > 0; i -= 100) {
85             b3.accept(i);
86             assertResult("Sink3::" + i);
87         }
88 
89         cntxt = "blah";
90         Consumer<String> b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));};
91         b4.accept("Yor");
92         assertResult("b4: blah .. Yor");
93 
94         String flaw = "flaw";
95         Consumer<String> b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));};
96         b5.accept("BB");
97         assertResult("b5: flaw .. BB");
98 
99         cntxt = "flew";
100         Consumer<String> b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));};
101         b6.accept("flee");
102         assertResult("b6: flee .. flew .. flaw");
103 
104         Consumer<String> b7 = t -> {setResult(String.format("b7: %s %s", t, this.protectedSuperclassMethod()));};
105         b7.accept("this:");
106         assertResult("b7: this: instance:flew");
107 
108         Consumer<String> b8 = t -> {setResult(String.format("b8: %s %s", t, super.protectedSuperclassMethod()));};
109         b8.accept("super:");
110         assertResult("b8: super: I'm the sub");
111 
112         Consumer<String> b7b = t -> {setResult(String.format("b9: %s %s", t, protectedSuperclassMethod()));};
113         b7b.accept("implicit this:");
114         assertResult("b9: implicit this: instance:flew");
115 
116         Consumer<Object> b10 = t -> {setResult(String.format("b10: new LT1Thing: %s", (new LT1Thing(t)).str));};
117         b10.accept("thing");
118         assertResult("b10: new LT1Thing: thing");
119 
120         Consumer<Object> b11 = t -> {setResult(String.format("b11: %s", (new LT1Thing(t) {
121             String get() {
122                 return "*" + str.toString() + "*";
123             }
124         }).get()));};
125         b11.accept(999);
126         assertResult("b11: *999*");
127     }
128 
testMethodRefs()129     public void testMethodRefs() {
130         LT1IA ia = LambdaTranslationTest1::eye;
131         ia.doit(1234);
132         assertResult("I:1234");
133 
134         LT1IIA iia = LambdaTranslationTest1::ieye;
135         iia.doit(1234);
136         assertResult("i:1234");
137 
138         LT1IA da = LambdaTranslationTest1::deye;
139         da.doit(1234);
140         assertResult(String.format("d:%f", 1234.0));
141 
142         LT1SA a = LambdaTranslationTest1::count;
143         assertEquals((Integer) 5, a.doit("howdy"));
144 
145         a = LambdaTranslationTest1::icount;
146         assertEquals((Integer) 6, a.doit("shower"));
147     }
148 
testInner()149     public void testInner() throws Exception {
150         (new In()).doInner();
151     }
152 
protectedSuperclassMethod()153     protected String protectedSuperclassMethod() {
154         return "instance:" + cntxt;
155     }
156 
157     private class In {
158 
159         private int that = 1234;
160 
doInner()161         void doInner() {
162             Consumer<String> i4 = t -> {setResult(String.format("i4: %d .. %s", that, t));};
163             i4.accept("=1234");
164             assertResult("i4: 1234 .. =1234");
165 
166             Consumer<String> i5 = t -> {setResult(""); appendResult(t); appendResult(t);};
167             i5.accept("fruit");
168             assertResult("fruitfruit");
169 
170             cntxt = "human";
171             Consumer<String> b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));};
172             b4.accept("bin");
173             assertResult("b4: human .. bin");
174 
175             final String flaw = "flaw";
176 
177 /**
178  Callable<String> c5 = () ->  "["+flaw+"]" ;
179  System.out.printf("c5: %s\n", c5.call() );
180  **/
181 
182             Consumer<String> b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));};
183             b5.accept("BB");
184             assertResult("b5: flaw .. BB");
185 
186             cntxt = "borg";
187             Consumer<String> b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));};
188             b6.accept("flee");
189             assertResult("b6: flee .. borg .. flaw");
190 
191             Consumer<String> b7b = t -> {setResult(String.format("b7b: %s %s", t, protectedSuperclassMethod()));};
192             b7b.accept("implicit outer this");
193             assertResult("b7b: implicit outer this instance:borg");
194 
195             /**
196              Block<Object> b9 = t -> { System.out.printf("New: %s\n", (new LT1Thing(t)).str); };
197              b9.apply("thing");
198 
199              Block<Object> ba = t -> { System.out.printf("Def: %s\n", (new LT1Thing(t) { String get() { return "*" + str.toString() +"*";}}).get() ); };
200              ba.apply(999);
201 
202              */
203         }
204     }
205 }
206 
207 class LT1Sub {
protectedSuperclassMethod()208     protected String protectedSuperclassMethod() {
209         return "I'm the sub";
210     }
211 }
212 
213 class LT1Thing {
214     final Object str;
215 
LT1Thing(Object s)216     LT1Thing(Object s) {
217         str = s;
218     }
219 }
220 
221 interface LT1SA {
doit(String s)222     Integer doit(String s);
223 }
224 
225 interface LT1IA {
doit(int i)226     void doit(int i);
227 }
228 
229 interface LT1IIA {
doit(Integer i)230     void doit(Integer i);
231 }
232 
233