Skip to content

Commit

Permalink
Refactor imports and enhance logging and error handling
Browse files Browse the repository at this point in the history
Consolidate imports in UsePerson and refactor equals method in Person class. Add comprehensive logging and specific error handling in LazyStreams to manage exceptional cases. Introduce new CompanyEmployee class implementing multiple interfaces and update relevant test cases.
  • Loading branch information
kousen committed Aug 21, 2024
1 parent 8dc0e97 commit 9628360
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
31 changes: 31 additions & 0 deletions src/main/java/interfaces/CompanyEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package interfaces;

public class CompanyEmployee implements Company, Employee {
private final String first;
private final String last;

public CompanyEmployee(String first, String last) {
this.first = first;
this.last = last;
}

@Override
public String getFirst() {
return first;
}

@Override
public String getLast() {
return last;
}

@Override
public void doWork() {
System.out.println("Converting caffeine into code for $$$.");
}

@Override
public String getName() {
return Employee.super.getName() + " works for " + Company.super.getName();
}
}
4 changes: 1 addition & 3 deletions src/main/java/lambdas/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public void setName(String name) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;

Person person = (Person) o;
if (!(o instanceof Person person)) return false;

return Objects.equals(name, person.name);
}
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/lambdas/UsePerson.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package lambdas;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

public class UsePerson {
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Paul", "George", "Ringo");
List<String> names = List.of("John", "Paul", "George", "Ringo");

// Old-style way:
List<Person> beatles = new ArrayList<>(); // Shared mutable state
Expand All @@ -24,10 +21,19 @@ public static void main(String[] args) {

people = names.stream()
.map(Person::new) // uses the Person(String) ctr
// .map(Person::new) // uses the Person(Person) ctr
//.map(Person::new) // uses the Person(Person) ctr
.collect(Collectors.toList());
System.out.println(people);

Optional<Person> first = names.stream()
.peek(System.out::println)
.filter(name -> name.startsWith("P"))
.peek(System.out::println)
.map(Person::new)
.peek(System.out::println)
.findFirst(); // short-circuiting, terminal operation; returns Optional<Person>
System.out.println(first);

Person[] peopleArray = names.stream()
.map(Person::new)
.toArray(Person[]::new);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/lazy/LazyStreams.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import java.util.stream.IntStream;

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

public static int multByTwo(int n) {
System.out.printf("Inside multByTwo with arg %d on thread %s%n",
n, Thread.currentThread().getName());
if (n == 101) try {
throw new Exception("101 is a bad number");
} catch (Exception e) {
throw new RuntimeException(e);
}
return n * 2;
}

Expand All @@ -30,8 +33,8 @@ public static void main(String[] args) {
// Demonstrate laziness using print statements
firstEvenDoubleDivBy3 = IntStream.rangeClosed(100, 2_000_000)
// .parallel()
.map(n -> multByTwo(n))
.filter(LazyStreams::modByThree)
.map(LazyStreams::multByTwo)
.findFirst().orElse(0);
System.out.printf("First even divisible by 3 is %d%n", firstEvenDoubleDivBy3);
}
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/interfaces/CompanyEmployeeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.jupiter.api.Test;

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

// Create a class called CompanyEmployee that implements both
// the Company and Employee interfaces
// Implement the necessary methods
Expand All @@ -11,7 +13,7 @@ public class CompanyEmployeeTest {

@Test
public void getName() {
// CompanyEmployee emp = new CompanyEmployee("Peter", "Gibbons");
// assertEquals("Peter Gibbons works for Initech", emp.getName());
CompanyEmployee emp = new CompanyEmployee("Peter", "Gibbons");
assertEquals("Peter Gibbons works for Initech", emp.getName());
}
}
12 changes: 11 additions & 1 deletion src/test/java/lambdas/LazyErrorMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@

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 getErrorMessage() {
System.out.println("Generating error message...");
return "x should be true";
}

@Test
private String getLogMessage() {
System.out.println("Generating log message...");
return "Here is my log message";
}

@Test // "Eager"
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>
logger.fine(() -> getLogMessage());
}
}

0 comments on commit 9628360

Please sign in to comment.