Created
November 17, 2016 09:54
-
-
Save mathieuancelin/804425260a50de8fa90f91fd72e73889 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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