diff --git a/src/test/java/lambdas/FileFilterTest.java b/src/test/java/lambdas/FileFilterTest.java index 489b4dc..76a6c88 100644 --- a/src/test/java/lambdas/FileFilterTest.java +++ b/src/test/java/lambdas/FileFilterTest.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileFilter; +import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -17,6 +18,7 @@ public void testListFiles() { assertEquals(22, files.length); } + @SuppressWarnings({"Convert2Lambda", "Anonymous2MethodRef"}) @Test void listDirectories_anonInnerClass() { File[] directories = root.listFiles(new FileFilter() { @@ -29,6 +31,7 @@ public boolean accept(File pathname) { assertEquals(14, directories.length); } + @SuppressWarnings("Convert2MethodRef") @Test void listDirectories_expression_lambda() { File[] directories = root.listFiles(file -> file.isDirectory()); @@ -36,5 +39,59 @@ void listDirectories_expression_lambda() { assertEquals(14, directories.length); } + @SuppressWarnings("Convert2MethodRef") + @Test + void listDirectories_lambda_variable() { + FileFilter fileFilter = file -> file.isDirectory(); + File[] directories = root.listFiles(fileFilter); + assert directories != null; + assertEquals(14, directories.length); + } + + @SuppressWarnings({"Convert2MethodRef", "CodeBlock2Expr"}) + @Test + void listDirectories_block_lambda() { + File[] directories = root.listFiles(file -> { + return file.isDirectory(); + }); + assert directories != null; + assertEquals(14, directories.length); + } + + @Test + void listDirectories_method_reference() { + File[] directories = root.listFiles(File::isDirectory); + assert directories != null; + assertEquals(14, directories.length); + } + + @Test + void listJavaSrcFiles_fileFilter() { + File[] javaFiles = root.listFiles(file -> file.getName().endsWith(".java")); + assert javaFiles != null; + assertEquals(8, javaFiles.length); + } + + @Test + void listJavaSrcFiles_fileNameFilter() { + File[] javaFiles = root.listFiles((dir, name) -> name.endsWith(".java")); + assert javaFiles != null; + assertEquals(8, javaFiles.length); + } + + @Test + void forEach_on_a_list() { + List strings = List.of("this", "is", "a", "list"); + // Java 5+ + for (String string : strings) { + System.out.println(string); + } + + // Java 8+ + strings.forEach(System.out::println); + + // Parallel stream + strings.parallelStream().forEach(System.out::println); + } }