-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathDelegatingDataLoader.java
More file actions
188 lines (160 loc) · 5.5 KB
/
DelegatingDataLoader.java
File metadata and controls
188 lines (160 loc) · 5.5 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
package org.dataloader;
import org.dataloader.annotations.PublicApi;
import org.dataloader.stats.Statistics;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
* This delegating {@link DataLoader} makes it easier to create wrappers of {@link DataLoader}s in case you want to change how
* values are returned for example.
* <p>
* The most common way would be to make a new {@link DelegatingDataLoader} subclass that overloads the {@link DelegatingDataLoader#load(Object, Object)}
* method.
* <p>
* For example the following allows you to change the returned value in some way :
* <pre>{@code
* DataLoader<String, String> rawLoader = createDataLoader();
* DelegatingDataLoader<String, String> delegatingDataLoader = new DelegatingDataLoader<>(rawLoader) {
* public CompletableFuture<String> load(@NonNull String key, @Nullable Object keyContext) {
* CompletableFuture<String> cf = super.load(key, keyContext);
* return cf.thenApply(v -> "|" + v + "|");
* }
*};
*}</pre>
*
* @param <K> type parameter indicating the type of the data load keys
* @param <V> type parameter indicating the type of the data that is returned
*/
@PublicApi
@NullMarked
public class DelegatingDataLoader<K, V> extends DataLoader<K, V> {
protected final DataLoader<K, V> delegate;
/**
* This can be called to unwrap a given {@link DataLoader} such that if it's a {@link DelegatingDataLoader} the underlying
* {@link DataLoader} is returned otherwise it's just passed in data loader
*
* @param dataLoader the dataLoader to unwrap
* @param <K> type parameter indicating the type of the data load keys
* @param <V> type parameter indicating the type of the data that is returned
* @return the delegate dataLoader OR just this current one if it's not wrapped
*/
public static <K, V> DataLoader<K, V> unwrap(DataLoader<K, V> dataLoader) {
if (dataLoader instanceof DelegatingDataLoader) {
return ((DelegatingDataLoader<K, V>) dataLoader).getDelegate();
}
return dataLoader;
}
public DelegatingDataLoader(DataLoader<K, V> delegate) {
super(delegate.getBatchLoadFunction(), delegate.getOptions());
this.delegate = delegate;
}
public DataLoader<K, V> getDelegate() {
return delegate;
}
/**
* The {@link DataLoader#load(Object)} and {@link DataLoader#loadMany(List)} type methods all call back
* to the {@link DataLoader#load(Object, Object)} and hence we don't override them.
*
* @param key the key to load
* @param keyContext a context object that is specific to this key
* @return the future of the value
*/
@Override
public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) {
return delegate.load(key, keyContext);
}
@Override
public DataLoader<K, V> transform(Consumer<DataLoaderFactory.Builder<K, V>> builderConsumer) {
return delegate.transform(builderConsumer);
}
@Override
public Instant getLastDispatchTime() {
return delegate.getLastDispatchTime();
}
@Override
public Duration getTimeSinceDispatch() {
return delegate.getTimeSinceDispatch();
}
@Override
public Optional<CompletableFuture<V>> getIfPresent(K key) {
return delegate.getIfPresent(key);
}
@Override
public Optional<CompletableFuture<V>> getIfCompleted(K key) {
return delegate.getIfCompleted(key);
}
@Override
public CompletableFuture<List<V>> dispatch() {
return delegate.dispatch();
}
@Override
public DispatchResult<V> dispatchWithCounts() {
return delegate.dispatchWithCounts();
}
@Override
public List<V> dispatchAndJoin() {
return delegate.dispatchAndJoin();
}
@Override
public int dispatchDepth() {
return delegate.dispatchDepth();
}
@Override
public Object getCacheKey(K key) {
return delegate.getCacheKey(key);
}
@Override
public Statistics getStatistics() {
return delegate.getStatistics();
}
@Override
public CacheMap<Object, V> getCacheMap() {
return delegate.getCacheMap();
}
@Override
public ValueCache<K, V> getValueCache() {
return delegate.getValueCache();
}
@Override
public DataLoader<K, V> clear(K key) {
delegate.clear(key);
return this;
}
@Override
public DataLoader<K, V> clear(K key, BiConsumer<Void, Throwable> handler) {
delegate.clear(key, handler);
return this;
}
@Override
public DataLoader<K, V> clearAll() {
delegate.clearAll();
return this;
}
@Override
public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
delegate.clearAll(handler);
return this;
}
@Override
public DataLoader<K, V> prime(K key, V value) {
delegate.prime(key, value);
return this;
}
@Override
public DataLoader<K, V> prime(K key, Exception error) {
delegate.prime(key, error);
return this;
}
@Override
public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
delegate.prime(key, value);
return this;
}
}