-
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 Record class and FileFilter Tests; Update UsePerson
Introduced a Java record, PersonRecord, in the Person.java file to provide an immutable data holder functionality. Inclusion of 'record class' also brings auto-generation of equals(), hashCode() and toString() methods. Further, added FileFilter Tests in FileFilterTest.java, ensuring that different filtering scenarios work as expected. Modifications were also made to UsePerson.java file to improve code readability and performance via using parallel streams and the addition of useful comments for better understanding of the code.
- Loading branch information
Showing
3 changed files
with
105 additions
and
8 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
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 java.io.FilenameFilter; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class FileFilterTest { | ||
|
||
private final File root = new File("src/main/java"); | ||
|
||
@Test | ||
void listFiles() { | ||
File[] files = root.listFiles(); | ||
assert files != null; | ||
assertEquals(22, files.length); | ||
} | ||
|
||
@SuppressWarnings({"Convert2Lambda", "Anonymous2MethodRef"}) | ||
@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); | ||
} | ||
|
||
@SuppressWarnings("Convert2MethodRef") | ||
@Test | ||
void listDirectories_expressionLambda() { | ||
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() { | ||
File[] directories = root.listFiles(pathname -> { | ||
System.out.println("Checking " + pathname); | ||
return pathname.isDirectory(); | ||
}); | ||
assert directories != null; | ||
assertEquals(14, directories.length); | ||
} | ||
|
||
@Test | ||
void listJavaSrcFiles_fileFilter() { | ||
File[] javaSrcFiles = root.listFiles(pathname -> pathname.getName().endsWith(".java")); | ||
assert javaSrcFiles != null; | ||
assertEquals(8, javaSrcFiles.length); | ||
} | ||
|
||
@Test | ||
void listJavaSrcFiles_fileNameFilter() { | ||
File[] javaSrcFiles = root.listFiles((dir, name) -> name.endsWith(".java")); | ||
assert javaSrcFiles != null; | ||
assertEquals(8, javaSrcFiles.length); | ||
} | ||
|
||
@Test | ||
void listJavaSrcFiles_assignToVariable() { | ||
FilenameFilter filter = (File dir, String name) -> name.endsWith(".java"); | ||
File[] javaSrcFiles = root.listFiles(filter); | ||
assert javaSrcFiles != null; | ||
assertEquals(8, javaSrcFiles.length); | ||
} | ||
} |