-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathStreamsExercise1.java
More file actions
executable file
·104 lines (87 loc) · 3.81 KB
/
StreamsExercise1.java
File metadata and controls
executable file
·104 lines (87 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package part1.exercise;
import data.Employee;
import data.JobHistoryEntry;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import static data.Generator.generateEmployeeList;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class StreamsExercise1 {
// https://youtu.be/kxgo7Y4cdA8 Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 1
// https://youtu.be/JRBWBJ6S4aU Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 2
// https://youtu.be/O8oN4KSZEXE Сергей Куксенко — Stream API, часть 1
// https://youtu.be/i0Jr2l3jrDA Сергей Куксенко — Stream API, часть 2
private final static String EPAM = "epam";
private final Predicate<String> isEpam = s -> s.equals(EPAM);
@Test
public void getAllEpamEmployees() {
List<Employee> initialEmployees = generateEmployeeList();
List<Employee> actual = initialEmployees.stream()
.filter(employee ->
employee.getJobHistory().stream()
.map(JobHistoryEntry::getEmployer)
.anyMatch(isEpam))
.collect(toList());
List<Employee> expected = new ArrayList<>();
for (Employee employee : initialEmployees) {
boolean isEpamEmloyee = false;
for (JobHistoryEntry entry : employee.getJobHistory()) {
if (isEpam.test(entry.getEmployer())) {
isEpamEmloyee = true;
}
}
if (isEpamEmloyee) expected.add(employee);
}
assertThat(actual, is(expected));
}
@Test
public void getEmployeesStartedFromEpam() {
List<Employee> initialList = generateEmployeeList();
List<Employee> actualList = initialList.stream()
.filter(employee -> employee.getJobHistory()
.stream()
.findFirst()
.map(JobHistoryEntry::getEmployer)
.filter(isEpam)
.map(than -> true)
.orElse(false))
.collect(toList());
List<Employee> expectedList = new ArrayList<>();
for (Employee employee : initialList) {
List<JobHistoryEntry> jobHistory = employee.getJobHistory();
boolean epamIsFirstEmployer = false;
if (!jobHistory.isEmpty()) {
String employer = jobHistory.get(0).getEmployer();
epamIsFirstEmployer = isEpam.test(employer);
}
if (epamIsFirstEmployer) {
expectedList.add(employee);
}
}
assertThat(actualList, is(expectedList));
}
@Test
public void sumEpamDurations() {
final List<Employee> employees = generateEmployeeList();
int expected = 0;
for (Employee e : employees) {
for (JobHistoryEntry j : e.getJobHistory()) {
if (j.getEmployer().equals("epam")) {
expected += j.getDuration();
}
}
}
int actual = employees.stream()
.map(Employee::getJobHistory)
.flatMap(Collection::stream)
.filter(s -> isEpam.test(s.getEmployer()))
.mapToInt(JobHistoryEntry::getDuration)
.sum();
assertEquals(expected, actual);
}
}