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

Tue, 12 Mar 2013 16:02:43 +0000

author
mcimadamore
date
Tue, 12 Mar 2013 16:02:43 +0000
changeset 1628
5ddecb91d843
parent 1415
01c9d4161882
child 2370
acd64168cf8b
permissions
-rw-r--r--

8009545: Graph inference: dependencies between inference variables should be set during incorporation
Summary: Move all transitivity checks into the incorporation round
Reviewed-by: jjg

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

mercurial