Update CompletableFutureUtils.java

This commit is contained in:
Andrea Cavalli 2020-10-27 16:27:12 +01:00
parent c1a8a23961
commit 8813ef3e88

View File

@ -28,6 +28,28 @@ import org.warp.commonutils.type.ScoredValue;
public class CompletableFutureUtils {
/**
* Safely get a CompletableFuture asynchronously
*/
public static <T> CompletableFuture<T> getCompletableFutureAsync(Supplier<CompletableFuture<T>> completableFutureSupplier, Executor executor) {
CompletableFuture<T> cf = new CompletableFuture<>();
executor.execute(() -> {
try {
var cf2 = completableFutureSupplier.get();
cf2.whenComplete((result, error) -> {
if (error == null) {
cf.complete(result);
} else {
cf.completeExceptionally(error);
}
});
} catch (Exception ex) {
cf.completeExceptionally(ex);
}
});
return cf;
}
/**
* Safely get a CompletableFuture or a FailedFuture
*/