Skip to content

Commit

Permalink
Add FileFilter tests and simplify stream collection usage
Browse files Browse the repository at this point in the history
Introduce new unit tests in `FileFilterTest.java` to demonstrate various file filtering and iteration approaches using lambdas and method references. Additionally, refactor `AntarcticaTimeZones.java` to use the simplified `toList()` method for stream collection, enhancing code readability.
  • Loading branch information
kousen committed Dec 11, 2024
1 parent f44bab8 commit 1c70e0f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/datetime/AntarcticaTimeZones.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void main(String[] args) {
.map(now::atZone) // Stream<ZonedDateTime>
.sorted(comparingInt(
zoneId -> zoneId.getOffset().getTotalSeconds()))
.collect(Collectors.toList());
.toList();

antarcticZones.forEach(zdt ->
System.out.printf("UTC%6s: %25s %7s%n", zdt.getOffset(), zdt.getZone(),
Expand Down
79 changes: 79 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package lambdas;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileFilter;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class FileFilterTest {
private final File root = new File("src");

@Test
void listFiles() {
File[] files = root.listFiles();
assert files != null;
for (File file : files) {
System.out.println(file);
}
}

@Test
void listDirectories_fileFilter_anoninnerclass() {
File[] files = root.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
assert files != null;
for (File file : files) {
System.out.println(file);
}
}

@Test
void listDirectories_fileFilter_expressionlambda() {
File[] files = root.listFiles(pathname -> pathname.isDirectory());
assert files != null;
assertEquals(2, files.length);
}

@Test
void listDirectories_fileNameFilter_expressionlambda() {
File[] files = root.listFiles((dir, name) -> new File(dir, name).isDirectory());
assert files != null;
assertEquals(2, files.length);
}

@Test
void listDirectories_fileFilter_variable() {
FileFilter fileFilter = (File pathname) -> pathname.isDirectory();
File[] files = root.listFiles(fileFilter);
assert files != null;
assertEquals(2, files.length);
}

@Test
void listDirectories_fileFilter_methodReference() {
File[] files = root.listFiles(File::isDirectory);
assert files != null;
assertEquals(2, files.length);
}

@Test
void iterateOverMap() {
Map.of("a", 1, "b", 2, "c", 2)
.forEach((k, v) -> System.out.println(k + " -> " + v));
}

@Test
void iterateOverList() {
List.of("this", "is", "a", "list")
.forEach(System.out::println);

}
}

0 comments on commit 1c70e0f

Please sign in to comment.