test/tools/javac/lambda/lambdaExpression/LambdaTest1.java

Tue, 22 Apr 2014 17:55:22 +0100

author
vromero
date
Tue, 22 Apr 2014 17:55:22 +0100
changeset 2370
acd64168cf8b
parent 1415
01c9d4161882
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029718: Should always use lambda body structure to disambiguate overload resolution
Reviewed-by: dlsmith, jjg, jlahoda

mcimadamore@1415 1 /*
vromero@2370 2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1415 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1415 4 *
mcimadamore@1415 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1415 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1415 7 * published by the Free Software Foundation.
mcimadamore@1415 8 *
mcimadamore@1415 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1415 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1415 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1415 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1415 13 * accompanied this code).
mcimadamore@1415 14 *
mcimadamore@1415 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1415 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1415 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1415 18 *
mcimadamore@1415 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1415 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1415 21 * questions.
mcimadamore@1415 22 */
mcimadamore@1415 23
mcimadamore@1415 24 /**
mcimadamore@1415 25 * @test
mcimadamore@1415 26 * @bug 8003280
mcimadamore@1415 27 * @summary Add lambda tests
mcimadamore@1415 28 * Test lambda expressions for existing SAM interfaces like Runnable and Comparator<T>
mcimadamore@1415 29 * @compile LambdaTest1.java
mcimadamore@1415 30 * @run main LambdaTest1
mcimadamore@1415 31 */
mcimadamore@1415 32
mcimadamore@1415 33 import java.util.Collections;
mcimadamore@1415 34 import java.util.List;
mcimadamore@1415 35 import java.util.ArrayList;
mcimadamore@1415 36
mcimadamore@1415 37 public class LambdaTest1 {
mcimadamore@1415 38
mcimadamore@1415 39 private static String assertionStr = "";
mcimadamore@1415 40
mcimadamore@1415 41 private static void assertTrue(boolean b) {
mcimadamore@1415 42 if(!b)
mcimadamore@1415 43 throw new AssertionError();
mcimadamore@1415 44 }
mcimadamore@1415 45
mcimadamore@1415 46 private static void test1(Runnable r) {
mcimadamore@1415 47 r.run();
mcimadamore@1415 48 }
mcimadamore@1415 49
mcimadamore@1415 50 void test2(Object o) {
mcimadamore@1415 51 if(o instanceof Runnable)
mcimadamore@1415 52 ((Runnable)o).run();
mcimadamore@1415 53 }
mcimadamore@1415 54
mcimadamore@1415 55 Runnable test3() {
mcimadamore@1415 56 return ()-> { assertionStr += "Runnable6"; };
mcimadamore@1415 57 }
mcimadamore@1415 58
mcimadamore@1415 59 public static void main(String[] args) {
mcimadamore@1415 60
mcimadamore@1415 61 //lambda expressions for SAM interface Runnable:
mcimadamore@1415 62 //assign:
mcimadamore@1415 63 Runnable r = ()-> { assertionStr += "Runnable1 "; };
mcimadamore@1415 64 r.run();
mcimadamore@1415 65
mcimadamore@1415 66 //cast:
mcimadamore@1415 67 ((Runnable)()-> { assertionStr += "Runnable2 "; }).run();
mcimadamore@1415 68
mcimadamore@1415 69 Object o = (Runnable)()-> {};
mcimadamore@1415 70
mcimadamore@1415 71 o = (Runnable)()-> {
mcimadamore@1415 72 switch (assertionStr) {
mcimadamore@1415 73 case "Runnable1 Runnable2 ":
mcimadamore@1415 74 assertionStr += "Runnable3 ";
mcimadamore@1415 75 break;
mcimadamore@1415 76 default:
mcimadamore@1415 77 throw new AssertionError();
mcimadamore@1415 78 }
mcimadamore@1415 79 return;
mcimadamore@1415 80 };
mcimadamore@1415 81
mcimadamore@1415 82 //method parameter:
mcimadamore@1415 83 test1(()-> { assertionStr += "Runnable4 "; return; });
mcimadamore@1415 84
mcimadamore@1415 85 LambdaTest1 test = new LambdaTest1();
mcimadamore@1415 86 test.test2((Runnable)()-> { assertionStr += "Runnable5 "; });
mcimadamore@1415 87
mcimadamore@1415 88 //return type:
mcimadamore@1415 89 r = test.test3();
mcimadamore@1415 90 r.run();
mcimadamore@1415 91
mcimadamore@1415 92 assertTrue(assertionStr.equals("Runnable1 Runnable2 Runnable4 Runnable5 Runnable6"));
mcimadamore@1415 93
mcimadamore@1415 94 //lambda expressions for SAM interface Comparator<T>:
mcimadamore@1415 95 List<Integer> list = new ArrayList<Integer>();
mcimadamore@1415 96 list.add(4);
mcimadamore@1415 97 list.add(10);
mcimadamore@1415 98 list.add(-5);
mcimadamore@1415 99 list.add(100);
mcimadamore@1415 100 list.add(9);
mcimadamore@1415 101 Collections.sort(list, (Integer i1, Integer i2)-> i2 - i1);
mcimadamore@1415 102 String result = "";
mcimadamore@1415 103 for(int i : list)
mcimadamore@1415 104 result += i + " ";
mcimadamore@1415 105 assertTrue(result.equals("100 10 9 4 -5 "));
mcimadamore@1415 106
mcimadamore@1415 107 Collections.sort(list,
mcimadamore@1415 108 (i1, i2) -> {
mcimadamore@1415 109 String s1 = i1.toString();
mcimadamore@1415 110 String s2 = i2.toString();
mcimadamore@1415 111 return s1.length() - s2.length();
mcimadamore@1415 112 });
mcimadamore@1415 113 result = "";
mcimadamore@1415 114 for(int i : list)
mcimadamore@1415 115 result += i + " ";
mcimadamore@1415 116 assertTrue(result.equals("9 4 10 -5 100 "));
mcimadamore@1415 117 }
mcimadamore@1415 118 }

mercurial