Skip to content

Instantly share code, notes, and snippets.

@mathieuancelin
Created November 17, 2016 09:54
Show Gist options
  • Select an option

  • Save mathieuancelin/804425260a50de8fa90f91fd72e73889 to your computer and use it in GitHub Desktop.

Select an option

Save mathieuancelin/804425260a50de8fa90f91fd72e73889 to your computer and use it in GitHub Desktop.
interface Future<T> {
/**
* Creates a {@code Future} with the given java.util.concurrent.CompletableFuture, backed by the {@link #DEFAULT_EXECUTOR_SERVICE}
*
* @param completableFuture A {@link java.util.concurrent.CompletableFuture}
* @param <T> Result type of the Future
* @return A new {@code Future} wrapping the result of the Java future
* @throws NullPointerException if future is null
*/
static <T> Future<T> fromJavaCompletableFuture(CompletableFuture<T> completableFuture) {
return fromJavaCompletionStage(DEFAULT_EXECUTOR_SERVICE, completableFuture);
}
/**
* Creates a {@code Future} with the given java.util.concurrent.CompletionStage, backed by the {@link #DEFAULT_EXECUTOR_SERVICE}
*
* @param completionStage A {@link java.util.concurrent.CompletionStage}
* @param <T> Result type of the Future
* @return A new {@code Future} wrapping the result of the Java future
* @throws NullPointerException if future is null
*/
static <T> Future<T> fromJavaCompletionStage(CompletionStage<T> completionStage) {
return fromJavaCompletionStage(DEFAULT_EXECUTOR_SERVICE, completionStage);
}
/**
* Creates a {@code Future} with the given java.util.concurrent.CompletableFuture, backed by given {@link ExecutorService}
*
* @param executorService An {@link ExecutorService}
* @param completableFuture A {@link java.util.concurrent.CompletableFuture}
* @param <T> Result type of the Future
* @return A new {@code Future} wrapping the result of the Java future
* @throws NullPointerException if executorService or future is null
*/
static <T> Future<T> fromJavaCompletableFuture(ExecutorService executorService, CompletableFuture<T> completableFuture) {
return fromJavaCompletionStage(executorService, completableFuture);
}
/**
* Creates a {@code Future} with the given java.util.concurrent.CompletionStage, backed by given {@link ExecutorService}
*
* @param executorService An {@link ExecutorService}
* @param completionStage A {@link java.util.concurrent.CompletionStage}
* @param <T> Result type of the Future
* @return A new {@code Future} wrapping the result of the Java future
* @throws NullPointerException if executorService or future is null
*/
static <T> Future<T> fromJavaCompletionStage(ExecutorService executorService, CompletionStage<T> completionStage) {
Objects.requireNonNull(executorService, "executorService is null");
Objects.requireNonNull(completionStage, "completionStage is null");
Promise<T> promise = Promise.make(executorService);
completionStage.whenCompleteAsync((value, exception) -> {
if (value != null) {
promise.trySuccess(value);
} else if (exception != null) {
promise.tryFailure(exception);
} else {
promise.tryFailure(new RuntimeException("CompletionStage completed without result nor exception"));
}
}, executorService);
return promise.future();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment