io.appulse.utils.threads.FutureUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-java Show documentation
Show all versions of utils-java Show documentation
Utils for Appulse Java projects
/*
* Copyright 2019 the original author or authors.
*
* 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 io.appulse.utils.threads;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.stream.Stream;
import lombok.NonNull;
import lombok.val;
/**
* Different future helpers.
*
* @author Artem Labazin
* @since 1.11.0
*/
public final class FutureUtils {
/**
* Creates {@link CompletableFuture} instance and completes it with specified exception.
*
* @param throwable the exception
*
* @return completed exceptionally {@link CompletableFuture} instance.
*/
public static CompletableFuture completedExceptionally (Throwable throwable) {
val future = new CompletableFuture();
future.completeExceptionally(throwable);
return future;
}
/**
* Returns first completed future, which didn't throw an exception.
*
* @param futures array of futures.
*
* @return first non-exceptional completed future.
*/
public static CompletableFuture firstCompletedWithoutException (@NonNull CompletionStage extends T>... futures) {
val stream = Stream.of(futures);
return firstCompletedWithoutException(stream);
}
/**
* Returns first completed future, which didn't throw an exception.
*
* @param futures list of futures.
*
* @return first non-exceptional completed future.
*/
public static CompletableFuture firstCompletedWithoutException (@NonNull List extends CompletionStage extends T>> futures) {
val stream = futures.stream();
return firstCompletedWithoutException(stream);
}
/**
* Transforms standard stdlib's {@link Future} instance into {@link CompletableFuture}.
*
* @param future for transform.
*
* @return {@link CompletableFuture} instance.
*/
public static CompletableFuture toCompletableFuture (@NonNull Future future) {
return new CompletablePromise<>(future);
}
// https://stackoverflow.com/a/34163913
private static CompletableFuture firstCompletedWithoutException (Stream extends CompletionStage extends T>> stream) {
val result = new CompletableFuture();
val complete = (Consumer) result::complete;
val futures = stream
.map(it -> it.thenAccept(complete))
.toArray(CompletableFuture>[]::new);
CompletableFuture.allOf(futures)
.exceptionally(ex -> {
result.completeExceptionally(ex);
return null;
});
return result;
}
private FutureUtils () {
throw new UnsupportedOperationException();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy