diff options
Diffstat (limited to 'jetty-util/src/main/java/org/eclipse/jetty/util/Callback.java')
-rw-r--r-- | jetty-util/src/main/java/org/eclipse/jetty/util/Callback.java | 100 |
1 files changed, 89 insertions, 11 deletions
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/Callback.java b/jetty-util/src/main/java/org/eclipse/jetty/util/Callback.java index 8c8f543bc8..b32d6260f0 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/Callback.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/Callback.java @@ -18,6 +18,8 @@ package org.eclipse.jetty.util; +import java.util.concurrent.CompletableFuture; + /** * <p>A callback abstraction that handles completed/failed events of asynchronous operations.</p> * @@ -30,8 +32,7 @@ public interface Callback * Instance of Adapter that can be used when the callback methods need an empty * implementation without incurring in the cost of allocating a new Adapter object. */ - static Callback NOOP = new Callback(){}; - + Callback NOOP = new Callback(){}; /** * <p>Callback invoked when the operation completes.</p> @@ -55,25 +56,102 @@ public interface Callback { return false; } - - + + /** + * <p>Creates a non-blocking callback from the given incomplete CompletableFuture.</p> + * <p>When the callback 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 callback + * @return a callback that when completed, completes the given CompletableFuture + */ + static Callback from(CompletableFuture<?> completable) + { + return from(completable, false); + } + + /** + * <p>Creates a callback from the given incomplete CompletableFuture, + * with the given {@code blocking} characteristic.</p> + * + * @param completable the CompletableFuture to convert into a callback + * @param blocking whether the callback is blocking + * @return a callback that when completed, completes the given CompletableFuture + */ + static Callback from(CompletableFuture<?> completable, boolean blocking) + { + if (completable instanceof Callback) + return (Callback)completable; + + return new Callback() + { + @Override + public void succeeded() + { + completable.complete(null); + } + + @Override + public void failed(Throwable x) + { + completable.completeExceptionally(x); + } + + @Override + public boolean isNonBlocking() + { + return !blocking; + } + }; + } + /** * Callback interface that declares itself as non-blocking */ interface NonBlocking extends Callback { @Override - public default boolean isNonBlocking() + default boolean isNonBlocking() { return true; } } - - + /** - * <p>Empty implementation of {@link Callback}</p> + * <p>A CompletableFuture that is also a Callback.</p> */ - @Deprecated - static class Adapter implements Callback - {} + class Completable extends CompletableFuture<Void> implements Callback + { + private final boolean blocking; + + public Completable() + { + this(false); + } + + public Completable(boolean blocking) + { + this.blocking = blocking; + } + + @Override + public void succeeded() + { + complete(null); + } + + @Override + public void failed(Throwable x) + { + completeExceptionally(x); + } + + @Override + public boolean isNonBlocking() + { + return !blocking; + } + } } |