-
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.
Update Person class with variant constructors; add LazyErrorMessage t…
…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
Showing
3 changed files
with
45 additions
and
2 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,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 | ||
} | ||
} |