Skip to content

Commit

Permalink
Implement logging and enhance error handling
Browse files Browse the repository at this point in the history
The code was updated to include a more refined approach to error management through the implementation of logging in the LazyErrorMessageTest and ProcessDictionary classes. Additionally, the way files are processed was changed to throw unchecked exceptions instead of merely printing stack traces. A new test file, MapTest, was created to test iteration over a map.
  • Loading branch information
kousen committed May 18, 2024
1 parent 1af8ec8 commit 1eb801d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/io/ProcessDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -16,6 +18,8 @@

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

private final Path dictionary = Paths.get("/usr/share/dict/words");

public long getMaxLength() {
Expand All @@ -28,16 +32,17 @@ public long getMaxLength() {

public void printTenLongestWords() {
System.out.println("\nTen Longest Words:");
long max = getMaxLength() - 5;
try (Stream<String> words = Files.lines(dictionary)) {
words.filter(s -> s.length() > 20)
words.filter(s -> s.length() > max)
.sorted(Comparator.comparingInt(String::length).reversed()
//.thenComparing(Comparator.reverseOrder()))
)
.limit(10)
.forEach(w ->
System.out.printf("%s (%d)%n", w, w.length()));
} catch (IOException e) {
e.printStackTrace();
logger.warning(() -> e.getMessage());
}
}

Expand All @@ -48,7 +53,7 @@ public void printWordsOfEachLength() {
.collect(Collectors.groupingBy(String::length)) // Map<Integer,List<String>>
.forEach((len, wordList) -> System.out.println(len + ": " + wordList));
} catch (IOException e) {
e.printStackTrace();
throw new UncheckedIOException(e);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/java/lambdas/LazyErrorMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

import org.junit.jupiter.api.Test;

import java.util.logging.Logger;

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

public class LazyErrorMessageTest {
private final Logger logger = Logger.getLogger(LazyErrorMessageTest.class.getName());

private String getLogMessage() {
System.out.println("Generating log message...");
return "x should be true";
}

private String getErrorMessage() {
System.out.println("Generating error message...");
Expand All @@ -15,11 +23,14 @@ private String getErrorMessage() {
void assertArgIsTrue() {
boolean x = true;
assertTrue(x, getErrorMessage()); // 2nd arg is a String
logger.fine(getLogMessage());
}

@Test
void assertArgIsTrue_lazyErrorMessage() {
boolean x = true;
assertTrue(x, () -> getErrorMessage()); // 2nd arg is a Supplier<String>
assertTrue(x, this::getErrorMessage); // 2nd arg is a Supplier<String>
logger.fine(() -> getLogMessage());
}
}
14 changes: 14 additions & 0 deletions src/test/java/lambdas/MapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lambdas;

import org.junit.jupiter.api.Test;

import java.util.Map;

public class MapTest {

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

0 comments on commit 1eb801d

Please sign in to comment.