-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathCompletableFutureBasics.java
More file actions
executable file
·199 lines (146 loc) · 7.62 KB
/
CompletableFutureBasics.java
File metadata and controls
executable file
·199 lines (146 loc) · 7.62 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package part1.exercise;
import data.Employee;
import data.Generator;
import data.Person;
import db.SlowCompletableFutureDb;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
public class CompletableFutureBasics {
private static SlowCompletableFutureDb<Employee> employeeDb;
private static List<String> keys;
@BeforeClass
public static void before() {
final Map<String, Employee> employeeMap = Generator.generateEmployeeList(1000)
.stream()
.collect(toMap(
e -> getKeyByPerson(e.getPerson()),
Function.identity(),
(a, b) -> a));
employeeDb = new SlowCompletableFutureDb<>(employeeMap);
keys = employeeMap.keySet().stream().collect(toList());
}
private static String getKeyByPerson(Person person) {
return person.getFirstName() + "_" + person.getLastName() + "_" + person.getAge();
}
@AfterClass
public static void after() {
try {
employeeDb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void createNonEmpty() throws ExecutionException, InterruptedException {
final Person person = new Person("John", "Galt", 33);
// TODO Create non empty Optional
final Optional<Person> optPerson = Optional.of(person);
assertTrue(optPerson.isPresent());
assertEquals(person, optPerson.get());
// TODO Create stream with a single element
final Stream<Person> streamPerson = Stream.of(person);
final List<Person> persons = streamPerson.collect(toList());
assertThat(persons.size(), is(1));
assertEquals(person, persons.get(0));
// TODO Create completed CompletableFuture
final CompletableFuture<Person> futurePerson = CompletableFuture.completedFuture(person);
assertTrue(futurePerson.isDone());
assertEquals(person, futurePerson.get());
}
@Test
public void createEmpty() throws ExecutionException, InterruptedException {
// TODO Create empty Optional
final Optional<Person> optPerson = Optional.empty();
assertFalse(optPerson.isPresent());
// TODO Create empty stream
final Stream<Person> streamPerson = Stream.empty();
final List<Person> persons = streamPerson.collect(toList());
assertThat(persons.size(), is(0));
// TODO Complete CompletableFuture with NoSuchElementException
final CompletableFuture<Person> futurePerson = new CompletableFuture<>();
futurePerson.completeExceptionally(new NoSuchElementException());
// futurePerson.???
assertTrue(futurePerson.isCompletedExceptionally());
assertTrue(futurePerson
.thenApply(x -> false)
.exceptionally(t -> t.getCause() instanceof NoSuchElementException).get());
}
@Test
public void forEach() throws ExecutionException, InterruptedException {
final Person person = new Person("John", "Galt", 33);
// TODO Create non empty Optional
final Optional<Person> optPerson = Optional.of(person);
final CompletableFuture<Person> result1 = new CompletableFuture<>();
// TODO using optPerson.ifPresent complete result1
optPerson.ifPresent(result1::complete);
assertEquals(person, result1.get());
// TODO Create stream with a single element
final Stream<Person> streamPerson = Stream.of(person);
final CompletableFuture<Person> result2 = new CompletableFuture<>();
// TODO Using streamPerson.forEach complete result2
streamPerson.forEach(result2::complete);
assertEquals(person, result2.get());
// TODO Create completed CompletableFuture
final CompletableFuture<Person> futurePerson = CompletableFuture.completedFuture(person);
final CompletableFuture<Person> result3 = new CompletableFuture<>();
// TODO Using futurePerson.thenAccept complete result3
futurePerson.thenAccept(result3::complete);
assertEquals(person, result3.get());
}
@Test
public void map() throws ExecutionException, InterruptedException {
final Person person = new Person("John", "Galt", 33);
// TODO Create non empty Optional
final Optional<Person> optPerson = Optional.of(person);
// TODO get Optional<first name> from optPerson
final Optional<String> optFirstName = optPerson.map(Person::getFirstName);
assertEquals(person.getFirstName(), optFirstName.get());
// TODO Create stream with a single element
final Stream<Person> streamPerson = Stream.of(person);
// TODO Get Stream<first name> from streamPerson
final Stream<String> streamFirstName = streamPerson.map(Person::getFirstName);
assertEquals(person.getFirstName(), streamFirstName.collect(toList()).get(0));
// TODO Create completed CompletableFuture
final CompletableFuture<Person> futurePerson = CompletableFuture.completedFuture(person);
// TODO Get CompletableFuture<first name> from futurePerson
final CompletableFuture<String> futureFirstName = futurePerson.thenApply(Person::getFirstName);
assertEquals(person.getFirstName(), futureFirstName.get());
}
@Test
public void flatMap() throws ExecutionException, InterruptedException {
final Person person = employeeDb.get(keys.get(0)).thenApply(Employee::getPerson).get();
// TODO Create non empty Optional
final Optional<Person> optPerson = Optional.of(person);
// TODO Using flatMap and .getFirstName().codePoints().mapToObj(p -> p).findFirst()
// TODO get the first letter of first name if any
final Optional<Integer> optFirstCodePointOfFirstName =
optPerson.flatMap(person1 -> person1.getFirstName().codePoints().mapToObj(value -> value).findFirst());
assertEquals(Integer.valueOf(65), optFirstCodePointOfFirstName.get());
// TODO Create stream with a single element
final Stream<Person> streamPerson = Stream.of(person);
// TODO Using flatMapToInt and .getFirstName().codePoints() get codepoints stream from streamPerson
final IntStream codePoints = streamPerson.flatMapToInt(person1 -> person1.getFirstName().codePoints());
final int[] codePointsArray = codePoints.toArray();
assertEquals(person.getFirstName(), new String(codePointsArray, 0, codePointsArray.length));
// TODO Create completed CompletableFuture
final CompletableFuture<Person> futurePerson = CompletableFuture.completedFuture(person);
// TODO Get CompletableFuture<Employee> from futurePerson using getKeyByPerson and employeeDb
final CompletableFuture<Employee> futureEmployee = futurePerson.thenCompose(person1 -> employeeDb.get(getKeyByPerson(person1)));
assertEquals(person, futureEmployee.thenApply(Employee::getPerson).get());
}
}