-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathComposeCachingDataStorage.java
executable file
·41 lines (32 loc) · 1.53 KB
/
ComposeCachingDataStorage.java
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
package part3.exercise;
import part2.cache.CachingDataStorage;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
public class ComposeCachingDataStorage<K1, T1, K2, T2> implements CachingDataStorage<K1, T2> {
private final CachingDataStorage<K1, T1> storage1;
private final CachingDataStorage<K2, T2> storage2;
private final Function<T1, K2> mapping;
public ComposeCachingDataStorage(CachingDataStorage<K1, T1> storage1,
CachingDataStorage<K2, T2> storage2,
Function<T1, K2> mapping) {
this.storage1 = storage1;
this.storage2 = storage2;
this.mapping = mapping;
}
@Override
public OutdatableResult<T2> getOutdatable(K1 key) {
final OutdatableResult<T1> outdatable = storage1.getOutdatable(key);
final CompletableFuture<OutdatableResult<T2>> outdated
= outdatable.getResult().thenApply(v -> storage2.getOutdatable(mapping.apply(v)));
final OutdatableResult<T2> result
= new OutdatableResult<>(new CompletableFuture<>(), new CompletableFuture<>());
outdatable.getOutdated().thenAccept(v -> result.getOutdated().complete(v));
outdated.thenAccept(v -> {
v.getResult().thenAccept(t -> {
result.getResult().complete(t);
result.getOutdated().thenAccept(o -> result.getOutdated().complete(o));
});
});
return result;
}
}