Skip to content

Commit 5379982

Browse files
committed
Merged in master and tweaked immutable building
1 parent b80f57b commit 5379982

File tree

4 files changed

+61
-46
lines changed

4 files changed

+61
-46
lines changed

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

+36-29
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ private DataLoaderOptions(Builder builder) {
8686
this.environmentProvider = builder.environmentProvider;
8787
this.valueCacheOptions = builder.valueCacheOptions;
8888
this.batchLoaderScheduler = builder.batchLoaderScheduler;
89+
this.instrumentation = builder.instrumentation;
8990
}
9091

9192
/**
@@ -174,7 +175,7 @@ public boolean batchingEnabled() {
174175
* Sets the option that determines whether batch loading is enabled.
175176
*
176177
* @param batchingEnabled {@code true} to enable batch loading, {@code false} otherwise
177-
* @return the data loader options for fluent coding
178+
* @return a new data loader options instance for fluent coding
178179
*/
179180
public DataLoaderOptions setBatchingEnabled(boolean batchingEnabled) {
180181
return builder().setBatchingEnabled(batchingEnabled).build();
@@ -193,7 +194,7 @@ public boolean cachingEnabled() {
193194
* Sets the option that determines whether caching is enabled.
194195
*
195196
* @param cachingEnabled {@code true} to enable caching, {@code false} otherwise
196-
* @return the data loader options for fluent coding
197+
* @return a new data loader options instance for fluent coding
197198
*/
198199
public DataLoaderOptions setCachingEnabled(boolean cachingEnabled) {
199200
return builder().setCachingEnabled(cachingEnabled).build();
@@ -217,7 +218,7 @@ public boolean cachingExceptionsEnabled() {
217218
* Sets the option that determines whether exceptional values are cache enabled.
218219
*
219220
* @param cachingExceptionsEnabled {@code true} to enable caching exceptional values, {@code false} otherwise
220-
* @return the data loader options for fluent coding
221+
* @return a new data loader options instance for fluent coding
221222
*/
222223
public DataLoaderOptions setCachingExceptionsEnabled(boolean cachingExceptionsEnabled) {
223224
return builder().setCachingExceptionsEnabled(cachingExceptionsEnabled).build();
@@ -238,7 +239,7 @@ public Optional<CacheKey> cacheKeyFunction() {
238239
* Sets the function to use for creating the cache key, if caching is enabled.
239240
*
240241
* @param cacheKeyFunction the cache key function to use
241-
* @return the data loader options for fluent coding
242+
* @return a new data loader options instance for fluent coding
242243
*/
243244
public DataLoaderOptions setCacheKeyFunction(CacheKey<?> cacheKeyFunction) {
244245
return builder().setCacheKeyFunction(cacheKeyFunction).build();
@@ -259,7 +260,7 @@ public DataLoaderOptions setCacheKeyFunction(CacheKey<?> cacheKeyFunction) {
259260
* Sets the cache map implementation to use for caching, if caching is enabled.
260261
*
261262
* @param cacheMap the cache map instance
262-
* @return the data loader options for fluent coding
263+
* @return a new data loader options instance for fluent coding
263264
*/
264265
public DataLoaderOptions setCacheMap(CacheMap<?, ?> cacheMap) {
265266
return builder().setCacheMap(cacheMap).build();
@@ -280,7 +281,7 @@ public int maxBatchSize() {
280281
* before they are split into multiple class
281282
*
282283
* @param maxBatchSize the maximum batch size
283-
* @return the data loader options for fluent coding
284+
* @return a new data loader options instance for fluent coding
284285
*/
285286
public DataLoaderOptions setMaxBatchSize(int maxBatchSize) {
286287
return builder().setMaxBatchSize(maxBatchSize).build();
@@ -299,7 +300,7 @@ public StatisticsCollector getStatisticsCollector() {
299300
* a common value
300301
*
301302
* @param statisticsCollector the statistics collector to use
302-
* @return the data loader options for fluent coding
303+
* @return a new data loader options instance for fluent coding
303304
*/
304305
public DataLoaderOptions setStatisticsCollector(Supplier<StatisticsCollector> statisticsCollector) {
305306
return builder().setStatisticsCollector(nonNull(statisticsCollector)).build();
@@ -316,7 +317,7 @@ public BatchLoaderContextProvider getBatchLoaderContextProvider() {
316317
* Sets the batch loader environment provider that will be used to give context to batch load functions
317318
*
318319
* @param contextProvider the batch loader context provider
319-
* @return the data loader options for fluent coding
320+
* @return a new data loader options instance for fluent coding
320321
*/
321322
public DataLoaderOptions setBatchLoaderContextProvider(BatchLoaderContextProvider contextProvider) {
322323
return builder().setBatchLoaderContextProvider(nonNull(contextProvider)).build();
@@ -337,7 +338,7 @@ public DataLoaderOptions setBatchLoaderContextProvider(BatchLoaderContextProvide
337338
* Sets the value cache implementation to use for caching values, if caching is enabled.
338339
*
339340
* @param valueCache the value cache instance
340-
* @return the data loader options for fluent coding
341+
* @return a new data loader options instance for fluent coding
341342
*/
342343
public DataLoaderOptions setValueCache(ValueCache<?, ?> valueCache) {
343344
return builder().setValueCache(valueCache).build();
@@ -354,7 +355,7 @@ public ValueCacheOptions getValueCacheOptions() {
354355
* Sets the {@link ValueCacheOptions} that control how the {@link ValueCache} will be used
355356
*
356357
* @param valueCacheOptions the value cache options
357-
* @return the data loader options for fluent coding
358+
* @return a new data loader options instance for fluent coding
358359
*/
359360
public DataLoaderOptions setValueCacheOptions(ValueCacheOptions valueCacheOptions) {
360361
return builder().setValueCacheOptions(nonNull(valueCacheOptions)).build();
@@ -372,12 +373,29 @@ public BatchLoaderScheduler getBatchLoaderScheduler() {
372373
* to some future time.
373374
*
374375
* @param batchLoaderScheduler the scheduler
375-
* @return the data loader options for fluent coding
376+
* @return a new data loader options instance for fluent coding
376377
*/
377378
public DataLoaderOptions setBatchLoaderScheduler(BatchLoaderScheduler batchLoaderScheduler) {
378379
return builder().setBatchLoaderScheduler(batchLoaderScheduler).build();
379380
}
380381

382+
/**
383+
* @return the {@link DataLoaderInstrumentation} to use
384+
*/
385+
public DataLoaderInstrumentation getInstrumentation() {
386+
return instrumentation;
387+
}
388+
389+
/**
390+
* Sets in a new {@link DataLoaderInstrumentation}
391+
*
392+
* @param instrumentation the new {@link DataLoaderInstrumentation}
393+
* @return a new data loader options instance for fluent coding
394+
*/
395+
public DataLoaderOptions setInstrumentation(DataLoaderInstrumentation instrumentation) {
396+
return builder().setInstrumentation(instrumentation).build();
397+
}
398+
381399
private Builder builder() {
382400
return new Builder(this);
383401
}
@@ -394,6 +412,7 @@ public static class Builder {
394412
private BatchLoaderContextProvider environmentProvider;
395413
private ValueCacheOptions valueCacheOptions;
396414
private BatchLoaderScheduler batchLoaderScheduler;
415+
private DataLoaderInstrumentation instrumentation;
397416

398417
public Builder() {
399418
this(new DataLoaderOptions()); // use the defaults of the DataLoaderOptions for this builder
@@ -411,6 +430,7 @@ public Builder() {
411430
this.environmentProvider = other.environmentProvider;
412431
this.valueCacheOptions = other.valueCacheOptions;
413432
this.batchLoaderScheduler = other.batchLoaderScheduler;
433+
this.instrumentation = other.instrumentation;
414434
}
415435

416436
public Builder setBatchingEnabled(boolean batchingEnabled) {
@@ -468,27 +488,14 @@ public Builder setBatchLoaderScheduler(BatchLoaderScheduler batchLoaderScheduler
468488
return this;
469489
}
470490

491+
public Builder setInstrumentation(DataLoaderInstrumentation instrumentation) {
492+
this.instrumentation = nonNull(instrumentation);
493+
return this;
494+
}
495+
471496
public DataLoaderOptions build() {
472497
return new DataLoaderOptions(this);
473498
}
474499

475500
}
476-
477-
/**
478-
* @return the {@link DataLoaderInstrumentation} to use
479-
*/
480-
public DataLoaderInstrumentation getInstrumentation() {
481-
return instrumentation;
482-
}
483-
484-
/**
485-
* Sets in a new {@link DataLoaderInstrumentation}
486-
*
487-
* @param instrumentation the new {@link DataLoaderInstrumentation}
488-
* @return the data loader options for fluent coding
489-
*/
490-
public DataLoaderOptions setInstrumentation(DataLoaderInstrumentation instrumentation) {
491-
this.instrumentation = nonNull(instrumentation);
492-
return this;
493-
}
494501
}

src/main/java/org/dataloader/DataLoaderRegistry.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
* <p>
3232
* If the {@link DataLoader} has no {@link DataLoaderInstrumentation} then the registry one is added to it. If it does have one already
3333
* then a {@link ChainedDataLoaderInstrumentation} is created with the registry {@link DataLoaderInstrumentation} in it first and then any other
34-
* {@link DataLoaderInstrumentation}s added after that.
34+
* {@link DataLoaderInstrumentation}s added after that. If the registry {@link DataLoaderInstrumentation} instance and {@link DataLoader} {@link DataLoaderInstrumentation} instance
35+
* are the same object, then nothing is changed, since the same instrumentation code is being run.
3536
*/
3637
@PublicApi
3738
public class DataLoaderRegistry {
@@ -40,14 +41,14 @@ public class DataLoaderRegistry {
4041

4142

4243
public DataLoaderRegistry() {
43-
this(new ConcurrentHashMap<>(),null);
44+
this(new ConcurrentHashMap<>(), null);
4445
}
4546

4647
private DataLoaderRegistry(Builder builder) {
47-
this(builder.dataLoaders,builder.instrumentation);
48+
this(builder.dataLoaders, builder.instrumentation);
4849
}
4950

50-
protected DataLoaderRegistry(Map<String, DataLoader<?, ?>> dataLoaders, DataLoaderInstrumentation instrumentation ) {
51+
protected DataLoaderRegistry(Map<String, DataLoader<?, ?>> dataLoaders, DataLoaderInstrumentation instrumentation) {
5152
this.dataLoaders = instrumentDLs(dataLoaders, instrumentation);
5253
this.instrumentation = instrumentation;
5354
}
@@ -97,12 +98,16 @@ protected DataLoaderRegistry(Map<String, DataLoader<?, ?>> dataLoaders, DataLoa
9798
}
9899

99100
private static DataLoader<?, ?> mkInstrumentedDataLoader(DataLoader<?, ?> existingDL, DataLoaderOptions options, DataLoaderInstrumentation newInstrumentation) {
100-
return existingDL.transform(builder -> {
101-
options.setInstrumentation(newInstrumentation);
102-
builder.options(options);
103-
});
101+
return existingDL.transform(builder -> builder.options(setInInstrumentation(options, newInstrumentation)));
102+
}
103+
104+
private static DataLoaderOptions setInInstrumentation(DataLoaderOptions options, DataLoaderInstrumentation newInstrumentation) {
105+
return options.transform(optionsBuilder -> optionsBuilder.setInstrumentation(newInstrumentation));
104106
}
105107

108+
/**
109+
* @return the {@link DataLoaderInstrumentation} associated with this registry which can be null
110+
*/
106111
public DataLoaderInstrumentation getInstrumentation() {
107112
return instrumentation;
108113
}

src/test/java/org/dataloader/instrumentation/ChainedDataLoaderInstrumentationTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.util.concurrent.CompletableFuture;
1515

1616
import static org.awaitility.Awaitility.await;
17-
import static org.dataloader.DataLoaderOptions.newOptions;
17+
import static org.dataloader.DataLoaderOptions.newOptionsBuilder;
1818
import static org.hamcrest.MatcherAssert.assertThat;
1919
import static org.hamcrest.Matchers.equalTo;
2020

@@ -37,7 +37,7 @@ void canChainTogetherZeroInstrumentation() {
3737
// just to prove its useless but harmless
3838
ChainedDataLoaderInstrumentation chainedItn = new ChainedDataLoaderInstrumentation();
3939

40-
DataLoaderOptions options = newOptions().setInstrumentation(chainedItn);
40+
DataLoaderOptions options = newOptionsBuilder().setInstrumentation(chainedItn).build();
4141

4242
DataLoader<String, String> dl = DataLoaderFactory.newDataLoader(TestKit.keysAsValues(), options);
4343

@@ -57,7 +57,7 @@ void canChainTogetherOneInstrumentation() {
5757
ChainedDataLoaderInstrumentation chainedItn = new ChainedDataLoaderInstrumentation()
5858
.add(capturingA);
5959

60-
DataLoaderOptions options = newOptions().setInstrumentation(chainedItn);
60+
DataLoaderOptions options = newOptionsBuilder().setInstrumentation(chainedItn).build();
6161

6262
DataLoader<String, String> dl = DataLoaderFactory.newDataLoader(TestKit.keysAsValues(), options);
6363

@@ -83,7 +83,7 @@ public void canChainTogetherManyInstrumentationsWithDifferentBatchLoaders(TestDa
8383
.add(capturingB)
8484
.add(capturingButReturnsNull);
8585

86-
DataLoaderOptions options = newOptions().setInstrumentation(chainedItn);
86+
DataLoaderOptions options = newOptionsBuilder().setInstrumentation(chainedItn).build();
8787

8888
DataLoader<String, String> dl = factory.idLoader(options);
8989

src/test/java/org/dataloader/instrumentation/DataLoaderRegistryInstrumentationTest.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.dataloader.instrumentation;
22

33
import org.dataloader.DataLoader;
4+
import org.dataloader.DataLoaderOptions;
45
import org.dataloader.DataLoaderRegistry;
56
import org.dataloader.fixtures.TestKit;
67
import org.dataloader.fixtures.parameterized.TestDataLoaderFactory;
@@ -119,9 +120,9 @@ void wontDoAnyThingIfThereIsNoRegistryInstrumentation() {
119120

120121
@Test
121122
void wontDoAnyThingIfThereTheyAreTheSameInstrumentationAlready() {
122-
DataLoader<String, String> newX = dlX.transform(builder -> dlX.getOptions().setInstrumentation(instrA));
123-
DataLoader<String, String> newY = dlX.transform(builder -> dlY.getOptions().setInstrumentation(instrA));
124-
DataLoader<String, String> newZ = dlX.transform(builder -> dlY.getOptions().setInstrumentation(instrA));
123+
DataLoader<String, String> newX = dlX.transform(builder -> builder.options(dlX.getOptions().setInstrumentation(instrA)));
124+
DataLoader<String, String> newY = dlX.transform(builder -> builder.options(dlY.getOptions().setInstrumentation(instrA)));
125+
DataLoader<String, String> newZ = dlX.transform(builder -> builder.options(dlZ.getOptions().setInstrumentation(instrA)));
125126
DataLoaderRegistry registry = DataLoaderRegistry.newRegistry()
126127
.instrumentation(instrA)
127128
.register("X", newX)
@@ -144,7 +145,8 @@ void wontDoAnyThingIfThereTheyAreTheSameInstrumentationAlready() {
144145

145146
@Test
146147
void ifTheDLHasAInstrumentationThenItsTurnedIntoAChainedOne() {
147-
DataLoader<String, String> newX = dlX.transform(builder -> dlX.getOptions().setInstrumentation(instrA));
148+
DataLoaderOptions options = dlX.getOptions().setInstrumentation(instrA);
149+
DataLoader<String, String> newX = dlX.transform(builder -> builder.options(options));
148150

149151
DataLoaderRegistry registry = DataLoaderRegistry.newRegistry()
150152
.instrumentation(instrB)
@@ -162,7 +164,8 @@ void ifTheDLHasAInstrumentationThenItsTurnedIntoAChainedOne() {
162164

163165
@Test
164166
void chainedInstrumentationsWillBeCombined() {
165-
DataLoader<String, String> newX = dlX.transform(builder -> builder.options(dlX.getOptions().setInstrumentation(chainedInstrB)));
167+
DataLoaderOptions options = dlX.getOptions().setInstrumentation(chainedInstrB);
168+
DataLoader<String, String> newX = dlX.transform(builder -> builder.options(options));
166169

167170
DataLoaderRegistry registry = DataLoaderRegistry.newRegistry()
168171
.instrumentation(instrA)

0 commit comments

Comments
 (0)