|
| 1 | +package org.dataloader; |
| 2 | + |
| 3 | +import org.dataloader.annotations.PublicApi; |
| 4 | +import org.dataloader.stats.Statistics; |
| 5 | +import org.jspecify.annotations.NonNull; |
| 6 | +import org.jspecify.annotations.NullMarked; |
| 7 | +import org.jspecify.annotations.Nullable; |
| 8 | + |
| 9 | +import java.time.Duration; |
| 10 | +import java.time.Instant; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.concurrent.CompletableFuture; |
| 14 | +import java.util.function.BiConsumer; |
| 15 | +import java.util.function.Consumer; |
| 16 | + |
| 17 | +/** |
| 18 | + * This delegating {@link DataLoader} makes it easier to create wrappers of {@link DataLoader}s in case you want to change how |
| 19 | + * values are returned for example. |
| 20 | + * <p> |
| 21 | + * The most common way would be to make a new {@link DelegatingDataLoader} subclass that overloads the {@link DelegatingDataLoader#load(Object, Object)} |
| 22 | + * method. |
| 23 | + * <p> |
| 24 | + * For example the following allows you to change the returned value in some way : |
| 25 | + * <pre>{@code |
| 26 | + * DataLoader<String, String> rawLoader = createDataLoader(); |
| 27 | + * DelegatingDataLoader<String, String> delegatingDataLoader = new DelegatingDataLoader<>(rawLoader) { |
| 28 | + * public CompletableFuture<String> load(@NonNull String key, @Nullable Object keyContext) { |
| 29 | + * CompletableFuture<String> cf = super.load(key, keyContext); |
| 30 | + * return cf.thenApply(v -> "|" + v + "|"); |
| 31 | + * } |
| 32 | + *}; |
| 33 | + *}</pre> |
| 34 | + * |
| 35 | + * @param <K> type parameter indicating the type of the data load keys |
| 36 | + * @param <V> type parameter indicating the type of the data that is returned |
| 37 | + */ |
| 38 | +@PublicApi |
| 39 | +@NullMarked |
| 40 | +public class DelegatingDataLoader<K, V> extends DataLoader<K, V> { |
| 41 | + |
| 42 | + protected final DataLoader<K, V> delegate; |
| 43 | + |
| 44 | + /** |
| 45 | + * This can be called to unwrap a given {@link DataLoader} such that if it's a {@link DelegatingDataLoader} the underlying |
| 46 | + * {@link DataLoader} is returned otherwise it's just passed in data loader |
| 47 | + * |
| 48 | + * @param dataLoader the dataLoader to unwrap |
| 49 | + * @param <K> type parameter indicating the type of the data load keys |
| 50 | + * @param <V> type parameter indicating the type of the data that is returned |
| 51 | + * @return the delegate dataLoader OR just this current one if it's not wrapped |
| 52 | + */ |
| 53 | + public static <K, V> DataLoader<K, V> unwrap(DataLoader<K, V> dataLoader) { |
| 54 | + if (dataLoader instanceof DelegatingDataLoader) { |
| 55 | + return ((DelegatingDataLoader<K, V>) dataLoader).getDelegate(); |
| 56 | + } |
| 57 | + return dataLoader; |
| 58 | + } |
| 59 | + |
| 60 | + public DelegatingDataLoader(DataLoader<K, V> delegate) { |
| 61 | + super(delegate.getBatchLoadFunction(), delegate.getOptions()); |
| 62 | + this.delegate = delegate; |
| 63 | + } |
| 64 | + |
| 65 | + public DataLoader<K, V> getDelegate() { |
| 66 | + return delegate; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * The {@link DataLoader#load(Object)} and {@link DataLoader#loadMany(List)} type methods all call back |
| 71 | + * to the {@link DataLoader#load(Object, Object)} and hence we don't override them. |
| 72 | + * |
| 73 | + * @param key the key to load |
| 74 | + * @param keyContext a context object that is specific to this key |
| 75 | + * @return the future of the value |
| 76 | + */ |
| 77 | + @Override |
| 78 | + public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) { |
| 79 | + return delegate.load(key, keyContext); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public DataLoader<K, V> transform(Consumer<DataLoaderFactory.Builder<K, V>> builderConsumer) { |
| 84 | + return delegate.transform(builderConsumer); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Instant getLastDispatchTime() { |
| 89 | + return delegate.getLastDispatchTime(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Duration getTimeSinceDispatch() { |
| 94 | + return delegate.getTimeSinceDispatch(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Optional<CompletableFuture<V>> getIfPresent(K key) { |
| 99 | + return delegate.getIfPresent(key); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Optional<CompletableFuture<V>> getIfCompleted(K key) { |
| 104 | + return delegate.getIfCompleted(key); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public CompletableFuture<List<V>> dispatch() { |
| 109 | + return delegate.dispatch(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public DispatchResult<V> dispatchWithCounts() { |
| 114 | + return delegate.dispatchWithCounts(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public List<V> dispatchAndJoin() { |
| 119 | + return delegate.dispatchAndJoin(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public int dispatchDepth() { |
| 124 | + return delegate.dispatchDepth(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Object getCacheKey(K key) { |
| 129 | + return delegate.getCacheKey(key); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Statistics getStatistics() { |
| 134 | + return delegate.getStatistics(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public CacheMap<Object, V> getCacheMap() { |
| 139 | + return delegate.getCacheMap(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public ValueCache<K, V> getValueCache() { |
| 144 | + return delegate.getValueCache(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public DataLoader<K, V> clear(K key) { |
| 149 | + delegate.clear(key); |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public DataLoader<K, V> clear(K key, BiConsumer<Void, Throwable> handler) { |
| 155 | + delegate.clear(key, handler); |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public DataLoader<K, V> clearAll() { |
| 161 | + delegate.clearAll(); |
| 162 | + return this; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) { |
| 167 | + delegate.clearAll(handler); |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public DataLoader<K, V> prime(K key, V value) { |
| 173 | + delegate.prime(key, value); |
| 174 | + return this; |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public DataLoader<K, V> prime(K key, Exception error) { |
| 179 | + delegate.prime(key, error); |
| 180 | + return this; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public DataLoader<K, V> prime(K key, CompletableFuture<V> value) { |
| 185 | + delegate.prime(key, value); |
| 186 | + return this; |
| 187 | + } |
| 188 | +} |
0 commit comments