diff options
Diffstat (limited to 'jetty-util/src/main/java/org/eclipse/jetty/util/Promise.java')
-rw-r--r-- | jetty-util/src/main/java/org/eclipse/jetty/util/Promise.java | 69 |
1 files changed, 62 insertions, 7 deletions
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/Promise.java b/jetty-util/src/main/java/org/eclipse/jetty/util/Promise.java index 30ed7f11be..a20c0d18de 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/Promise.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/Promise.java @@ -18,6 +18,8 @@ package org.eclipse.jetty.util; +import java.util.concurrent.CompletableFuture; + import org.eclipse.jetty.util.log.Log; /** @@ -33,25 +35,28 @@ public interface Promise<C> * @param result the context * @see #failed(Throwable) */ - public abstract void succeeded(C result); + default void succeeded(C result) + { + } /** * <p>Callback invoked when the operation fails.</p> * * @param x the reason for the operation failure */ - public void failed(Throwable x); - + default void failed(Throwable x) + { + } /** - * <p>Empty implementation of {@link Promise}</p> + * <p>Empty implementation of {@link Promise}.</p> * - * @param <C> the type of the context object + * @param <U> the type of the result */ - public static class Adapter<C> implements Promise<C> + class Adapter<U> implements Promise<U> { @Override - public void succeeded(C result) + public void succeeded(U result) { } @@ -62,4 +67,54 @@ public interface Promise<C> } } + /** + * <p>Creates a promise from the given incomplete CompletableFuture.</p> + * <p>When the promise completes, either succeeding or failing, the + * CompletableFuture is also completed, respectively via + * {@link CompletableFuture#complete(Object)} or + * {@link CompletableFuture#completeExceptionally(Throwable)}.</p> + * + * @param completable the CompletableFuture to convert into a promise + * @return a promise that when completed, completes the given CompletableFuture + */ + static <T> Promise<T> from(CompletableFuture<? super T> completable) + { + if (completable instanceof Promise) + return (Promise<T>)completable; + + return new Promise<T>() + { + @Override + public void succeeded(T result) + { + completable.complete(result); + } + + @Override + public void failed(Throwable x) + { + completable.completeExceptionally(x); + } + }; + } + + /** + * <p>A CompletableFuture that is also a Promise.</p> + * + * @param <S> the type of the result + */ + class Completable<S> extends CompletableFuture<S> implements Promise<S> + { + @Override + public void succeeded(S result) + { + complete(result); + } + + @Override + public void failed(Throwable x) + { + completeExceptionally(x); + } + } } |