Skip to content

Commit

Permalink
Add comments in RunnableDemo and create FileFilterTest
Browse files Browse the repository at this point in the history
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
kousen committed Dec 11, 2023
1 parent b756cd7 commit d61898d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/lambdas/RunnableDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.concurrent.Executors;

public class RunnableDemo {
@SuppressWarnings("Convert2Lambda")
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(4);

Expand All @@ -15,8 +16,10 @@ public void run() {
}
});

// Expression lambda
executorService.submit(() -> System.out.println("Inside expression lambda"));

// Block lambda
executorService.submit(() -> {
System.out.println(Thread.currentThread().getName());
System.out.println("Inside block lambda");
Expand Down
81 changes: 81 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
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);
}

}

0 comments on commit d61898d

Please sign in to comment.