17
17
package org .dataloader ;
18
18
19
19
import org .dataloader .annotations .PublicApi ;
20
- import org .dataloader .impl .Assertions ;
21
20
import org .dataloader .instrumentation .DataLoaderInstrumentation ;
22
21
import org .dataloader .instrumentation .DataLoaderInstrumentationHelper ;
23
22
import org .dataloader .scheduler .BatchLoaderScheduler ;
24
23
import org .dataloader .stats .NoOpStatisticsCollector ;
25
24
import org .dataloader .stats .StatisticsCollector ;
26
25
26
+ import java .util .Objects ;
27
27
import java .util .Optional ;
28
+ import java .util .function .Consumer ;
28
29
import java .util .function .Supplier ;
29
30
30
31
import static org .dataloader .impl .Assertions .nonNull ;
31
32
32
33
/**
33
- * Configuration options for {@link DataLoader} instances.
34
+ * Configuration options for {@link DataLoader} instances. This is an immutable class so each time
35
+ * you change a value it returns a new object.
34
36
*
35
37
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
36
38
*/
37
39
@ PublicApi
38
40
public class DataLoaderOptions {
39
41
40
42
private static final BatchLoaderContextProvider NULL_PROVIDER = () -> null ;
41
-
42
- private boolean batchingEnabled ;
43
- private boolean cachingEnabled ;
44
- private boolean cachingExceptionsEnabled ;
45
- private CacheKey <?> cacheKeyFunction ;
46
- private CacheMap <?, ?> cacheMap ;
47
- private ValueCache <?, ?> valueCache ;
48
- private int maxBatchSize ;
49
- private Supplier <StatisticsCollector > statisticsCollector ;
50
- private BatchLoaderContextProvider environmentProvider ;
51
- private ValueCacheOptions valueCacheOptions ;
52
- private BatchLoaderScheduler batchLoaderScheduler ;
53
- private DataLoaderInstrumentation instrumentation ;
43
+ private static final Supplier <StatisticsCollector > NOOP_COLLECTOR = NoOpStatisticsCollector ::new ;
44
+ private static final ValueCacheOptions DEFAULT_VALUE_CACHE_OPTIONS = ValueCacheOptions .newOptions ();
45
+
46
+ private final boolean batchingEnabled ;
47
+ private final boolean cachingEnabled ;
48
+ private final boolean cachingExceptionsEnabled ;
49
+ private final CacheKey <?> cacheKeyFunction ;
50
+ private final CacheMap <?, ?> cacheMap ;
51
+ private final ValueCache <?, ?> valueCache ;
52
+ private final int maxBatchSize ;
53
+ private final Supplier <StatisticsCollector > statisticsCollector ;
54
+ private final BatchLoaderContextProvider environmentProvider ;
55
+ private final ValueCacheOptions valueCacheOptions ;
56
+ private final BatchLoaderScheduler batchLoaderScheduler ;
57
+ private final DataLoaderInstrumentation instrumentation ;
54
58
55
59
/**
56
60
* Creates a new data loader options with default settings.
@@ -59,14 +63,31 @@ public DataLoaderOptions() {
59
63
batchingEnabled = true ;
60
64
cachingEnabled = true ;
61
65
cachingExceptionsEnabled = true ;
66
+ cacheKeyFunction = null ;
67
+ cacheMap = null ;
68
+ valueCache = null ;
62
69
maxBatchSize = -1 ;
63
- statisticsCollector = NoOpStatisticsCollector :: new ;
70
+ statisticsCollector = NOOP_COLLECTOR ;
64
71
environmentProvider = NULL_PROVIDER ;
65
- valueCacheOptions = ValueCacheOptions . newOptions () ;
72
+ valueCacheOptions = DEFAULT_VALUE_CACHE_OPTIONS ;
66
73
batchLoaderScheduler = null ;
67
74
instrumentation = DataLoaderInstrumentationHelper .NOOP_INSTRUMENTATION ;
68
75
}
69
76
77
+ private DataLoaderOptions (Builder builder ) {
78
+ this .batchingEnabled = builder .batchingEnabled ;
79
+ this .cachingEnabled = builder .cachingEnabled ;
80
+ this .cachingExceptionsEnabled = builder .cachingExceptionsEnabled ;
81
+ this .cacheKeyFunction = builder .cacheKeyFunction ;
82
+ this .cacheMap = builder .cacheMap ;
83
+ this .valueCache = builder .valueCache ;
84
+ this .maxBatchSize = builder .maxBatchSize ;
85
+ this .statisticsCollector = builder .statisticsCollector ;
86
+ this .environmentProvider = builder .environmentProvider ;
87
+ this .valueCacheOptions = builder .valueCacheOptions ;
88
+ this .batchLoaderScheduler = builder .batchLoaderScheduler ;
89
+ }
90
+
70
91
/**
71
92
* Clones the provided data loader options.
72
93
*
@@ -95,6 +116,51 @@ public static DataLoaderOptions newOptions() {
95
116
return new DataLoaderOptions ();
96
117
}
97
118
119
+ /**
120
+ * @return a new default data loader options {@link Builder} that you can then customize
121
+ */
122
+ public static DataLoaderOptions .Builder newOptionsBuilder () {
123
+ return new DataLoaderOptions .Builder ();
124
+ }
125
+
126
+ /**
127
+ * @param otherOptions the options to copy
128
+ * @return a new default data loader options {@link Builder} from the specified one that you can then customize
129
+ */
130
+ public static DataLoaderOptions .Builder newDataLoaderOptions (DataLoaderOptions otherOptions ) {
131
+ return new DataLoaderOptions .Builder (otherOptions );
132
+ }
133
+
134
+ /**
135
+ * Will transform the current options in to a builder ands allow you to build a new set of options
136
+ *
137
+ * @param builderConsumer the consumer of a builder that has this objects starting values
138
+ * @return a new {@link DataLoaderOptions} object
139
+ */
140
+ public DataLoaderOptions transform (Consumer <Builder > builderConsumer ) {
141
+ Builder builder = newOptionsBuilder ();
142
+ builderConsumer .accept (builder );
143
+ return builder .build ();
144
+ }
145
+
146
+ @ Override
147
+ public boolean equals (Object o ) {
148
+ if (o == null || getClass () != o .getClass ()) return false ;
149
+ DataLoaderOptions that = (DataLoaderOptions ) o ;
150
+ return batchingEnabled == that .batchingEnabled
151
+ && cachingEnabled == that .cachingEnabled
152
+ && cachingExceptionsEnabled == that .cachingExceptionsEnabled
153
+ && maxBatchSize == that .maxBatchSize
154
+ && Objects .equals (cacheKeyFunction , that .cacheKeyFunction ) &&
155
+ Objects .equals (cacheMap , that .cacheMap ) &&
156
+ Objects .equals (valueCache , that .valueCache ) &&
157
+ Objects .equals (statisticsCollector , that .statisticsCollector ) &&
158
+ Objects .equals (environmentProvider , that .environmentProvider ) &&
159
+ Objects .equals (valueCacheOptions , that .valueCacheOptions ) &&
160
+ Objects .equals (batchLoaderScheduler , that .batchLoaderScheduler );
161
+ }
162
+
163
+
98
164
/**
99
165
* Option that determines whether to use batching (the default), or not.
100
166
*
@@ -111,8 +177,7 @@ public boolean batchingEnabled() {
111
177
* @return the data loader options for fluent coding
112
178
*/
113
179
public DataLoaderOptions setBatchingEnabled (boolean batchingEnabled ) {
114
- this .batchingEnabled = batchingEnabled ;
115
- return this ;
180
+ return builder ().setBatchingEnabled (batchingEnabled ).build ();
116
181
}
117
182
118
183
/**
@@ -131,8 +196,7 @@ public boolean cachingEnabled() {
131
196
* @return the data loader options for fluent coding
132
197
*/
133
198
public DataLoaderOptions setCachingEnabled (boolean cachingEnabled ) {
134
- this .cachingEnabled = cachingEnabled ;
135
- return this ;
199
+ return builder ().setCachingEnabled (cachingEnabled ).build ();
136
200
}
137
201
138
202
/**
@@ -156,8 +220,7 @@ public boolean cachingExceptionsEnabled() {
156
220
* @return the data loader options for fluent coding
157
221
*/
158
222
public DataLoaderOptions setCachingExceptionsEnabled (boolean cachingExceptionsEnabled ) {
159
- this .cachingExceptionsEnabled = cachingExceptionsEnabled ;
160
- return this ;
223
+ return builder ().setCachingExceptionsEnabled (cachingExceptionsEnabled ).build ();
161
224
}
162
225
163
226
/**
@@ -178,8 +241,7 @@ public Optional<CacheKey> cacheKeyFunction() {
178
241
* @return the data loader options for fluent coding
179
242
*/
180
243
public DataLoaderOptions setCacheKeyFunction (CacheKey <?> cacheKeyFunction ) {
181
- this .cacheKeyFunction = cacheKeyFunction ;
182
- return this ;
244
+ return builder ().setCacheKeyFunction (cacheKeyFunction ).build ();
183
245
}
184
246
185
247
/**
@@ -200,8 +262,7 @@ public DataLoaderOptions setCacheKeyFunction(CacheKey<?> cacheKeyFunction) {
200
262
* @return the data loader options for fluent coding
201
263
*/
202
264
public DataLoaderOptions setCacheMap (CacheMap <?, ?> cacheMap ) {
203
- this .cacheMap = cacheMap ;
204
- return this ;
265
+ return builder ().setCacheMap (cacheMap ).build ();
205
266
}
206
267
207
268
/**
@@ -222,8 +283,7 @@ public int maxBatchSize() {
222
283
* @return the data loader options for fluent coding
223
284
*/
224
285
public DataLoaderOptions setMaxBatchSize (int maxBatchSize ) {
225
- this .maxBatchSize = maxBatchSize ;
226
- return this ;
286
+ return builder ().setMaxBatchSize (maxBatchSize ).build ();
227
287
}
228
288
229
289
/**
@@ -242,8 +302,7 @@ public StatisticsCollector getStatisticsCollector() {
242
302
* @return the data loader options for fluent coding
243
303
*/
244
304
public DataLoaderOptions setStatisticsCollector (Supplier <StatisticsCollector > statisticsCollector ) {
245
- this .statisticsCollector = nonNull (statisticsCollector );
246
- return this ;
305
+ return builder ().setStatisticsCollector (nonNull (statisticsCollector )).build ();
247
306
}
248
307
249
308
/**
@@ -260,8 +319,7 @@ public BatchLoaderContextProvider getBatchLoaderContextProvider() {
260
319
* @return the data loader options for fluent coding
261
320
*/
262
321
public DataLoaderOptions setBatchLoaderContextProvider (BatchLoaderContextProvider contextProvider ) {
263
- this .environmentProvider = nonNull (contextProvider );
264
- return this ;
322
+ return builder ().setBatchLoaderContextProvider (nonNull (contextProvider )).build ();
265
323
}
266
324
267
325
/**
@@ -282,8 +340,7 @@ public DataLoaderOptions setBatchLoaderContextProvider(BatchLoaderContextProvide
282
340
* @return the data loader options for fluent coding
283
341
*/
284
342
public DataLoaderOptions setValueCache (ValueCache <?, ?> valueCache ) {
285
- this .valueCache = valueCache ;
286
- return this ;
343
+ return builder ().setValueCache (valueCache ).build ();
287
344
}
288
345
289
346
/**
@@ -300,8 +357,7 @@ public ValueCacheOptions getValueCacheOptions() {
300
357
* @return the data loader options for fluent coding
301
358
*/
302
359
public DataLoaderOptions setValueCacheOptions (ValueCacheOptions valueCacheOptions ) {
303
- this .valueCacheOptions = Assertions .nonNull (valueCacheOptions );
304
- return this ;
360
+ return builder ().setValueCacheOptions (nonNull (valueCacheOptions )).build ();
305
361
}
306
362
307
363
/**
@@ -319,8 +375,103 @@ public BatchLoaderScheduler getBatchLoaderScheduler() {
319
375
* @return the data loader options for fluent coding
320
376
*/
321
377
public DataLoaderOptions setBatchLoaderScheduler (BatchLoaderScheduler batchLoaderScheduler ) {
322
- this .batchLoaderScheduler = batchLoaderScheduler ;
323
- return this ;
378
+ return builder ().setBatchLoaderScheduler (batchLoaderScheduler ).build ();
379
+ }
380
+
381
+ private Builder builder () {
382
+ return new Builder (this );
383
+ }
384
+
385
+ public static class Builder {
386
+ private boolean batchingEnabled ;
387
+ private boolean cachingEnabled ;
388
+ private boolean cachingExceptionsEnabled ;
389
+ private CacheKey <?> cacheKeyFunction ;
390
+ private CacheMap <?, ?> cacheMap ;
391
+ private ValueCache <?, ?> valueCache ;
392
+ private int maxBatchSize ;
393
+ private Supplier <StatisticsCollector > statisticsCollector ;
394
+ private BatchLoaderContextProvider environmentProvider ;
395
+ private ValueCacheOptions valueCacheOptions ;
396
+ private BatchLoaderScheduler batchLoaderScheduler ;
397
+
398
+ public Builder () {
399
+ this (new DataLoaderOptions ()); // use the defaults of the DataLoaderOptions for this builder
400
+ }
401
+
402
+ Builder (DataLoaderOptions other ) {
403
+ this .batchingEnabled = other .batchingEnabled ;
404
+ this .cachingEnabled = other .cachingEnabled ;
405
+ this .cachingExceptionsEnabled = other .cachingExceptionsEnabled ;
406
+ this .cacheKeyFunction = other .cacheKeyFunction ;
407
+ this .cacheMap = other .cacheMap ;
408
+ this .valueCache = other .valueCache ;
409
+ this .maxBatchSize = other .maxBatchSize ;
410
+ this .statisticsCollector = other .statisticsCollector ;
411
+ this .environmentProvider = other .environmentProvider ;
412
+ this .valueCacheOptions = other .valueCacheOptions ;
413
+ this .batchLoaderScheduler = other .batchLoaderScheduler ;
414
+ }
415
+
416
+ public Builder setBatchingEnabled (boolean batchingEnabled ) {
417
+ this .batchingEnabled = batchingEnabled ;
418
+ return this ;
419
+ }
420
+
421
+ public Builder setCachingEnabled (boolean cachingEnabled ) {
422
+ this .cachingEnabled = cachingEnabled ;
423
+ return this ;
424
+ }
425
+
426
+ public Builder setCachingExceptionsEnabled (boolean cachingExceptionsEnabled ) {
427
+ this .cachingExceptionsEnabled = cachingExceptionsEnabled ;
428
+ return this ;
429
+ }
430
+
431
+ public Builder setCacheKeyFunction (CacheKey <?> cacheKeyFunction ) {
432
+ this .cacheKeyFunction = cacheKeyFunction ;
433
+ return this ;
434
+ }
435
+
436
+ public Builder setCacheMap (CacheMap <?, ?> cacheMap ) {
437
+ this .cacheMap = cacheMap ;
438
+ return this ;
439
+ }
440
+
441
+ public Builder setValueCache (ValueCache <?, ?> valueCache ) {
442
+ this .valueCache = valueCache ;
443
+ return this ;
444
+ }
445
+
446
+ public Builder setMaxBatchSize (int maxBatchSize ) {
447
+ this .maxBatchSize = maxBatchSize ;
448
+ return this ;
449
+ }
450
+
451
+ public Builder setStatisticsCollector (Supplier <StatisticsCollector > statisticsCollector ) {
452
+ this .statisticsCollector = statisticsCollector ;
453
+ return this ;
454
+ }
455
+
456
+ public Builder setBatchLoaderContextProvider (BatchLoaderContextProvider environmentProvider ) {
457
+ this .environmentProvider = environmentProvider ;
458
+ return this ;
459
+ }
460
+
461
+ public Builder setValueCacheOptions (ValueCacheOptions valueCacheOptions ) {
462
+ this .valueCacheOptions = valueCacheOptions ;
463
+ return this ;
464
+ }
465
+
466
+ public Builder setBatchLoaderScheduler (BatchLoaderScheduler batchLoaderScheduler ) {
467
+ this .batchLoaderScheduler = batchLoaderScheduler ;
468
+ return this ;
469
+ }
470
+
471
+ public DataLoaderOptions build () {
472
+ return new DataLoaderOptions (this );
473
+ }
474
+
324
475
}
325
476
326
477
/**
0 commit comments