Skip to content

Commit 0a5a78d

Browse files
committed
Moar work on getIfPresent
1 parent 1658732 commit 0a5a78d

File tree

2 files changed

+40
-49
lines changed

2 files changed

+40
-49
lines changed

src/main/java/org/dataloader/DataLoader.java

+37-44
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
*
5252
* @param <K> type parameter indicating the type of the data load keys
5353
* @param <V> type parameter indicating the type of the data that is returned
54-
*
5554
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
5655
* @author <a href="https://github.com/bbakerman/">Brad Baker</a>
5756
*/
@@ -69,7 +68,6 @@ public class DataLoader<K, V> {
6968
* @param batchLoadFunction the batch load function to use
7069
* @param <K> the key type
7170
* @param <V> the value type
72-
*
7371
* @return a new DataLoader
7472
*/
7573
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadFunction) {
@@ -83,7 +81,6 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
8381
* @param options the options to use
8482
* @param <K> the key type
8583
* @param <V> the value type
86-
*
8784
* @return a new DataLoader
8885
*/
8986
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
@@ -104,7 +101,6 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
104101
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
105102
* @param <K> the key type
106103
* @param <V> the value type
107-
*
108104
* @return a new DataLoader
109105
*/
110106
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>> batchLoadFunction) {
@@ -120,9 +116,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
120116
* @param options the options to use
121117
* @param <K> the key type
122118
* @param <V> the value type
123-
*
124119
* @return a new DataLoader
125-
*
126120
* @see #newDataLoaderWithTry(BatchLoader)
127121
*/
128122
@SuppressWarnings("unchecked")
@@ -137,7 +131,6 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
137131
* @param batchLoadFunction the batch load function to use
138132
* @param <K> the key type
139133
* @param <V> the value type
140-
*
141134
* @return a new DataLoader
142135
*/
143136
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V> batchLoadFunction) {
@@ -151,7 +144,6 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
151144
* @param options the options to use
152145
* @param <K> the key type
153146
* @param <V> the value type
154-
*
155147
* @return a new DataLoader
156148
*/
157149
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
@@ -172,7 +164,6 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
172164
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
173165
* @param <K> the key type
174166
* @param <V> the value type
175-
*
176167
* @return a new DataLoader
177168
*/
178169
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContext<K, Try<V>> batchLoadFunction) {
@@ -188,9 +179,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
188179
* @param options the options to use
189180
* @param <K> the key type
190181
* @param <V> the value type
191-
*
192182
* @return a new DataLoader
193-
*
194183
* @see #newDataLoaderWithTry(BatchLoader)
195184
*/
196185
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
@@ -204,7 +193,6 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
204193
* @param batchLoadFunction the batch load function to use
205194
* @param <K> the key type
206195
* @param <V> the value type
207-
*
208196
* @return a new DataLoader
209197
*/
210198
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V> batchLoadFunction) {
@@ -218,7 +206,6 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
218206
* @param options the options to use
219207
* @param <K> the key type
220208
* @param <V> the value type
221-
*
222209
* @return a new DataLoader
223210
*/
224211
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
@@ -232,15 +219,14 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
232219
* <p>
233220
* If its important you to know the exact status of each item in a batch call and whether it threw exceptions then
234221
* you can use this form to create the data loader.
235-
*
222+
* <p>
236223
* Using Try objects allows you to capture a value returned or an exception that might
237224
* have occurred trying to get a value. .
238225
* <p>
239226
*
240227
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
241228
* @param <K> the key type
242229
* @param <V> the value type
243-
*
244230
* @return a new DataLoader
245231
*/
246232
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoader<K, Try<V>> batchLoadFunction) {
@@ -256,9 +242,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
256242
* @param options the options to use
257243
* @param <K> the key type
258244
* @param <V> the value type
259-
*
260245
* @return a new DataLoader
261-
*
262246
* @see #newDataLoaderWithTry(BatchLoader)
263247
*/
264248
@SuppressWarnings("unchecked")
@@ -273,7 +257,6 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
273257
* @param batchLoadFunction the batch load function to use
274258
* @param <K> the key type
275259
* @param <V> the value type
276-
*
277260
* @return a new DataLoader
278261
*/
279262
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithContext<K, V> batchLoadFunction) {
@@ -287,7 +270,6 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
287270
* @param options the options to use
288271
* @param <K> the key type
289272
* @param <V> the value type
290-
*
291273
* @return a new DataLoader
292274
*/
293275
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
@@ -308,7 +290,6 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
308290
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
309291
* @param <K> the key type
310292
* @param <V> the value type
311-
*
312293
* @return a new DataLoader
313294
*/
314295
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction) {
@@ -324,9 +305,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
324305
* @param options the options to use
325306
* @param <K> the key type
326307
* @param <V> the value type
327-
*
328308
* @return a new DataLoader
329-
*
330309
* @see #newDataLoaderWithTry(BatchLoader)
331310
*/
332311
@SuppressWarnings("unchecked")
@@ -375,37 +354,57 @@ private CacheMap<Object, CompletableFuture<V>> determineCacheMap(DataLoaderOptio
375354
* and returned from cache).
376355
*
377356
* @param key the key to load
378-
*
379357
* @return the future of the value
380358
*/
381359
public CompletableFuture<V> load(K key) {
382360
return load(key, null);
383361
}
384362

363+
/**
364+
* This will return an optional promise to a value previously loaded via {@link #load(Object)} call or empty if not call has been made for that key.
365+
* <p>
366+
* If you do get a present CompletableFuture it does not mean it has been dispatched and completed yet. It just means
367+
* its at least pending and in cache. Of course if caching is disabled there will never be a present Optional returned.
368+
* <p>
369+
* NOTE : This will NOT cause a data load to happen. You must called {@link #load(Object)} for that.
370+
*
371+
* @param key the key to check
372+
* @return an Optional to the future of the value
373+
*/
385374
public Optional<CompletableFuture<V>> getIfPresent(K key) {
386375
return helper.getIfPresent(key);
387376
}
388377

378+
/**
379+
* This will return an optional promise to a value previously loaded via {@link #load(Object)} call that has in fact been completed or empty
380+
* if no call has been made for that key or the promise has not completed yet.
381+
* <p>
382+
* If you do get a present CompletableFuture it means it has been dispatched and completed.
383+
* <p>
384+
* NOTE : This will NOT cause a data load to happen. You must called {@link #load(Object)} for that.
385+
*
386+
* @param key the key to check
387+
* @return an Optional to the future of the value
388+
*/
389389
public Optional<CompletableFuture<V>> getIfCompleted(K key) {
390390
return helper.getIfCompleted(key);
391391
}
392392

393393

394-
/**
395-
* Requests to load the data with the specified key asynchronously, and returns a future of the resulting value.
396-
* <p>
397-
* If batching is enabled (the default), you'll have to call {@link DataLoader#dispatch()} at a later stage to
398-
* start batch execution. If you forget this call the future will never be completed (unless already completed,
399-
* and returned from cache).
400-
* <p>
401-
* The key context object may be useful in the batch loader interfaces such as {@link org.dataloader.BatchLoaderWithContext} or
402-
* {@link org.dataloader.MappedBatchLoaderWithContext} to help retrieve data.
403-
*
404-
* @param key the key to load
405-
* @param keyContext a context object that is specific to this key
406-
*
407-
* @return the future of the value
408-
*/
394+
/**
395+
* Requests to load the data with the specified key asynchronously, and returns a future of the resulting value.
396+
* <p>
397+
* If batching is enabled (the default), you'll have to call {@link DataLoader#dispatch()} at a later stage to
398+
* start batch execution. If you forget this call the future will never be completed (unless already completed,
399+
* and returned from cache).
400+
* <p>
401+
* The key context object may be useful in the batch loader interfaces such as {@link org.dataloader.BatchLoaderWithContext} or
402+
* {@link org.dataloader.MappedBatchLoaderWithContext} to help retrieve data.
403+
*
404+
* @param key the key to load
405+
* @param keyContext a context object that is specific to this key
406+
* @return the future of the value
407+
*/
409408
public CompletableFuture<V> load(K key, Object keyContext) {
410409
return helper.load(key, keyContext);
411410
}
@@ -419,7 +418,6 @@ public CompletableFuture<V> load(K key, Object keyContext) {
419418
* and returned from cache).
420419
*
421420
* @param keys the list of keys to load
422-
*
423421
* @return the composite future of the list of values
424422
*/
425423
public CompletableFuture<List<V>> loadMany(List<K> keys) {
@@ -439,7 +437,6 @@ public CompletableFuture<List<V>> loadMany(List<K> keys) {
439437
*
440438
* @param keys the list of keys to load
441439
* @param keyContexts the list of key calling context objects
442-
*
443440
* @return the composite future of the list of values
444441
*/
445442
public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContexts) {
@@ -505,7 +502,6 @@ public int dispatchDepth() {
505502
* on the next load request.
506503
*
507504
* @param key the key to remove
508-
*
509505
* @return the data loader for fluent coding
510506
*/
511507
public DataLoader<K, V> clear(K key) {
@@ -533,7 +529,6 @@ public DataLoader<K, V> clearAll() {
533529
*
534530
* @param key the key
535531
* @param value the value
536-
*
537532
* @return the data loader for fluent coding
538533
*/
539534
public DataLoader<K, V> prime(K key, V value) {
@@ -551,7 +546,6 @@ public DataLoader<K, V> prime(K key, V value) {
551546
*
552547
* @param key the key
553548
* @param error the exception to prime instead of a value
554-
*
555549
* @return the data loader for fluent coding
556550
*/
557551
public DataLoader<K, V> prime(K key, Exception error) {
@@ -569,7 +563,6 @@ public DataLoader<K, V> prime(K key, Exception error) {
569563
* If no cache key function is present in {@link DataLoaderOptions}, then the returned value equals the input key.
570564
*
571565
* @param key the input key
572-
*
573566
* @return the cache key after the input is transformed with the cache key function
574567
*/
575568
public Object getCacheKey(K key) {

src/test/java/org/dataloader/DataLoaderIfPresentTest.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ public void should_detect_if_present_cf() {
4747
public void should_not_be_present_if_cleared() {
4848
DataLoader<Integer, Integer> dataLoader = new DataLoader<>(keysAsValues());
4949

50-
Optional<CompletableFuture<Integer>> cachedPromise = dataLoader.getIfPresent(1);
5150
dataLoader.load(1);
5251

53-
cachedPromise = dataLoader.getIfPresent(1);
52+
Optional<CompletableFuture<Integer>> cachedPromise = dataLoader.getIfPresent(1);
5453
assertThat(cachedPromise.isPresent(), equalTo(true));
5554

5655
dataLoader.clear(1);
@@ -67,10 +66,9 @@ public void should_not_be_present_if_cleared() {
6766
public void should_allow_completed_cfs_to_be_found() {
6867
DataLoader<Integer, Integer> dataLoader = new DataLoader<>(keysAsValues());
6968

70-
Optional<CompletableFuture<Integer>> cachedPromise = dataLoader.getIfPresent(1);
7169
dataLoader.load(1);
7270

73-
cachedPromise = dataLoader.getIfPresent(1);
71+
Optional<CompletableFuture<Integer>> cachedPromise = dataLoader.getIfPresent(1);
7472
assertThat(cachedPromise.isPresent(), equalTo(true));
7573

7674
cachedPromise = dataLoader.getIfCompleted(1);
@@ -89,7 +87,7 @@ public void should_allow_completed_cfs_to_be_found() {
8987
@Test
9088
public void should_work_with_primed_caches() {
9189
DataLoader<Integer, Integer> dataLoader = new DataLoader<>(keysAsValues());
92-
dataLoader.prime(1, 666);
90+
dataLoader.prime(1, 666).prime(2, 999);
9391

9492
Optional<CompletableFuture<Integer>> cachedPromise = dataLoader.getIfPresent(1);
9593
assertThat(cachedPromise.isPresent(), equalTo(true));

0 commit comments

Comments
 (0)