-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add comments in RunnableDemo and create FileFilterTest
Lambda expressions in RunnableDemo were updated to include commentary for clarity. A new FileFilterTest.java file was added to the test directory, including various testing methods for file operations using different variants of lambda expressions.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package lambdas; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileFilter; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@SuppressWarnings({"Convert2MethodRef", "Convert2Lambda", "Anonymous2MethodRef"}) | ||
public class FileFilterTest { | ||
private final File root = new File("src/main/java"); | ||
|
||
@Test | ||
public void listFiles() { | ||
File[] files = root.listFiles(); | ||
assert files != null; | ||
assertEquals(22, files.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_anonInnerClass() { | ||
File[] directories = root.listFiles(new FileFilter() { | ||
@Override | ||
public boolean accept(File pathname) { | ||
return pathname.isDirectory(); | ||
} | ||
}); | ||
assert directories != null; | ||
assertEquals(14, directories.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_expressionLambda() { | ||
// Single argument to lambda, without data type --> no parens necessary | ||
File[] directories = root.listFiles(pathname -> pathname.isDirectory()); | ||
assert directories != null; | ||
assertEquals(14, directories.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_methodReference() { | ||
File[] directories = root.listFiles(File::isDirectory); | ||
assert directories != null; | ||
assertEquals(14, directories.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_blockLambda() { | ||
// Single argument to lambda, without data type --> no parens necessary | ||
File[] directories = root.listFiles(pathname -> { | ||
System.out.println("pathname = " + pathname); | ||
return pathname.isDirectory(); | ||
}); | ||
assert directories != null; | ||
assertEquals(14, directories.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_assignToVariable() { | ||
FileFilter filter = pathname -> pathname.isDirectory(); | ||
File[] directories = root.listFiles(filter); | ||
assert directories != null; | ||
assertEquals(14, directories.length); | ||
} | ||
|
||
@Test | ||
void listJavaSrcFiles_fileFilter() { | ||
File[] javaFiles = root.listFiles(pathname -> pathname.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); | ||
} | ||
|
||
} |