| 1 | /* | |
| 2 | * Copyright 2014-2026 Grzegorz Piwowarek, https://4comprehension.com/ | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package com.pivovarit.collectors; | |
| 17 | ||
| 18 | import java.util.List; | |
| 19 | import java.util.Objects; | |
| 20 | import java.util.concurrent.CompletableFuture; | |
| 21 | import java.util.stream.Collector; | |
| 22 | import java.util.stream.Collectors; | |
| 23 | ||
| 24 | import static java.util.stream.Collectors.toList; | |
| 25 | ||
| 26 | /** | |
| 27 | * @author Grzegorz Piwowarek | |
| 28 | */ | |
| 29 | final class FutureCollectors { | |
| 30 | static <T, R> Collector<CompletableFuture<T>, ?, CompletableFuture<R>> toFuture(Collector<T, ?, R> collector) { | |
| 31 | Objects.requireNonNull(collector, "collector cannot be null"); | |
| 32 | ||
| 33 |
1
1. toFuture : replaced return value with null for com/pivovarit/collectors/FutureCollectors::toFuture → KILLED |
return Collectors.collectingAndThen(toList(), list -> { |
| 34 |
1
1. lambda$toFuture$1 : replaced return value with null for com/pivovarit/collectors/FutureCollectors::lambda$toFuture$1 → KILLED |
var future = CompletableFuture.allOf(list.toArray(CompletableFuture[]::new)) |
| 35 |
1
1. lambda$toFuture$2 : replaced return value with null for com/pivovarit/collectors/FutureCollectors::lambda$toFuture$2 → KILLED |
.thenApply(__ -> list.stream() |
| 36 | .map(CompletableFuture::join) | |
| 37 | .collect(collector)); | |
| 38 | ||
| 39 | for (var f : list) { | |
| 40 | f.whenComplete((__, throwable) -> { | |
| 41 |
1
1. lambda$toFuture$3 : negated conditional → KILLED |
if (throwable != null) { |
| 42 | future.completeExceptionally(throwable); | |
| 43 | } | |
| 44 | }); | |
| 45 | } | |
| 46 | ||
| 47 |
1
1. lambda$toFuture$0 : replaced return value with null for com/pivovarit/collectors/FutureCollectors::lambda$toFuture$0 → KILLED |
return future; |
| 48 | }); | |
| 49 | } | |
| 50 | ||
| 51 | static <T> Collector<CompletableFuture<T>, ?, CompletableFuture<List<T>>> toFuture() { | |
| 52 |
1
1. toFuture : replaced return value with null for com/pivovarit/collectors/FutureCollectors::toFuture → KILLED |
return toFuture(toList()); |
| 53 | } | |
| 54 | } | |
Mutations | ||
| 33 |
1.1 |
|
| 34 |
1.1 |
|
| 35 |
1.1 |
|
| 41 |
1.1 |
|
| 47 |
1.1 |
|
| 52 |
1.1 |