Skip to content

Commit b80f57b

Browse files
committed
Merge remote-tracking branch 'origin/master' into instrumentation-support
# Conflicts: # src/main/java/org/dataloader/DataLoaderOptions.java
2 parents 610343f + cf79290 commit b80f57b

File tree

3 files changed

+396
-39
lines changed

3 files changed

+396
-39
lines changed

src/main/java/org/dataloader/DataLoaderOptions.java

+190-39
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,44 @@
1717
package org.dataloader;
1818

1919
import org.dataloader.annotations.PublicApi;
20-
import org.dataloader.impl.Assertions;
2120
import org.dataloader.instrumentation.DataLoaderInstrumentation;
2221
import org.dataloader.instrumentation.DataLoaderInstrumentationHelper;
2322
import org.dataloader.scheduler.BatchLoaderScheduler;
2423
import org.dataloader.stats.NoOpStatisticsCollector;
2524
import org.dataloader.stats.StatisticsCollector;
2625

26+
import java.util.Objects;
2727
import java.util.Optional;
28+
import java.util.function.Consumer;
2829
import java.util.function.Supplier;
2930

3031
import static org.dataloader.impl.Assertions.nonNull;
3132

3233
/**
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.
3436
*
3537
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
3638
*/
3739
@PublicApi
3840
public class DataLoaderOptions {
3941

4042
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;
5458

5559
/**
5660
* Creates a new data loader options with default settings.
@@ -59,14 +63,31 @@ public DataLoaderOptions() {
5963
batchingEnabled = true;
6064
cachingEnabled = true;
6165
cachingExceptionsEnabled = true;
66+
cacheKeyFunction = null;
67+
cacheMap = null;
68+
valueCache = null;
6269
maxBatchSize = -1;
63-
statisticsCollector = NoOpStatisticsCollector::new;
70+
statisticsCollector = NOOP_COLLECTOR;
6471
environmentProvider = NULL_PROVIDER;
65-
valueCacheOptions = ValueCacheOptions.newOptions();
72+
valueCacheOptions = DEFAULT_VALUE_CACHE_OPTIONS;
6673
batchLoaderScheduler = null;
6774
instrumentation = DataLoaderInstrumentationHelper.NOOP_INSTRUMENTATION;
6875
}
6976

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+
7091
/**
7192
* Clones the provided data loader options.
7293
*
@@ -95,6 +116,51 @@ public static DataLoaderOptions newOptions() {
95116
return new DataLoaderOptions();
96117
}
97118

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+
98164
/**
99165
* Option that determines whether to use batching (the default), or not.
100166
*
@@ -111,8 +177,7 @@ public boolean batchingEnabled() {
111177
* @return the data loader options for fluent coding
112178
*/
113179
public DataLoaderOptions setBatchingEnabled(boolean batchingEnabled) {
114-
this.batchingEnabled = batchingEnabled;
115-
return this;
180+
return builder().setBatchingEnabled(batchingEnabled).build();
116181
}
117182

118183
/**
@@ -131,8 +196,7 @@ public boolean cachingEnabled() {
131196
* @return the data loader options for fluent coding
132197
*/
133198
public DataLoaderOptions setCachingEnabled(boolean cachingEnabled) {
134-
this.cachingEnabled = cachingEnabled;
135-
return this;
199+
return builder().setCachingEnabled(cachingEnabled).build();
136200
}
137201

138202
/**
@@ -156,8 +220,7 @@ public boolean cachingExceptionsEnabled() {
156220
* @return the data loader options for fluent coding
157221
*/
158222
public DataLoaderOptions setCachingExceptionsEnabled(boolean cachingExceptionsEnabled) {
159-
this.cachingExceptionsEnabled = cachingExceptionsEnabled;
160-
return this;
223+
return builder().setCachingExceptionsEnabled(cachingExceptionsEnabled).build();
161224
}
162225

163226
/**
@@ -178,8 +241,7 @@ public Optional<CacheKey> cacheKeyFunction() {
178241
* @return the data loader options for fluent coding
179242
*/
180243
public DataLoaderOptions setCacheKeyFunction(CacheKey<?> cacheKeyFunction) {
181-
this.cacheKeyFunction = cacheKeyFunction;
182-
return this;
244+
return builder().setCacheKeyFunction(cacheKeyFunction).build();
183245
}
184246

185247
/**
@@ -200,8 +262,7 @@ public DataLoaderOptions setCacheKeyFunction(CacheKey<?> cacheKeyFunction) {
200262
* @return the data loader options for fluent coding
201263
*/
202264
public DataLoaderOptions setCacheMap(CacheMap<?, ?> cacheMap) {
203-
this.cacheMap = cacheMap;
204-
return this;
265+
return builder().setCacheMap(cacheMap).build();
205266
}
206267

207268
/**
@@ -222,8 +283,7 @@ public int maxBatchSize() {
222283
* @return the data loader options for fluent coding
223284
*/
224285
public DataLoaderOptions setMaxBatchSize(int maxBatchSize) {
225-
this.maxBatchSize = maxBatchSize;
226-
return this;
286+
return builder().setMaxBatchSize(maxBatchSize).build();
227287
}
228288

229289
/**
@@ -242,8 +302,7 @@ public StatisticsCollector getStatisticsCollector() {
242302
* @return the data loader options for fluent coding
243303
*/
244304
public DataLoaderOptions setStatisticsCollector(Supplier<StatisticsCollector> statisticsCollector) {
245-
this.statisticsCollector = nonNull(statisticsCollector);
246-
return this;
305+
return builder().setStatisticsCollector(nonNull(statisticsCollector)).build();
247306
}
248307

249308
/**
@@ -260,8 +319,7 @@ public BatchLoaderContextProvider getBatchLoaderContextProvider() {
260319
* @return the data loader options for fluent coding
261320
*/
262321
public DataLoaderOptions setBatchLoaderContextProvider(BatchLoaderContextProvider contextProvider) {
263-
this.environmentProvider = nonNull(contextProvider);
264-
return this;
322+
return builder().setBatchLoaderContextProvider(nonNull(contextProvider)).build();
265323
}
266324

267325
/**
@@ -282,8 +340,7 @@ public DataLoaderOptions setBatchLoaderContextProvider(BatchLoaderContextProvide
282340
* @return the data loader options for fluent coding
283341
*/
284342
public DataLoaderOptions setValueCache(ValueCache<?, ?> valueCache) {
285-
this.valueCache = valueCache;
286-
return this;
343+
return builder().setValueCache(valueCache).build();
287344
}
288345

289346
/**
@@ -300,8 +357,7 @@ public ValueCacheOptions getValueCacheOptions() {
300357
* @return the data loader options for fluent coding
301358
*/
302359
public DataLoaderOptions setValueCacheOptions(ValueCacheOptions valueCacheOptions) {
303-
this.valueCacheOptions = Assertions.nonNull(valueCacheOptions);
304-
return this;
360+
return builder().setValueCacheOptions(nonNull(valueCacheOptions)).build();
305361
}
306362

307363
/**
@@ -319,8 +375,103 @@ public BatchLoaderScheduler getBatchLoaderScheduler() {
319375
* @return the data loader options for fluent coding
320376
*/
321377
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+
324475
}
325476

326477
/**

0 commit comments

Comments
 (0)