org.apache.pulsar.shade.com.spotify.futures.CompletableFutures Maven / Gradle / Ivy
/*-
* -\-\-
* completable-futures
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package org.apache.pulsar.shade.com.spotify.futures;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
/**
* A collection of static utility methods that extend the
* {@link java.util.concurrent.CompletableFuture Java completable future} API.
*
* @since 0.1.0
*/
public final class CompletableFutures {
private CompletableFutures() {
throw new IllegalAccessError("This class must not be instantiated.");
}
/**
* Returns a new {@link CompletableFuture} which completes to a list of all values of its input
* stages, if all succeed. The list of results is in the same order as the input stages.
*
* As soon as any of the given stages complete exceptionally, then the returned future also does so,
* with a {@link CompletionException} holding this exception as its cause.
*
*
If no stages are provided, returns a future holding an empty list.
*
* @param stages the stages to combine
* @param the common super-type of all of the input stages, that determines the monomorphic
* type of the output future
* @return a future that completes to a list of the results of the supplied stages
* @throws NullPointerException if the stages list or any of its elements are {@code null}
* @since 0.1.0
*/
public static CompletableFuture> allAsList(
List extends CompletionStage extends T>> stages) {
// We use traditional for-loops instead of streams here for performance reasons,
// see AllAsListBenchmark
@SuppressWarnings("unchecked") // generic array creation
final CompletableFuture extends T>[] all = new CompletableFuture[stages.size()];
for (int i = 0; i < stages.size(); i++) {
all[i] = stages.get(i).toCompletableFuture();
}
CompletableFuture allOf = CompletableFuture.allOf(all);
for (int i = 0; i < all.length; i++) {
all[i].exceptionally(throwable -> {
if (!allOf.isDone()) {
allOf.completeExceptionally(throwable);
}
return null; // intentionally unused
});
}
return allOf
.thenApply(ignored -> {
final List result = new ArrayList<>(all.length);
for (int i = 0; i < all.length; i++) {
result.add(all[i].join());
}
return result;
});
}
/**
* Returns a new {@link CompletableFuture} which completes to a map of all values of its input
* stages, if all succeed.
*
* If any of the given stages complete exceptionally, then the returned future also does so,
* with a {@link CompletionException} holding this exception as its cause.
*
*
If no stages are provided, returns a future holding an empty map.
*
* @param map the map of stages to combine
* @param the common super-type of the keys
* @param the common super-type of all of the input value-stages, that determines the
* monomorphic type of the output future
* @return a future that completes to a map of the results of the supplied keys and value-stages
* @throws NullPointerException if value-stages or any of its elements are {@code null}
* @since 0.3.3
*/
public static CompletableFuture