1/* 2 * Copyright (c) 2011, 2015, 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/** 25 * @test 26 * @bug 8003280 27 * @summary Add lambda tests 28 * This test is for identifying SAM types 2 and 3, see Helper.java for SAM types 29 * @modules java.sql 30 * @compile LambdaTest2_SAM1.java Helper.java 31 * @run main LambdaTest2_SAM1 32 */ 33 34import java.util.Collections; 35import java.util.List; 36import java.util.ArrayList; 37import java.io.*; 38 39public class LambdaTest2_SAM1 { 40 private static List<String> strs = new ArrayList<String>(); 41 private static List<File> files = new ArrayList<File>(); 42 43 public static void main(String[] args) { 44 strs.add("copy"); 45 strs.add("paste"); 46 strs.add("delete"); 47 strs.add("rename"); 48 49 files.add(new File("a.txt")); 50 files.add(new File("c.txt")); 51 files.add(new File("b.txt")); 52 53 //type #2: Comparator<T> 54 Collections.sort(files, (File f1, File f2) -> f1.getName().compareTo(f2.getName())); 55 for(File f : files) 56 System.out.println(f.getName()); 57 System.out.println(); 58 Collections.sort(files, (File f1, File f2) -> (int)(f1.length() - f2.length())); 59 for(File f : files) 60 System.out.println(f.getName() + " " + f.length()); 61 System.out.println(); 62 63 LambdaTest2_SAM1 test = new LambdaTest2_SAM1(); 64 65 //type #2: 66 test.methodMars((File f) -> { 67 System.out.println("implementing Mars<File>.getAge(File f)..."); 68 return (int)f.length(); 69 }); 70 test.methodJupiter((int n) -> n+1); 71 72 //type #3: 73 test.methodXY((List<String> strList) -> strList.size() ); 74 test.methodXYZ((List<String> strList) -> 20 ); 75 } 76 77 //type #2: 78 void methodMars(Mars<File> m) { 79 System.out.println("methodMars(): SAM type interface Mars object instantiated: " + m); 80 System.out.println(m.getAge(new File("a.txt"))); 81 } 82 83 //type #2: 84 void methodJupiter(Jupiter j) { 85 System.out.println("methodJupiter(): SAM type interface Jupiter object instantiated: " + j); 86 System.out.println(j.increment(33)); 87 } 88 89 //type #3: 90 void methodXY(XY xy) { 91 System.out.println("methodXY(): SAM type interface XY object instantiated: " + xy); 92 System.out.println(xy.getTotal(strs)); 93 } 94 95 //type #3: 96 void methodXYZ(XYZ xyz) { 97 System.out.println("methodXYZ(): SAM type interface XYZ object instantiated: " + xyz); 98 System.out.println(xyz.getTotal(strs)); 99 } 100} 101