mcimadamore@1415: /* mcimadamore@1415: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. mcimadamore@1415: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@1415: * mcimadamore@1415: * This code is free software; you can redistribute it and/or modify it mcimadamore@1415: * under the terms of the GNU General Public License version 2 only, as mcimadamore@1415: * published by the Free Software Foundation. mcimadamore@1415: * mcimadamore@1415: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@1415: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@1415: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@1415: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@1415: * accompanied this code). mcimadamore@1415: * mcimadamore@1415: * You should have received a copy of the GNU General Public License version mcimadamore@1415: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@1415: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@1415: * mcimadamore@1415: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@1415: * or visit www.oracle.com if you need additional information or have any mcimadamore@1415: * questions. mcimadamore@1415: */ mcimadamore@1415: mcimadamore@1415: /** mcimadamore@1415: * @test mcimadamore@1415: * @bug 8003280 mcimadamore@1415: * @summary Add lambda tests mcimadamore@1415: * Test lambda expressions for existing SAM interfaces like Runnable and Comparator mcimadamore@1415: * @compile LambdaTest1.java mcimadamore@1415: * @run main LambdaTest1 mcimadamore@1415: */ mcimadamore@1415: mcimadamore@1415: import java.util.Collections; mcimadamore@1415: import java.util.List; mcimadamore@1415: import java.util.ArrayList; mcimadamore@1415: import java.util.Date; mcimadamore@1415: mcimadamore@1415: public class LambdaTest1 { mcimadamore@1415: mcimadamore@1415: private static String assertionStr = ""; mcimadamore@1415: mcimadamore@1415: private static void assertTrue(boolean b) { mcimadamore@1415: if(!b) mcimadamore@1415: throw new AssertionError(); mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: private static void test1(Runnable r) { mcimadamore@1415: r.run(); mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: void test2(Object o) { mcimadamore@1415: if(o instanceof Runnable) mcimadamore@1415: ((Runnable)o).run(); mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: Runnable test3() { mcimadamore@1415: return ()-> { assertionStr += "Runnable6"; }; mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: public static void main(String[] args) { mcimadamore@1415: mcimadamore@1415: //lambda expressions for SAM interface Runnable: mcimadamore@1415: //assign: mcimadamore@1415: Runnable r = ()-> { assertionStr += "Runnable1 "; }; mcimadamore@1415: r.run(); mcimadamore@1415: mcimadamore@1415: //cast: mcimadamore@1415: ((Runnable)()-> { assertionStr += "Runnable2 "; }).run(); mcimadamore@1415: mcimadamore@1415: Object o = (Runnable)()-> {}; mcimadamore@1415: mcimadamore@1415: o = (Runnable)()-> { mcimadamore@1415: switch (assertionStr) { mcimadamore@1415: case "Runnable1 Runnable2 ": mcimadamore@1415: assertionStr += "Runnable3 "; mcimadamore@1415: break; mcimadamore@1415: default: mcimadamore@1415: throw new AssertionError(); mcimadamore@1415: } mcimadamore@1415: return; mcimadamore@1415: }; mcimadamore@1415: mcimadamore@1415: //method parameter: mcimadamore@1415: test1(()-> { assertionStr += "Runnable4 "; return; }); mcimadamore@1415: mcimadamore@1415: LambdaTest1 test = new LambdaTest1(); mcimadamore@1415: test.test2((Runnable)()-> { assertionStr += "Runnable5 "; }); mcimadamore@1415: mcimadamore@1415: //return type: mcimadamore@1415: r = test.test3(); mcimadamore@1415: r.run(); mcimadamore@1415: mcimadamore@1415: assertTrue(assertionStr.equals("Runnable1 Runnable2 Runnable4 Runnable5 Runnable6")); mcimadamore@1415: mcimadamore@1415: //lambda expressions for SAM interface Comparator: mcimadamore@1415: List list = new ArrayList(); mcimadamore@1415: list.add(4); mcimadamore@1415: list.add(10); mcimadamore@1415: list.add(-5); mcimadamore@1415: list.add(100); mcimadamore@1415: list.add(9); mcimadamore@1415: Collections.sort(list, (Integer i1, Integer i2)-> i2 - i1); mcimadamore@1415: String result = ""; mcimadamore@1415: for(int i : list) mcimadamore@1415: result += i + " "; mcimadamore@1415: assertTrue(result.equals("100 10 9 4 -5 ")); mcimadamore@1415: mcimadamore@1415: Collections.sort(list, mcimadamore@1415: (i1, i2) -> { mcimadamore@1415: String s1 = i1.toString(); mcimadamore@1415: String s2 = i2.toString(); mcimadamore@1415: return s1.length() - s2.length(); mcimadamore@1415: }); mcimadamore@1415: result = ""; mcimadamore@1415: for(int i : list) mcimadamore@1415: result += i + " "; mcimadamore@1415: assertTrue(result.equals("9 4 10 -5 100 ")); mcimadamore@1415: } mcimadamore@1415: }