Skip to content

Commit da7df70

Browse files
committed
minor code cleanups
- missing Thread.currentThread().interrupt(); after catching InterruptedException - converting Object.nonNull to Assertions.nonNull - removing some vestigial @SuppressWarnings("unchecked")
1 parent 96adddc commit da7df70

File tree

6 files changed

+19
-25
lines changed

6 files changed

+19
-25
lines changed

src/main/java/org/dataloader/BatchLoaderEnvironment.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.dataloader;
22

3+
import org.dataloader.impl.Assertions;
4+
35
import java.util.ArrayList;
46
import java.util.Collections;
57
import java.util.HashMap;
68
import java.util.List;
79
import java.util.Map;
810

9-
import static java.util.Objects.nonNull;
10-
1111
/**
1212
* This object is passed to a batch loader as calling context. It could contain security credentials
1313
* of the calling users for example or database parameters that allow the data layer call to succeed.
@@ -78,8 +78,8 @@ public Builder context(Object context) {
7878
}
7979

8080
public <K> Builder keyContexts(List<K> keys, List<Object> keyContexts) {
81-
nonNull(keys);
82-
nonNull(keyContexts);
81+
Assertions.nonNull(keys);
82+
Assertions.nonNull(keyContexts);
8383

8484
Map<Object, Object> map = new HashMap<>();
8585
List<Object> list = new ArrayList<>();

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
public class DataLoader<K, V> {
5959

6060
private final DataLoaderHelper<K, V> helper;
61-
private final DataLoaderOptions loaderOptions;
6261
private final CacheMap<Object, CompletableFuture<V>> futureCache;
6362
private final StatisticsCollector stats;
6463

@@ -246,7 +245,6 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
246245
* @return a new DataLoader
247246
* @see #newDataLoaderWithTry(BatchLoader)
248247
*/
249-
@SuppressWarnings("unchecked")
250248
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
251249
return new DataLoader<>(batchLoadFunction, options);
252250
}
@@ -309,7 +307,6 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
309307
* @return a new DataLoader
310308
* @see #newDataLoaderWithTry(BatchLoader)
311309
*/
312-
@SuppressWarnings("unchecked")
313310
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
314311
return new DataLoader<>(batchLoadFunction, options);
315312
}
@@ -334,12 +331,12 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options
334331
}
335332

336333
private DataLoader(Object batchLoadFunction, DataLoaderOptions options) {
337-
this.loaderOptions = options == null ? new DataLoaderOptions() : options;
334+
DataLoaderOptions loaderOptions = options == null ? new DataLoaderOptions() : options;
338335
this.futureCache = determineCacheMap(loaderOptions);
339336
// order of keys matter in data loader
340-
this.stats = nonNull(this.loaderOptions.getStatisticsCollector());
337+
this.stats = nonNull(loaderOptions.getStatisticsCollector());
341338

342-
this.helper = new DataLoaderHelper<>(this, batchLoadFunction, this.loaderOptions, this.futureCache, this.stats);
339+
this.helper = new DataLoaderHelper<>(this, batchLoadFunction, loaderOptions, this.futureCache, this.stats);
343340
}
344341

345342
@SuppressWarnings("unchecked")
@@ -496,10 +493,8 @@ public DispatchResult<V> dispatchWithCounts() {
496493
* @return the list of all results when the {@link #dispatchDepth()} reached 0
497494
*/
498495
public List<V> dispatchAndJoin() {
499-
List<V> results = new ArrayList<>();
500-
501496
List<V> joinedResults = dispatch().join();
502-
results.addAll(joinedResults);
497+
List<V> results = new ArrayList<>(joinedResults);
503498
while (this.dispatchDepth() > 0) {
504499
joinedResults = dispatch().join();
505500
results.addAll(joinedResults);

src/main/java/org/dataloader/DataLoaderHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
class DataLoaderHelper<K, V> {
3131

3232

33-
class LoaderQueueEntry<K, V> {
33+
static class LoaderQueueEntry<K, V> {
3434

3535
final K key;
3636
final V value;
@@ -151,7 +151,7 @@ DispatchResult<V> dispatch() {
151151
loaderQueue.clear();
152152
}
153153
if (!batchingEnabled || keys.isEmpty()) {
154-
return new DispatchResult<V>(CompletableFuture.completedFuture(emptyList()), 0);
154+
return new DispatchResult<>(CompletableFuture.completedFuture(emptyList()), 0);
155155
}
156156
final int totalEntriesHandled = keys.size();
157157
//
@@ -172,7 +172,7 @@ DispatchResult<V> dispatch() {
172172
} else {
173173
futureList = dispatchQueueBatch(keys, callContexts, queuedFutures);
174174
}
175-
return new DispatchResult<V>(futureList, totalEntriesHandled);
175+
return new DispatchResult<>(futureList, totalEntriesHandled);
176176
}
177177

178178
private CompletableFuture<List<V>> sliceIntoBatchesOfBatches(List<K> keys, List<CompletableFuture<V>> queuedFutures, List<Object> callContexts, int maxBatchSize) {
@@ -194,7 +194,7 @@ private CompletableFuture<List<V>> sliceIntoBatchesOfBatches(List<K> keys, List<
194194
}
195195
//
196196
// now reassemble all the futures into one that is the complete set of results
197-
return CompletableFuture.allOf(allBatches.toArray(new CompletableFuture[allBatches.size()]))
197+
return CompletableFuture.allOf(allBatches.toArray(new CompletableFuture[0]))
198198
.thenApply(v -> allBatches.stream()
199199
.map(CompletableFuture::join)
200200
.flatMap(Collection::stream)

src/main/java/org/dataloader/impl/CompletableFutureKit.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ public static <V> CompletableFuture<V> failedFuture(Exception e) {
2020
return future;
2121
}
2222

23-
public static Throwable cause(CompletableFuture completableFuture) {
23+
public static <V> Throwable cause(CompletableFuture<V> completableFuture) {
2424
if (!completableFuture.isCompletedExceptionally()) {
2525
return null;
2626
}
2727
try {
2828
completableFuture.get();
2929
return null;
3030
} catch (InterruptedException e) {
31+
Thread.currentThread().interrupt();
3132
return e;
3233
} catch (ExecutionException e) {
3334
Throwable cause = e.getCause();
@@ -38,11 +39,11 @@ public static Throwable cause(CompletableFuture completableFuture) {
3839
}
3940
}
4041

41-
public static boolean succeeded(CompletableFuture future) {
42+
public static <V> boolean succeeded(CompletableFuture<V> future) {
4243
return future.isDone() && !future.isCompletedExceptionally();
4344
}
4445

45-
public static boolean failed(CompletableFuture future) {
46+
public static <V> boolean failed(CompletableFuture<V> future) {
4647
return future.isDone() && future.isCompletedExceptionally();
4748
}
4849

src/main/java/org/dataloader/impl/DefaultCacheMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
@Internal
3434
public class DefaultCacheMap<U, V> implements CacheMap<U, V> {
3535

36-
private Map<U, V> cache;
36+
private final Map<U, V> cache;
3737

3838
/**
3939
* Default constructor

src/main/java/org/dataloader/impl/PromisedValuesImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ public class PromisedValuesImpl<T> implements PromisedValues<T> {
2020

2121
private final List<? extends CompletionStage<T>> futures;
2222
private final CompletionStage<Void> controller;
23-
private AtomicReference<Throwable> cause;
23+
private final AtomicReference<Throwable> cause;
2424

2525
private PromisedValuesImpl(List<? extends CompletionStage<T>> cs) {
2626
this.futures = nonNull(cs);
2727
this.cause = new AtomicReference<>();
28-
List<CompletableFuture> cfs = cs.stream().map(CompletionStage::toCompletableFuture).collect(Collectors.toList());
29-
CompletableFuture[] futuresArray = cfs.toArray(new CompletableFuture[cfs.size()]);
28+
CompletableFuture[] futuresArray = cs.stream().map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new);
3029
this.controller = CompletableFuture.allOf(futuresArray).handle((result, throwable) -> {
3130
setCause(throwable);
3231
return null;
@@ -104,7 +103,6 @@ public Throwable cause(int index) {
104103
}
105104

106105
@Override
107-
@SuppressWarnings("unchecked")
108106
public T get(int index) {
109107
assertState(isDone(), "The PromisedValues MUST be complete before calling the get() method");
110108
try {

0 commit comments

Comments
 (0)