Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.common.util;
import org.apache.pulsar.shade.com.google.common.util.concurrent.MoreExecutors;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.pulsar.shade.javax.annotation.Nonnull;
import org.apache.pulsar.shade.javax.annotation.concurrent.ThreadSafe;
/**
* This class is aimed at simplifying work with {@code CompletableFuture}.
*/
public class FutureUtil {
/**
* Return a future that represents the completion of the futures in the provided Collection.
*
* @param futures futures to wait for
* @return a new CompletableFuture that is completed when all of the given CompletableFutures complete
*/
public static CompletableFuture waitForAll(Collection extends CompletableFuture>> futures) {
if (futures == null || futures.isEmpty()) {
return CompletableFuture.completedFuture(null);
}
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
}
public static CompletableFuture runWithCurrentThread(Runnable runnable) {
return CompletableFuture.runAsync(
() -> runnable.run(), MoreExecutors.directExecutor());
}
public static CompletableFuture> waitForAll(Stream>> futures) {
return futures.reduce(CompletableFuture.completedFuture(new ArrayList<>()),
(pre, curr) -> pre.thenCompose(preV -> curr.thenApply(currV -> {
preV.addAll(currV);
return preV;
})));
}
/**
* Make the dest future complete after another one. {@param dest} is will be completed with the same value as
* {@param src}, or be completed with the same error as {@param src}.
*/
public static void completeAfter(final CompletableFuture dest, CompletableFuture src) {
src.whenComplete((v, ex) -> {
if (ex != null) {
dest.completeExceptionally(ex);
} else {
dest.complete(v);
}
});
}
/**
* Make the dest future complete after others. {@param dest} is will be completed with a {@link Void} value
* if all the futures of {@param src} is completed, or be completed exceptionally with the same error as the first
* one completed exceptionally future of {@param src}.
*/
public static void completeAfterAll(final CompletableFuture dest,
CompletableFuture extends Object>... src) {
FutureUtil.waitForAll(Arrays.asList(src)).whenComplete((ignore, ex) -> {
if (ex != null) {
dest.completeExceptionally(ex);
} else {
dest.complete(null);
}
});
}
/**
* Return a future that represents the completion of any future in the provided Collection.
*
* @param futures futures to wait any
* @return a new CompletableFuture that is completed when any of the given CompletableFutures complete
*/
public static CompletableFuture