1 /*
   2  * Copyright (c) 2020, 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 import java.util.Arrays;
  26 import java.util.List;
  27 import java.util.function.Predicate;
  28 
  29 public class PredicateApp {
  30     public static void main(String[] args){
  31         boolean isRuntime = (args.length == 1 && args[0].equals("run")) ? true : false;
  32         List languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
  33 
  34         doit(() -> {
  35             System.out.println("Languages which starts with J :");
  36             filter(languages, (str)->((String)str).startsWith("J"));
  37             LambdaVerification.verifyCallerIsArchivedLambda(isRuntime);
  38         });
  39 
  40         doit(() -> {
  41             System.out.println("Languages which ends with a ");
  42             filter(languages, (str)->((String)str).endsWith("a"));
  43             LambdaVerification.verifyCallerIsArchivedLambda(isRuntime);
  44         });
  45 
  46         doit(() -> {
  47             System.out.println("Print all languages :");
  48             filter(languages, (str)->true);
  49             LambdaVerification.verifyCallerIsArchivedLambda(isRuntime);
  50         });
  51 
  52         doit(() -> {
  53             System.out.println("Print no language : ");
  54             filter(languages, (str)->false);
  55             LambdaVerification.verifyCallerIsArchivedLambda(isRuntime);
  56         });
  57 
  58         doit(() -> {
  59             System.out.println("Print language whose length greater than 4:");
  60             filter(languages, (str)->((String)str).length() > 4);
  61             LambdaVerification.verifyCallerIsArchivedLambda(isRuntime);
  62         });
  63     }
  64 
  65     public static void filter(List names, Predicate condition) {
  66         names.stream().filter((name) -> (condition.test(name))).forEach((name) -> {
  67            System.out.println(name + " ");
  68        });
  69     }
  70 
  71     static void doit(Runnable t) {
  72         t.run();
  73     }
  74 }