-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathStep.java
More file actions
67 lines (54 loc) · 2.25 KB
/
Step.java
File metadata and controls
67 lines (54 loc) · 2.25 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
package org.dataloader.orchestration;
import org.dataloader.DataLoader;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import static org.dataloader.orchestration.Orchestrator.castAs;
public class Step<K, V> {
private final Orchestrator<?, ?> orchestrator;
private final DataLoader<Object, Object> dl;
private final Function<K, CompletableFuture<V>> codeToRun;
Step(Orchestrator<?, ?> orchestrator, DataLoader<?, ?> dataLoader, Function<K, CompletableFuture<V>> codeToRun) {
this.orchestrator = orchestrator;
this.dl = castAs(dataLoader);
this.codeToRun = codeToRun;
}
DataLoader<Object, Object> dataLoader() {
return dl;
}
public Function<K, CompletableFuture<V>> codeToRun() {
return codeToRun;
}
public <KT, VT> With<KT, VT> with(DataLoader<KT, VT> dataLoader) {
return new With<>(orchestrator, dataLoader);
}
public Step<K, V> load(K key, Object keyContext) {
return loadImpl(orchestrator, dl, key, keyContext);
}
public Step<V, V> thenLoad(Function<V, K> codeToRun) {
return thenLoadImpl(orchestrator, dl, codeToRun);
}
static <K, V> Step<K, V> loadImpl(Orchestrator<?, ?> orchestrator, DataLoader<Object, Object> dl, K key, Object keyContext) {
Function<K, CompletableFuture<V>> codeToRun = k -> {
CompletableFuture<V> cf = castAs(dl.load(key, keyContext));
orchestrator.getTracker().loadCall(dl);
return cf;
};
Step<K, V> step = new Step<>(orchestrator, dl, codeToRun);
orchestrator.record(step);
return step;
}
static <K, V> Step<V, V> thenLoadImpl(Orchestrator<?, ?> orchestrator, DataLoader<Object, Object> dl, Function<V, K> codeToRun) {
Function<V, CompletableFuture<V>> actualCodeToRun = v -> {
K key = codeToRun.apply(v);
CompletableFuture<V> cf = castAs(dl.load(key));
orchestrator.getTracker().loadCall(dl);
return cf;
};
Step<V, V> step = new Step<>(orchestrator, dl, actualCodeToRun);
orchestrator.record(step);
return step;
}
public CompletableFuture<V> toCompletableFuture() {
return orchestrator.execute();
}
}