Skip to content

Commit

Permalink
Update Person class with variant constructors; add LazyErrorMessage t…
Browse files Browse the repository at this point in the history
…est class

Multiple constructors were added to the Person class to enhance flexibility and allow for different initialization methods. Comments were added to describe each constructor's usage - default, single-arg, copy, and varargs. Additionally, a new test class, 'LazyErrorMessage.java', was introduced under the 'lambdas' package. It contains tests to underline the differences in producing eager and lazy error messages, thereby improving our understanding of the concept.
  • Loading branch information
kousen committed Nov 8, 2023
1 parent c36d920 commit 6d3f325
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main/java/lambdas/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
public class Person {
private String name;

// Default constructor
public Person() {}

// Single-arg constructor
public Person(String name) {
this.name = name;
}

// Copy constructor
public Person(Person other) {
this.name = other.name;
}

// Varargs constructor
public Person(String... names) {
this.name = String.join(" ", names);
}
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

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

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

@SuppressWarnings({"Convert2MethodRef", "Convert2Lambda", "Anonymous2MethodRef"})
public class FileFilterTest {
private final File root = new File("src/main/java");

Expand Down Expand Up @@ -56,7 +57,7 @@ void listDirectories_methodReference() {

@Test
void listDirectories_assignToVariable() {
FileFilter filter = path -> path.isDirectory();
FileFilter filter = File::isDirectory;
File[] directories = root.listFiles(filter);
assert directories != null;
assertEquals(14, directories.length);
Expand All @@ -75,4 +76,11 @@ void listJavaSrcFiles_fileNameFilter() {
assert javaFiles != null;
assertEquals(8, javaFiles.length);
}

@Test
void forEach_list() {
List<String> strings = List.of("one", "two", "three");
strings.forEach(s -> System.out.println(s));
strings.forEach(System.out::println);
}
}
31 changes: 31 additions & 0 deletions src/test/java/lambdas/LazyErrorMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lambdas;

import org.junit.jupiter.api.Test;

import java.util.logging.Logger;

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

@SuppressWarnings("Convert2MethodRef")
public class LazyErrorMessage {
private final Logger logger = Logger.getLogger(LazyErrorMessage.class.getName());

private String getErrorMessage() {
System.out.println("Calculating error message...");
return "This is the error message";
}

@Test
void eagerErrorMessage() {
boolean x = true;
logger.fine(getErrorMessage());
assertTrue(x, getErrorMessage()); // eagerly creates the error message
}

@Test
void lazyErrorMessage() {
boolean x = true;
logger.fine(() -> getErrorMessage()); // Supplier<String>
assertTrue(x, () -> getErrorMessage()); // does NOT create the error message
}
}

0 comments on commit 6d3f325

Please sign in to comment.