1 | package org.cardanofoundation.explorer.api.util; | |
2 | ||
3 | import java.math.BigInteger; | |
4 | import java.util.ArrayList; | |
5 | import java.util.Collection; | |
6 | import java.util.List; | |
7 | import java.util.Objects; | |
8 | import java.util.concurrent.CompletableFuture; | |
9 | import java.util.concurrent.ExecutionException; | |
10 | import java.util.function.Function; | |
11 | ||
12 | import lombok.experimental.UtilityClass; | |
13 | import lombok.extern.slf4j.Slf4j; | |
14 | ||
15 | @UtilityClass | |
16 | @Slf4j | |
17 | public class BatchUtils { | |
18 | ||
19 | public static <T> BigInteger processInBatches( | |
20 | int batchSize, | |
21 | List<T> itemList, | |
22 | Function<Collection<T>, CompletableFuture<BigInteger>> processingFunction) { | |
23 | ||
24 | List<CompletableFuture<BigInteger>> futures = new ArrayList<>(); | |
25 | final int COLLECTION_SIZE = itemList.size(); | |
26 |
3
1. processInBatches : changed conditional boundary → SURVIVED 2. processInBatches : Replaced integer addition with subtraction → KILLED 3. processInBatches : negated conditional → KILLED |
for (int startBatchIdx = 0; startBatchIdx < COLLECTION_SIZE; startBatchIdx += batchSize) { |
27 |
1
1. processInBatches : Replaced integer addition with subtraction → KILLED |
int endBatchIdx = Math.min(startBatchIdx + batchSize, COLLECTION_SIZE); |
28 | final List<T> batchList = itemList.subList(startBatchIdx, endBatchIdx); | |
29 | futures.add(processingFunction.apply(batchList)); | |
30 | } | |
31 | ||
32 | CompletableFuture<BigInteger> allFutures = | |
33 | CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) | |
34 | .thenApply( | |
35 | v -> | |
36 | futures.stream() | |
37 | .map(CompletableFuture::join) | |
38 | .filter(Objects::nonNull) | |
39 | .reduce(BigInteger::add) | |
40 |
1
1. lambda$processInBatches$0 : replaced return value with null for org/cardanofoundation/explorer/api/util/BatchUtils::lambda$processInBatches$0 → KILLED |
.orElse(BigInteger.ZERO)); |
41 | ||
42 | try { | |
43 |
1
1. processInBatches : replaced return value with null for org/cardanofoundation/explorer/api/util/BatchUtils::processInBatches → KILLED |
return allFutures.get(); |
44 | } catch (InterruptedException | ExecutionException e) { | |
45 | return null; | |
46 | } | |
47 | } | |
48 | } | |
Mutations | ||
26 |
1.1 2.2 3.3 |
|
27 |
1.1 |
|
40 |
1.1 |
|
43 |
1.1 |