25
25
import java .time .Clock ;
26
26
import java .time .Duration ;
27
27
import java .time .Instant ;
28
- import java .util .*;
28
+ import java .util .ArrayList ;
29
+ import java .util .Collections ;
30
+ import java .util .HashMap ;
31
+ import java .util .List ;
32
+ import java .util .Map ;
33
+ import java .util .Optional ;
29
34
import java .util .concurrent .CompletableFuture ;
30
35
import java .util .function .BiConsumer ;
36
+ import java .util .function .Consumer ;
31
37
32
38
import static org .dataloader .impl .Assertions .nonNull ;
33
39
54
60
*
55
61
* @param <K> type parameter indicating the type of the data load keys
56
62
* @param <V> type parameter indicating the type of the data that is returned
57
- *
58
63
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
59
64
* @author <a href="https://github.com/bbakerman/">Brad Baker</a>
60
65
*/
@@ -65,6 +70,8 @@ public class DataLoader<K, V> {
65
70
private final StatisticsCollector stats ;
66
71
private final CacheMap <Object , V > futureCache ;
67
72
private final ValueCache <K , V > valueCache ;
73
+ private final DataLoaderOptions options ;
74
+ private final Object batchLoadFunction ;
68
75
69
76
/**
70
77
* Creates new DataLoader with the specified batch loader function and default options
@@ -73,9 +80,7 @@ public class DataLoader<K, V> {
73
80
* @param batchLoadFunction the batch load function to use
74
81
* @param <K> the key type
75
82
* @param <V> the value type
76
- *
77
83
* @return a new DataLoader
78
- *
79
84
* @deprecated use {@link DataLoaderFactory} instead
80
85
*/
81
86
@ Deprecated
@@ -90,9 +95,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
90
95
* @param options the options to use
91
96
* @param <K> the key type
92
97
* @param <V> the value type
93
- *
94
98
* @return a new DataLoader
95
- *
96
99
* @deprecated use {@link DataLoaderFactory} instead
97
100
*/
98
101
@ Deprecated
@@ -114,9 +117,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
114
117
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
115
118
* @param <K> the key type
116
119
* @param <V> the value type
117
- *
118
120
* @return a new DataLoader
119
- *
120
121
* @deprecated use {@link DataLoaderFactory} instead
121
122
*/
122
123
@ Deprecated
@@ -133,9 +134,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
133
134
* @param options the options to use
134
135
* @param <K> the key type
135
136
* @param <V> the value type
136
- *
137
137
* @return a new DataLoader
138
- *
139
138
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
140
139
* @deprecated use {@link DataLoaderFactory} instead
141
140
*/
@@ -151,9 +150,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
151
150
* @param batchLoadFunction the batch load function to use
152
151
* @param <K> the key type
153
152
* @param <V> the value type
154
- *
155
153
* @return a new DataLoader
156
- *
157
154
* @deprecated use {@link DataLoaderFactory} instead
158
155
*/
159
156
@ Deprecated
@@ -168,9 +165,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
168
165
* @param options the options to use
169
166
* @param <K> the key type
170
167
* @param <V> the value type
171
- *
172
168
* @return a new DataLoader
173
- *
174
169
* @deprecated use {@link DataLoaderFactory} instead
175
170
*/
176
171
@ Deprecated
@@ -192,9 +187,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
192
187
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
193
188
* @param <K> the key type
194
189
* @param <V> the value type
195
- *
196
190
* @return a new DataLoader
197
- *
198
191
* @deprecated use {@link DataLoaderFactory} instead
199
192
*/
200
193
@ Deprecated
@@ -211,9 +204,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
211
204
* @param options the options to use
212
205
* @param <K> the key type
213
206
* @param <V> the value type
214
- *
215
207
* @return a new DataLoader
216
- *
217
208
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
218
209
* @deprecated use {@link DataLoaderFactory} instead
219
210
*/
@@ -229,9 +220,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
229
220
* @param batchLoadFunction the batch load function to use
230
221
* @param <K> the key type
231
222
* @param <V> the value type
232
- *
233
223
* @return a new DataLoader
234
- *
235
224
* @deprecated use {@link DataLoaderFactory} instead
236
225
*/
237
226
@ Deprecated
@@ -246,9 +235,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
246
235
* @param options the options to use
247
236
* @param <K> the key type
248
237
* @param <V> the value type
249
- *
250
238
* @return a new DataLoader
251
- *
252
239
* @deprecated use {@link DataLoaderFactory} instead
253
240
*/
254
241
@ Deprecated
@@ -271,9 +258,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
271
258
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
272
259
* @param <K> the key type
273
260
* @param <V> the value type
274
- *
275
261
* @return a new DataLoader
276
- *
277
262
* @deprecated use {@link DataLoaderFactory} instead
278
263
*/
279
264
@ Deprecated
@@ -290,9 +275,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
290
275
* @param options the options to use
291
276
* @param <K> the key type
292
277
* @param <V> the value type
293
- *
294
278
* @return a new DataLoader
295
- *
296
279
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
297
280
* @deprecated use {@link DataLoaderFactory} instead
298
281
*/
@@ -308,9 +291,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
308
291
* @param batchLoadFunction the batch load function to use
309
292
* @param <K> the key type
310
293
* @param <V> the value type
311
- *
312
294
* @return a new DataLoader
313
- *
314
295
* @deprecated use {@link DataLoaderFactory} instead
315
296
*/
316
297
@ Deprecated
@@ -325,9 +306,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
325
306
* @param options the options to use
326
307
* @param <K> the key type
327
308
* @param <V> the value type
328
- *
329
309
* @return a new DataLoader
330
- *
331
310
* @deprecated use {@link DataLoaderFactory} instead
332
311
*/
333
312
@ Deprecated
@@ -349,9 +328,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
349
328
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
350
329
* @param <K> the key type
351
330
* @param <V> the value type
352
- *
353
331
* @return a new DataLoader
354
- *
355
332
* @deprecated use {@link DataLoaderFactory} instead
356
333
*/
357
334
@ Deprecated
@@ -368,9 +345,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
368
345
* @param options the options to use
369
346
* @param <K> the key type
370
347
* @param <V> the value type
371
- *
372
348
* @return a new DataLoader
373
- *
374
349
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
375
350
* @deprecated use {@link DataLoaderFactory} instead
376
351
*/
@@ -383,7 +358,6 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
383
358
* Creates a new data loader with the provided batch load function, and default options.
384
359
*
385
360
* @param batchLoadFunction the batch load function to use
386
- *
387
361
* @deprecated use {@link DataLoaderFactory} instead
388
362
*/
389
363
@ Deprecated
@@ -396,7 +370,6 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction) {
396
370
*
397
371
* @param batchLoadFunction the batch load function to use
398
372
* @param options the batch load options
399
- *
400
373
* @deprecated use {@link DataLoaderFactory} instead
401
374
*/
402
375
@ Deprecated
@@ -416,6 +389,8 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options
416
389
this .valueCache = determineValueCache (loaderOptions );
417
390
// order of keys matter in data loader
418
391
this .stats = nonNull (loaderOptions .getStatisticsCollector ());
392
+ this .batchLoadFunction = nonNull (batchLoadFunction );
393
+ this .options = loaderOptions ;
419
394
420
395
this .helper = new DataLoaderHelper <>(this , batchLoadFunction , loaderOptions , this .futureCache , this .valueCache , this .stats , clock );
421
396
}
@@ -431,6 +406,32 @@ private ValueCache<K, V> determineValueCache(DataLoaderOptions loaderOptions) {
431
406
return (ValueCache <K , V >) loaderOptions .valueCache ().orElseGet (ValueCache ::defaultValueCache );
432
407
}
433
408
409
+ /**
410
+ * @return the options used to build this {@link DataLoader}
411
+ */
412
+ public DataLoaderOptions getOptions () {
413
+ return options ;
414
+ }
415
+
416
+ /**
417
+ * @return the batch load interface used to build this {@link DataLoader}
418
+ */
419
+ public Object getBatchLoadFunction () {
420
+ return batchLoadFunction ;
421
+ }
422
+
423
+ /**
424
+ * This allows you to change the current {@link DataLoader} and turn it into a new one
425
+ *
426
+ * @param builderConsumer the {@link DataLoaderFactory.Builder} consumer for changing the {@link DataLoader}
427
+ * @return a newly built {@link DataLoader} instance
428
+ */
429
+ public DataLoader <K , V > transform (Consumer <DataLoaderFactory .Builder <K , V >> builderConsumer ) {
430
+ DataLoaderFactory .Builder <K , V > builder = DataLoaderFactory .builder (this );
431
+ builderConsumer .accept (builder );
432
+ return builder .build ();
433
+ }
434
+
434
435
/**
435
436
* This returns the last instant the data loader was dispatched. When the data loader is created this value is set to now.
436
437
*
@@ -457,7 +458,6 @@ public Duration getTimeSinceDispatch() {
457
458
* and returned from cache).
458
459
*
459
460
* @param key the key to load
460
- *
461
461
* @return the future of the value
462
462
*/
463
463
public CompletableFuture <V > load (K key ) {
@@ -475,7 +475,6 @@ public CompletableFuture<V> load(K key) {
475
475
* NOTE : This will NOT cause a data load to happen. You must call {@link #load(Object)} for that to happen.
476
476
*
477
477
* @param key the key to check
478
- *
479
478
* @return an Optional to the future of the value
480
479
*/
481
480
public Optional <CompletableFuture <V >> getIfPresent (K key ) {
@@ -494,7 +493,6 @@ public Optional<CompletableFuture<V>> getIfPresent(K key) {
494
493
* NOTE : This will NOT cause a data load to happen. You must call {@link #load(Object)} for that to happen.
495
494
*
496
495
* @param key the key to check
497
- *
498
496
* @return an Optional to the future of the value
499
497
*/
500
498
public Optional <CompletableFuture <V >> getIfCompleted (K key ) {
@@ -514,7 +512,6 @@ public Optional<CompletableFuture<V>> getIfCompleted(K key) {
514
512
*
515
513
* @param key the key to load
516
514
* @param keyContext a context object that is specific to this key
517
- *
518
515
* @return the future of the value
519
516
*/
520
517
public CompletableFuture <V > load (K key , Object keyContext ) {
@@ -530,7 +527,6 @@ public CompletableFuture<V> load(K key, Object keyContext) {
530
527
* and returned from cache).
531
528
*
532
529
* @param keys the list of keys to load
533
- *
534
530
* @return the composite future of the list of values
535
531
*/
536
532
public CompletableFuture <List <V >> loadMany (List <K > keys ) {
@@ -550,7 +546,6 @@ public CompletableFuture<List<V>> loadMany(List<K> keys) {
550
546
*
551
547
* @param keys the list of keys to load
552
548
* @param keyContexts the list of key calling context objects
553
- *
554
549
* @return the composite future of the list of values
555
550
*/
556
551
public CompletableFuture <List <V >> loadMany (List <K > keys , List <Object > keyContexts ) {
@@ -583,7 +578,6 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
583
578
* {@link org.dataloader.MappedBatchLoaderWithContext} to help retrieve data.
584
579
*
585
580
* @param keysAndContexts the map of keys to their respective contexts
586
- *
587
581
* @return the composite future of the map of keys and values
588
582
*/
589
583
public CompletableFuture <Map <K , V >> loadMany (Map <K , ?> keysAndContexts ) {
@@ -656,7 +650,6 @@ public int dispatchDepth() {
656
650
* on the next load request.
657
651
*
658
652
* @param key the key to remove
659
- *
660
653
* @return the data loader for fluent coding
661
654
*/
662
655
public DataLoader <K , V > clear (K key ) {
@@ -670,7 +663,6 @@ public DataLoader<K, V> clear(K key) {
670
663
*
671
664
* @param key the key to remove
672
665
* @param handler a handler that will be called after the async remote clear completes
673
- *
674
666
* @return the data loader for fluent coding
675
667
*/
676
668
public DataLoader <K , V > clear (K key , BiConsumer <Void , Throwable > handler ) {
@@ -696,7 +688,6 @@ public DataLoader<K, V> clearAll() {
696
688
* Clears the entire cache map of the loader, and of the cached value store.
697
689
*
698
690
* @param handler a handler that will be called after the async remote clear all completes
699
- *
700
691
* @return the data loader for fluent coding
701
692
*/
702
693
public DataLoader <K , V > clearAll (BiConsumer <Void , Throwable > handler ) {
@@ -714,7 +705,6 @@ public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
714
705
*
715
706
* @param key the key
716
707
* @param value the value
717
- *
718
708
* @return the data loader for fluent coding
719
709
*/
720
710
public DataLoader <K , V > prime (K key , V value ) {
@@ -726,7 +716,6 @@ public DataLoader<K, V> prime(K key, V value) {
726
716
*
727
717
* @param key the key
728
718
* @param error the exception to prime instead of a value
729
- *
730
719
* @return the data loader for fluent coding
731
720
*/
732
721
public DataLoader <K , V > prime (K key , Exception error ) {
@@ -740,7 +729,6 @@ public DataLoader<K, V> prime(K key, Exception error) {
740
729
*
741
730
* @param key the key
742
731
* @param value the value
743
- *
744
732
* @return the data loader for fluent coding
745
733
*/
746
734
public DataLoader <K , V > prime (K key , CompletableFuture <V > value ) {
@@ -760,7 +748,6 @@ public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
760
748
* If no cache key function is present in {@link DataLoaderOptions}, then the returned value equals the input key.
761
749
*
762
750
* @param key the input key
763
- *
764
751
* @return the cache key after the input is transformed with the cache key function
765
752
*/
766
753
public Object getCacheKey (K key ) {
@@ -779,6 +766,7 @@ public Statistics getStatistics() {
779
766
780
767
/**
781
768
* Gets the cacheMap associated with this data loader passed in via {@link DataLoaderOptions#cacheMap()}
769
+ *
782
770
* @return the cacheMap of this data loader
783
771
*/
784
772
public CacheMap <Object , V > getCacheMap () {
@@ -788,6 +776,7 @@ public CacheMap<Object, V> getCacheMap() {
788
776
789
777
/**
790
778
* Gets the valueCache associated with this data loader passed in via {@link DataLoaderOptions#valueCache()}
779
+ *
791
780
* @return the valueCache of this data loader
792
781
*/
793
782
public ValueCache <K , V > getValueCache () {
0 commit comments