Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework.streams.part1.exercise1(guyo) #395

Open
wants to merge 2 commits into
base: guyo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/main/java/lambda/data/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lambda.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Employee {

private final Person person;
private final List<JobHistoryEntry> jobHistory;

public Employee(Person person, List<JobHistoryEntry> jobHistory) {
this.person = person;
this.jobHistory = new ArrayList<>(jobHistory);
}

public Person getPerson() {
return person;
}

public List<JobHistoryEntry> getJobHistory() {
return new ArrayList<>(jobHistory);
}

@Override
public String toString() {
return "Employee@" + hashCode() + " {"
+ "person=" + person + ", "
+ "jobHistory=" + jobHistory + "}";
}

@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
Employee employee = (Employee) object;
return Objects.equals(person, employee.person)
&& Objects.equals(jobHistory, employee.jobHistory);
}

@Override
public int hashCode() {
return Objects.hash(person, jobHistory);
}
}
55 changes: 55 additions & 0 deletions src/main/java/lambda/data/JobHistoryEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package lambda.data;

import java.util.Objects;

public class JobHistoryEntry {

private final String employer;
private final String position;
private final int duration;

public JobHistoryEntry(int duration, String position, String employer) {
this.duration = duration;
this.position = position;
this.employer = employer;
}

public int getDuration() {
return duration;
}

public String getPosition() {
return position;
}

public String getEmployer() {
return employer;
}

@Override
public String toString() {
return "JobHistoryEntry@" + hashCode() + ": {"
+ "duration=" + duration + ", "
+ "position='" + position + "\', "
+ "employer='" + employer + "\'}";
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
JobHistoryEntry entry = (JobHistoryEntry) other;
return duration == entry.duration
&& Objects.equals(position, entry.position)
&& Objects.equals(employer, entry.employer);
}

@Override
public int hashCode() {
return Objects.hash(duration, position, employer);
}
}
55 changes: 55 additions & 0 deletions src/main/java/lambda/data/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package lambda.data;

import java.util.Objects;

public class Person {

private final String firstName;
private final String lastName;
private final int age;

public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

public String getFirstName(Person this) {
return firstName;
}

public String getLastName() {
return lastName;
}

public int getAge() {
return age;
}

public String getFullName() {
return firstName + " " + lastName;
}

@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
Person person = (Person) other;
return age == person.age
&& Objects.equals(firstName, person.firstName)
&& Objects.equals(lastName, person.lastName);
}

@Override
public int hashCode() {
return Objects.hash(age, firstName, lastName);
}

@Override
public String toString() {
return "Person@" + hashCode() + ": {"
+ "firstName='" + firstName + "', "
+ "lastName='" + lastName + "', "
+ "age=" + age + "}";
}
}
127 changes: 127 additions & 0 deletions src/test/java/streams/part1/exercise/Exercise1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package streams.part1.exercise;

import lambda.data.Employee;
import lambda.data.JobHistoryEntry;
import lambda.data.Person;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;

@SuppressWarnings({"ConstantConditions", "unused"})
class Exercise1 {

@Test
void findPersonsEverWorkedInEpam() {
List<Employee> employees = getEmployees();

// TODO реализация, использовать Collectors.toList()
List<Person> personsEverWorkedInEpam = employees.stream()
.filter(employee -> employee.getJobHistory().stream()
.anyMatch(jobHistoryEntry -> "EPAM".equals(jobHistoryEntry.getEmployer())))
.map(Employee::getPerson)
.collect(Collectors.toList());

assertThat(personsEverWorkedInEpam, contains(
employees.get(0).getPerson(),
employees.get(1).getPerson(),
employees.get(4).getPerson(),
employees.get(5).getPerson())
);
}

@Test
void findPersonsBeganCareerInEpam() {
List<Employee> employees = getEmployees();

// TODO реализация, использовать Collectors.toList()
List<Person> startedFromEpam = employees.stream()
.filter(employee -> "EPAM".equals(employee.getJobHistory().stream()
.map(JobHistoryEntry::getEmployer)
.findFirst()
.orElse("nothing")))
.map(Employee::getPerson)
.collect(Collectors.toList());

assertThat(startedFromEpam, contains(
employees.get(0).getPerson(),
employees.get(1).getPerson(),
employees.get(4).getPerson()
));
}

@Test
void findAllCompanies() {
List<Employee> employees = getEmployees();

// TODO реализация, использовать Collectors.toSet()
Set<String> companies = employees.stream()
.flatMap(employee -> employee.getJobHistory().stream())
.map(JobHistoryEntry::getEmployer)
.collect(Collectors.toSet());

assertThat(companies, containsInAnyOrder("EPAM", "google", "yandex", "mail.ru", "T-Systems"));
}

@Test
void findMinimalAgeOfEmployees() {
List<Employee> employees = getEmployees();

// TODO реализация
Integer minimalAge = employees.stream()
.map(employee -> employee.getPerson().getAge())
.min(Integer::compareTo)
.get();

assertThat(minimalAge, is(21));
}

private static List<Employee> getEmployees() {
return Arrays.asList(
new Employee(
new Person("Иван", "Мельников", 30),
Arrays.asList(
new JobHistoryEntry(2, "dev", "EPAM"),
new JobHistoryEntry(1, "dev", "google")
)),
new Employee(
new Person("Александр", "Дементьев", 28),
Arrays.asList(
new JobHistoryEntry(1, "tester", "EPAM"),
new JobHistoryEntry(1, "dev", "EPAM"),
new JobHistoryEntry(1, "dev", "google")
)),
new Employee(
new Person("Дмитрий", "Осинов", 40),
Arrays.asList(
new JobHistoryEntry(3, "QA", "yandex"),
new JobHistoryEntry(1, "QA", "mail.ru"),
new JobHistoryEntry(1, "dev", "mail.ru")
)),
new Employee(
new Person("Анна", "Светличная", 21),
Collections.singletonList(
new JobHistoryEntry(1, "tester", "T-Systems")
)),
new Employee(
new Person("Игорь", "Толмачёв", 50),
Arrays.asList(
new JobHistoryEntry(5, "tester", "EPAM"),
new JobHistoryEntry(6, "QA", "EPAM")
)),
new Employee(
new Person("Иван", "Александров", 33),
Arrays.asList(
new JobHistoryEntry(2, "QA", "T-Systems"),
new JobHistoryEntry(3, "QA", "EPAM"),
new JobHistoryEntry(1, "dev", "EPAM")
))
);
}
}