Skip to content
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
6 changes: 4 additions & 2 deletions src/main/java/data/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @author Simon Popugaev
*/
public class Generator {
private static final int MAX_EMPLOYEE_AMOUNT = 15;

public static String generateString() {
final String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Expand Down Expand Up @@ -60,7 +61,8 @@ public static Employee generateEmployee() {
}

public static List<Employee> generateEmployeeList() {
// TODO
throw new UnsupportedOperationException();
return Stream.generate(Generator::generateEmployee)
.limit(ThreadLocalRandom.current().nextInt(MAX_EMPLOYEE_AMOUNT))
.collect(toList());
}
}
86 changes: 78 additions & 8 deletions src/test/java/part1/exercise/StreamsExercise1.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;

public class StreamsExercise1 {
// https://youtu.be/kxgo7Y4cdA8 Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 1
Expand All @@ -25,14 +26,79 @@ public class StreamsExercise1 {

@Test
public void getAllEpamEmployees() {
List<Person> epamEmployees = null;// TODO all persons with experience in epam
throw new UnsupportedOperationException();
final List<Employee> employees = generateEmployeeList();
List<Person> expected = new ArrayList<>();

for (final Employee employee : employees) {
if (hasEpamExperience(employee)) {
expected.add(employee.getPerson());
}
}

List<Person> epamEmployees = employees.stream()
.filter(this::hasEpamExperience)
.map(Employee::getPerson)
.collect(Collectors.toList());

assertEquals(expected, epamEmployees);
}

private boolean hasEpamExperience(Employee employee) {
return employee.getJobHistory().stream()
.anyMatch(jobHistoryEntry -> jobHistoryEntry.getEmployer().equals("epam"));
}

static class PersonFirstExperience {
private Person person;
private JobHistoryEntry firstJob;

public PersonFirstExperience(Person person, JobHistoryEntry firstJob) {
this.person = person;
this.firstJob = firstJob;
}

public Person getPerson() {
return person;
}

public JobHistoryEntry getFirstJob() {
return firstJob;
}

public static Optional<PersonFirstExperience> fromEmployee(Employee employee) {
if (employee.getJobHistory().size() < 1) {
return Optional.empty();
}

return Optional.of(new PersonFirstExperience(employee.getPerson(), employee.getJobHistory().get(0)));
}
}

@Test
public void getEmployeesStartedFromEpam() {
List<Person> epamEmployees = null;// TODO all persons with first experience in epam
throw new UnsupportedOperationException();
List<Employee> employees = generateEmployeeList();

List<Person> expected = new ArrayList<>();

for (final Employee employee : employees) {
if (employee.getJobHistory().size() > 1 && employee.getJobHistory().get(0).getEmployer().equals("epam")) {
expected.add(employee.getPerson());
}
}

List<Person> epamEmployees = employees.stream()
.map(PersonFirstExperience::fromEmployee)
.filter(Optional::isPresent)
.map(Optional::get)
.filter(this::withEpamFirstJob)
.map(PersonFirstExperience::getPerson)
.collect(toList());

assertEquals(expected, epamEmployees);
}

private boolean withEpamFirstJob(PersonFirstExperience personFirstExperience) {
return personFirstExperience.getFirstJob().getEmployer().equals("epam");
}

@Test
Expand All @@ -49,11 +115,15 @@ public void sumEpamDurations() {
}
}

// TODO
throw new UnsupportedOperationException();
int result = employees.stream()
.flatMap(
employee -> employee.getJobHistory().stream()
)
.filter(jobHistoryEntry -> jobHistoryEntry.getEmployer().equals("epam"))
.mapToInt(JobHistoryEntry::getDuration)
.sum();

// int result = ???
// assertEquals(expected, result);
assertEquals(expected, result);
}

}
123 changes: 114 additions & 9 deletions src/test/java/part1/exercise/StreamsExercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import data.Person;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Stream;

import static data.Generator.generateEmployeeList;
import static java.util.stream.Collectors.*;
import static org.junit.Assert.assertEquals;
import static part1.exercise.StreamsExercise2.PersonEmployerDuration.fromJobHistoryAndEmployee;

public class StreamsExercise2 {
// https://youtu.be/kxgo7Y4cdA8 Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 1
Expand All @@ -24,25 +23,131 @@ public class StreamsExercise2 {

// TODO class PersonEmployerPair

static class PersonEmployerPair {
private Person person;
private String employer;

public PersonEmployerPair(Person person, String employer) {
this.person = person;
this.employer = employer;
}

public Person getPerson() {
return person;
}

public String getEmployer() {
return employer;
}
}

@Test
public void employersStuffLists() {
Map<String, List<Person>> employersStuffLists = null;// TODO
throw new UnsupportedOperationException();
List<Employee> employees = getEmployees();

Map<String, List<Person>> employersStuffLists = employees.stream()
.flatMap(this::toPersonEmployerPair)
.collect(groupingBy(
PersonEmployerPair::getEmployer, mapping(PersonEmployerPair::getPerson, toList())
)
);

List<Person> expected = Arrays.asList(
new Person("John", "Galt", 20),
new Person("John", "Galt", 23),
new Person("John", "Galt", 26),
new Person("John", "Galt", 29)
);

assertEquals(expected, employersStuffLists.get("google"));
}

private Stream<? extends PersonEmployerPair> toPersonEmployerPair(Employee employee) {
return employee.getJobHistory().stream()
.map(jobHistoryEntry -> new PersonEmployerPair(employee.getPerson(), jobHistoryEntry.getEmployer()));
}

@Test
public void indexByFirstEmployer() {
Map<String, List<Person>> employeesIndex = null;// TODO
throw new UnsupportedOperationException();
Map<String, List<Person>> employeesIndex = getEmployees().stream()
.filter(employee -> employee.getJobHistory().size() > 0)
.map(
employee -> new PersonEmployerPair(employee.getPerson(), employee.getJobHistory().get(0).getEmployer())
)
.collect(
groupingBy(
PersonEmployerPair::getEmployer,
mapping(PersonEmployerPair::getPerson, toList())
)
);

List<Person> expected = Arrays.asList(
new Person("John", "Galt", 20),
new Person("John", "White", 22),
new Person("John", "Galt", 23),
new Person("John", "White", 25),
new Person("John", "Galt", 26),
new Person("John", "White", 28),
new Person("John", "Galt", 29),
new Person("Bob", "White", 31)
);

assertEquals(expected,employeesIndex.get("epam"));

}

static class PersonEmployerDuration {
private Person person;
private String employer;
private int duration;

public PersonEmployerDuration(Person person, String employer, int duration) {
this.person = person;
this.employer = employer;
this.duration = duration;
}

public Person getPerson() {
return person;
}

public String getEmployer() {
return employer;
}

public int getDuration() {
return duration;
}

public static PersonEmployerDuration fromJobHistoryAndEmployee(Employee employee, JobHistoryEntry jobHistoryEntry) {
return new PersonEmployerDuration(employee.getPerson(), jobHistoryEntry.getEmployer(), jobHistoryEntry.getDuration());
}

}

@Test
public void greatestExperiencePerEmployer() {
Map<String, Person> employeesIndex = null;// TODO
List<Employee> employees = getEmployees();

Map<String, Person> employeesIndex = employees.stream()
.flatMap(this::toPersonEmployerDuration)
.collect(
groupingBy(
PersonEmployerDuration::getEmployer,
collectingAndThen(
maxBy(Comparator.comparing(PersonEmployerDuration::getDuration)),
p -> p.get().getPerson()
)
)
);

assertEquals(new Person("John", "White", 28), employeesIndex.get("epam"));
}

private Stream<? extends PersonEmployerDuration> toPersonEmployerDuration(Employee employee) {
return employee.getJobHistory().stream()
.map(jobHistoryEntry -> fromJobHistoryAndEmployee(employee, jobHistoryEntry));
}

private List<Employee> getEmployees() {
return Arrays.asList(
Expand Down