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

Sat, 17 Nov 2012 19:01:03 +0000

author
mcimadamore
date
Sat, 17 Nov 2012 19:01:03 +0000
changeset 1415
01c9d4161882
child 2370
acd64168cf8b
permissions
-rw-r--r--

8003280: Add lambda tests
Summary: Turn on lambda expression, method reference and default method support
Reviewed-by: jjg

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

mercurial