Skip to content

Commit

Permalink
Add UsePerson functionality and refactored existing code
Browse files Browse the repository at this point in the history
This commit modifies the UsePerson.java file to provide a more streamlined approach to converting names to objects. It modifies the main method to incorporate separate helper methods for different approaches: Java 7 convention, streaming, and constructor referencing. It also returns result lists or arrays instead of printing them.

Additionally, we introduce the new UsePersonTest.java file containing unit tests for the introduced methods in UsePerson.java file. It also tests for more advanced cases like usage of parallel streams and three-arg-collect handling.

Changes have also been made to the CompanyEmployee.java file to implement Company and Employee interfaces with a test case in CompanyEmployeeTest.java file.

In Person.java, added a new comment for improved readability of the code.

These new changes result in a reduction of clutter in the main method, and cultivate easier testing and verification of individual functionalities.
  • Loading branch information
kousen committed Sep 26, 2023
1 parent a58a3b2 commit 6fcfd4d
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 40 deletions.
32 changes: 32 additions & 0 deletions src/main/java/interfaces/CompanyEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 getName() {
return Employee.super.getName() + " works for " +
Company.super.getName();
}

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

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

@Override
public void doWork() {
System.out.println("Converting caffeine into code for $$...");
}
}
1 change: 1 addition & 0 deletions src/main/java/lambdas/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public Person(String name) {
this.name = name;
}

// copy constructor
public Person(Person other) {
this.name = other.name;
}
Expand Down
79 changes: 41 additions & 38 deletions src/main/java/lambdas/UsePerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,67 @@
import java.util.stream.Collectors;

public class UsePerson {
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Paul", "George", "Ringo");
private final List<String> names = Arrays.asList("John", "Paul", "George", "Ringo");
private final List<String> fullNames = Arrays.asList("John Lennon", "Paul McCartney",
"George Harrison", "Ringo Starr");

// Old-style way:
// Java 7 and earlier:
public List<Person> convertNamesToPeopleJava7() {
List<Person> beatles = new ArrayList<>(); // Shared mutable state
for (String name : names) {
beatles.add(new Person(name));
}
System.out.println(beatles);
return beatles;
}

List<Person> people = names.stream() // Stream<String>
// Use Java streams:
@SuppressWarnings("Convert2MethodRef")
public List<Person> convertNamesToPeopleStreams() {
return names.stream() // Stream<String>
.map(name -> new Person(name)) // Stream<Person>
.collect(Collectors.toList()); // Converts Stream<Person> to List<Person>
System.out.println(people);
}

people = names.stream()
.map(Person::new) // uses the Person(String) ctr
// .map(Person::new) // uses the Person(Person) ctr
public List<Person> convertNamesUsingCtorRef() {
return names.stream()
.map(Person::new)
.collect(Collectors.toList());
System.out.println(people);
}

Person[] peopleArray = names.stream()
public Person[] convertNamesToPersonArray() {
return names.stream()
.map(Person::new)
.toArray(Person[]::new);
//.toArray(value -> new Person[value]);
System.out.println(Arrays.toString(peopleArray));
}

List<String> fullNames = Arrays.asList(
"John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr");
people = fullNames.stream()
.map(name -> name.split(" "))
.map(Person::new) // use the Person(String...) ctr
public List<Person> convertFullNamesToPerson() {
return fullNames.stream() // Stream<String>
.map(name -> name.split(" ")) // Stream<String[]>
.map(Person::new) // calls the vararg constructor
.collect(Collectors.toList());
System.out.println(people);
System.out.println(people.getClass().getName());
}

// p1..p5 | p6..p10 | p11..p15 | p16..p20 // say you have 4 cores and run in parallel
// l1 l2 l3 l4
// list
LinkedList<Person> linkedPersons = names.stream()
// p1..p5 | p6..p10 | p11..p15 | p16..p20 // 20 persons, 4 cores, and run in parallel
// l1 l2 l3 l4
// list
@SuppressWarnings("Convert2MethodRef")
public List<Person> threeArgCollect() {
return names.parallelStream()
.peek(name -> System.out.printf("%s: %s%n", Thread.currentThread().getName(), name))
.map(Person::new)
.collect(
() -> new LinkedList<Person>(), // Supplier<LinkedList>
(list, person) -> list.add(person), // BiConsumer<LinkedList, Person>
(list1, list2) -> list1.addAll(list2)); // BiConsumer<LinkedList, LinkedList>
System.out.println(linkedPersons);
() -> new LinkedList<>(), // Supplier<ArrayList>
(list, person) -> list.add(person), // BiConsumer<ArrayList, Person>
(list1, list2) -> list1.addAll(list2)); // BiConsumer<ArrayList, ArrayList>
}

linkedPersons = names.stream()
public List<Person> threeArgCollectMethodRefs() {
return names.stream()
.peek(name -> System.out.printf("%s: %s%n", Thread.currentThread().getName(), name))
.map(Person::new)
.collect(
LinkedList::new, // Supplier<LinkedList>
LinkedList::add, // BiConsumer<LinkedList, Person>
LinkedList::addAll); // BiConsumer<LinkedList, LinkedList>
System.out.println(linkedPersons);

linkedPersons = names.stream()
.map(Person::new)
.collect(Collectors.toCollection(LinkedList::new));
System.out.println(linkedPersons);
LinkedList::new, // Supplier<ArrayList>
LinkedList::add, // BiConsumer<ArrayList, Person>
LinkedList::addAll); // BiConsumer<ArrayList, ArrayList>
}
}
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());
}
}
9 changes: 9 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void testListFiles() {
assertThat(files.length).isEqualTo(22);
}

@SuppressWarnings({"Convert2Lambda", "Anonymous2MethodRef"})
@Test
void listDirectories_anonInnerClass() {
File[] directories = root.listFiles(new FileFilter() {
Expand All @@ -33,13 +34,21 @@ public boolean accept(File file) {
assertThat(directories.length).isEqualTo(14);
}

@SuppressWarnings("Convert2MethodRef")
@Test
void listDirectories_expressionLambda() {
File[] directories = root.listFiles(file -> file.isDirectory());
assert directories != null;
assertThat(directories.length).isEqualTo(14);
}

@Test
void listDirectories_methodReference() {
File[] directories = root.listFiles(File::isDirectory);
assert directories != null;
assertThat(directories.length).isEqualTo(14);
}

@Test
void listDirectories_blockLambda() {
File[] directories = root.listFiles(file -> {
Expand Down
82 changes: 82 additions & 0 deletions src/test/java/lambdas/UsePersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package lambdas;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class UsePersonTest {
private final UsePerson usePerson = new UsePerson();

@Test
void convertNamesToPeople_java7andEarlier() {
List<Person> beatles = usePerson.convertNamesToPeopleJava7();
assertThat(beatles).contains(
new Person("John"),
new Person("Paul"),
new Person("George"),
new Person("Ringo"));
}

@Test
void convertNamesToPeople_streams() {
List<Person> beatles = usePerson.convertNamesToPeopleStreams();
assertThat(beatles).contains(
new Person("John"),
new Person("Paul"),
new Person("George"),
new Person("Ringo"));
}

@Test
void convertNamesToPeople_ctrRef() {
List<Person> beatles = usePerson.convertNamesUsingCtorRef();
assertThat(beatles).contains(
new Person("John"),
new Person("Paul"),
new Person("George"),
new Person("Ringo"));
}

@Test
void convertNamesToPersonArray() {
Person[] beatles = usePerson.convertNamesToPersonArray();
assertThat(beatles).contains(
new Person("John"),
new Person("Paul"),
new Person("George"),
new Person("Ringo"));
}

@Test
void convertFullNamesToPeople() {
List<Person> beatles = usePerson.convertFullNamesToPerson();
assertThat(beatles).contains(
new Person("John", "Lennon"),
new Person("Paul", "McCartney"),
new Person("George", "Harrison"),
new Person("Ringo", "Starr"));
}

@Test
void threeArgCollect() {
List<Person> beatles = usePerson.threeArgCollect();
assertThat(beatles).contains(
new Person("John"),
new Person("Paul"),
new Person("George"),
new Person("Ringo"));
}

@Test
void threeArgCollect_methodRefs() {
List<Person> beatles = usePerson.threeArgCollectMethodRefs();
assertThat(beatles).contains(
new Person("John"),
new Person("Paul"),
new Person("George"),
new Person("Ringo"));
}

}

0 comments on commit 6fcfd4d

Please sign in to comment.