Class DefaultAsyncFuture<T>
- java.lang.Object
-
- java.util.concurrent.CompletableFuture<T>
-
- com.linkedin.alpini.base.concurrency.impl.DefaultAsyncFuture<T>
-
- All Implemented Interfaces:
AsyncFuture<T>
,AsyncPromise<T>
,Time.Awaitable
,java.util.concurrent.CompletionStage<T>
,java.util.concurrent.Future<T>
- Direct Known Subclasses:
DefaultAsyncFutureTask
,DefaultCollectingAsyncFuture
public class DefaultAsyncFuture<T> extends java.util.concurrent.CompletableFuture<T> implements AsyncPromise<T>, Time.Awaitable
An implementation ofAsyncFuture
where much of the implementation is based upon netty'sorg.jboss.netty.channel.DefaultChannelFuture
.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.concurrent.CompletableFuture
java.util.concurrent.CompletableFuture.AsynchronousCompletionTask
-
Nested classes/interfaces inherited from interface com.linkedin.alpini.base.concurrency.AsyncFuture
AsyncFuture.Status
-
-
Field Summary
-
Fields inherited from interface com.linkedin.alpini.base.concurrency.AsyncFuture
NULL_SUCCESS
-
-
Constructor Summary
Constructors Constructor Description DefaultAsyncFuture(boolean cancellable)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AsyncPromise<T>
addListener(AsyncFutureListener<T> listener)
Adds the specified listener to this future.AsyncPromise<T>
addListener(AsyncPromise<T> listener)
Adds the specified future as a listener to this future.AsyncFuture<T>
await()
Waits for this future to be completed.boolean
await(long timeout, java.util.concurrent.TimeUnit unit)
Waits for this future to be completed within the specified time limit.AsyncFuture<T>
awaitUninterruptibly()
Waits for this future to be completed without interruption.boolean
awaitUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit)
Waits for this future to be completed within the specified time limit without interruption.boolean
cancel(boolean mayInterruptIfRunning)
boolean
complete(T value)
boolean
completeExceptionally(java.lang.Throwable ex)
T
get(long timeout, java.util.concurrent.TimeUnit unit)
java.lang.Throwable
getCause()
Returns the cause of the failed I/O operation if the I/O operation has failed.T
getNow()
Non-blocking variant ofFuture.get()
boolean
isSuccess()
Returnstrue
if and only if the I/O operation was completed successfully.boolean
setFailure(java.lang.Throwable t)
Marks this future as a failure and notifies all listeners.boolean
setSuccess(T result)
Marks this future as a success and notifies all listeners.-
Methods inherited from class java.util.concurrent.CompletableFuture
acceptEither, acceptEitherAsync, acceptEitherAsync, allOf, anyOf, applyToEither, applyToEitherAsync, applyToEitherAsync, completeAsync, completeAsync, completedFuture, completedStage, completeOnTimeout, copy, defaultExecutor, delayedExecutor, delayedExecutor, exceptionally, failedFuture, failedStage, get, getNow, getNumberOfDependents, handle, handleAsync, handleAsync, isCancelled, isCompletedExceptionally, isDone, join, minimalCompletionStage, newIncompleteFuture, obtrudeException, obtrudeValue, orTimeout, runAfterBoth, runAfterBothAsync, runAfterBothAsync, runAfterEither, runAfterEitherAsync, runAfterEitherAsync, runAsync, runAsync, supplyAsync, supplyAsync, thenAccept, thenAcceptAsync, thenAcceptAsync, thenAcceptBoth, thenAcceptBothAsync, thenAcceptBothAsync, thenApply, thenApplyAsync, thenApplyAsync, thenCombine, thenCombineAsync, thenCombineAsync, thenCompose, thenComposeAsync, thenComposeAsync, thenRun, thenRunAsync, thenRunAsync, toCompletableFuture, toString, whenComplete, whenCompleteAsync, whenCompleteAsync
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.linkedin.alpini.base.concurrency.AsyncPromise
setComplete
-
Methods inherited from interface java.util.concurrent.CompletionStage
acceptEither, acceptEitherAsync, acceptEitherAsync, applyToEither, applyToEitherAsync, applyToEitherAsync, exceptionally, handle, handleAsync, handleAsync, runAfterBoth, runAfterBothAsync, runAfterBothAsync, runAfterEither, runAfterEitherAsync, runAfterEitherAsync, thenAccept, thenAcceptAsync, thenAcceptAsync, thenAcceptBoth, thenAcceptBothAsync, thenAcceptBothAsync, thenApply, thenApplyAsync, thenApplyAsync, thenCombine, thenCombineAsync, thenCombineAsync, thenCompose, thenComposeAsync, thenComposeAsync, thenRun, thenRunAsync, thenRunAsync, toCompletableFuture, whenComplete, whenCompleteAsync, whenCompleteAsync
-
-
-
-
Method Detail
-
isSuccess
public final boolean isSuccess()
Description copied from interface:AsyncFuture
Returnstrue
if and only if the I/O operation was completed successfully.- Specified by:
isSuccess
in interfaceAsyncFuture<T>
- See Also:
AsyncFuture.isSuccess()
-
getCause
public final java.lang.Throwable getCause()
Description copied from interface:AsyncFuture
Returns the cause of the failed I/O operation if the I/O operation has failed.- Specified by:
getCause
in interfaceAsyncFuture<T>
- Returns:
- the cause of the failure.
null
if succeeded or this future is not completed yet. - See Also:
AsyncFuture.getCause()
-
setSuccess
public boolean setSuccess(T result)
Description copied from interface:AsyncPromise
Marks this future as a success and notifies all listeners.- Specified by:
setSuccess
in interfaceAsyncPromise<T>
- Returns:
true
if and only if successfully marked this future as a success. Otherwisefalse
because this future is already marked as either a success or a failure.- See Also:
AsyncPromise.setSuccess(Object)
-
setFailure
public boolean setFailure(@Nonnull java.lang.Throwable t)
Description copied from interface:AsyncPromise
Marks this future as a failure and notifies all listeners.- Specified by:
setFailure
in interfaceAsyncPromise<T>
- Returns:
true
if and only if successfully marked this future as a failure. Otherwisefalse
because this future is already marked as either a success or a failure.- See Also:
AsyncPromise.setFailure(Throwable)
-
addListener
@Nonnull public AsyncPromise<T> addListener(AsyncFutureListener<T> listener)
Description copied from interface:AsyncPromise
Adds the specified listener to this future. The specified listener is notified when this future is done. If this future is already completed, the specified listener is notified immediately.- Specified by:
addListener
in interfaceAsyncFuture<T>
- Specified by:
addListener
in interfaceAsyncPromise<T>
- Returns:
this
to permit chaining of operations.- See Also:
AsyncFuture.addListener(AsyncFutureListener)
-
addListener
@Nonnull public AsyncPromise<T> addListener(AsyncPromise<T> listener)
Adds the specified future as a listener to this future. The specified future is notified when this future is done. If this future is already completed, the specified future is notified immediately.- Specified by:
addListener
in interfaceAsyncFuture<T>
- Specified by:
addListener
in interfaceAsyncPromise<T>
- Parameters:
listener
-- Returns:
this
to permit chaining of operations.
-
await
@Nonnull public final AsyncFuture<T> await() throws java.lang.InterruptedException
Description copied from interface:AsyncFuture
Waits for this future to be completed.- Specified by:
await
in interfaceAsyncFuture<T>
- Throws:
java.lang.InterruptedException
- if the current thread was interrupted- See Also:
AsyncFuture.await()
-
awaitUninterruptibly
@Nonnull public final AsyncFuture<T> awaitUninterruptibly()
Description copied from interface:AsyncFuture
Waits for this future to be completed without interruption. This method catches anInterruptedException
and discards it silently.- Specified by:
awaitUninterruptibly
in interfaceAsyncFuture<T>
- See Also:
AsyncFuture.awaitUninterruptibly()
-
await
public final boolean await(long timeout, @Nonnull java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Description copied from interface:AsyncFuture
Waits for this future to be completed within the specified time limit.- Specified by:
await
in interfaceAsyncFuture<T>
- Specified by:
await
in interfaceTime.Awaitable
- Returns:
true
if and only if the future was completed within the specified time limit- Throws:
java.lang.InterruptedException
- if the current thread was interrupted- See Also:
AsyncFuture.await(long, java.util.concurrent.TimeUnit)
-
awaitUninterruptibly
public final boolean awaitUninterruptibly(long timeout, @Nonnull java.util.concurrent.TimeUnit unit)
Description copied from interface:AsyncFuture
Waits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedException
and discards it silently.- Specified by:
awaitUninterruptibly
in interfaceAsyncFuture<T>
- Returns:
true
if and only if the future was completed within the specified time limit- See Also:
AsyncFuture.awaitUninterruptibly(long, java.util.concurrent.TimeUnit)
-
get
public T get(long timeout, @Nonnull java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
-
complete
public final boolean complete(T value)
- Overrides:
complete
in classjava.util.concurrent.CompletableFuture<T>
-
completeExceptionally
public final boolean completeExceptionally(java.lang.Throwable ex)
- Overrides:
completeExceptionally
in classjava.util.concurrent.CompletableFuture<T>
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
-
getNow
public final T getNow()
Description copied from interface:AsyncFuture
Non-blocking variant ofFuture.get()
- Specified by:
getNow
in interfaceAsyncFuture<T>
- Returns:
- value or
null
-
-