001package edu.pdx.cs410J.java8;
002
003import java.io.File;
004import java.util.Arrays;
005
006/**
007 * This program demonstrates how Lambdas in Java 8 greatly simplify using
008 * "functional interfaces" like <code>FileFilter</code> and
009 * <code>FileNameFilter</code>.
010 *
011 * @since Summer 2014
012 */
013public class FindJavaFilesUsingJava8 {
014
015  /**
016   * Prints out the names all of the Java source files in a directory
017   * and then recurses over subdirectories.
018   */
019  private static void findJavaFiles(File directory) {
020    File[] javaFiles = directory.listFiles((dir, name) -> name.endsWith(".java"));
021    Arrays.asList(javaFiles).forEach(System.out::println);
022
023    File[] subdirectories = directory.listFiles(File::isDirectory);
024    Arrays.asList(subdirectories).forEach(FindJavaFilesUsingJava8::findJavaFiles);
025  }
026
027  /**
028   * The one command line parameter is the directory in which to start
029   * the search.
030   */
031  public static void main(String[] args) {
032    File file = new File(args[0]);
033    if (file.isDirectory()) {
034      // Look for Java files
035      findJavaFiles(file);
036
037    } else {
038      System.err.println(file + " is not a directory");
039    }
040  }
041
042}