- java.lang.Object
-
- org.xnio.AbstractIoFuture<T>
-
- Type Parameters:
T- the type of result that this operation produces
- All Implemented Interfaces:
Cancellable,IoFuture<T>
- Direct Known Subclasses:
FailedIoFuture
public abstract class AbstractIoFuture<T> extends java.lang.Object implements IoFuture<T>
An abstract base class forIoFutureobjects. Used to easily produce implementations.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.xnio.IoFuture
IoFuture.HandlingNotifier<T,A>, IoFuture.Notifier<T,A>, IoFuture.Status
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractIoFuture()Construct a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddCancelHandler(Cancellable cancellable)Add a cancellation handler.<A> IoFuture<T>addNotifier(IoFuture.Notifier<? super T,A> notifier, A attachment)Add a notifier to be called when this operation is complete.IoFuture.Statusawait()Wait for the operation to complete.IoFuture.Statusawait(long time, java.util.concurrent.TimeUnit timeUnit)Wait for the operation to complete, with a timeout.IoFuture.StatusawaitInterruptibly()Wait for the operation to complete.IoFuture.StatusawaitInterruptibly(long time, java.util.concurrent.TimeUnit timeUnit)Wait for the operation to complete, with a timeout.IoFuture<T>cancel()Cancel an operation.Tget()Get the result of the operation.java.io.IOExceptiongetException()Get the failure reason.TgetInterruptibly()Get the result of the operation.protected java.util.concurrent.ExecutorgetNotifierExecutor()Get the executor used to run asynchronous notifiers.IoFuture.StatusgetStatus()Get the current status.protected voidrunNotifier(java.lang.Runnable runnable)Run a notifier.protected booleansetCancelled()Acknowledge the cancellation of this operation.protected booleansetException(java.io.IOException exception)Set the exception for this operation.protected booleansetResult(T result)Set the result for this operation.
-
-
-
Method Detail
-
getStatus
public IoFuture.Status getStatus()
Get the current status.
-
await
public IoFuture.Status await()
Wait for the operation to complete. This method will block until the status changes fromIoFuture.Status.WAITING.
-
await
public IoFuture.Status await(long time, java.util.concurrent.TimeUnit timeUnit)
Wait for the operation to complete, with a timeout. This method will block until the status changes fromIoFuture.Status.WAITING, or the given time elapses. If the time elapses before the operation is complete,IoFuture.Status.WAITINGis returned.- Specified by:
awaitin interfaceIoFuture<T>- Parameters:
time- the amount of time to waittimeUnit- the time unit- Returns:
- the new status, or
IoFuture.Status.WAITINGif the timeout expired
-
awaitInterruptibly
public IoFuture.Status awaitInterruptibly() throws java.lang.InterruptedException
Wait for the operation to complete. This method will block until the status changes fromIoFuture.Status.WAITING, or the current thread is interrupted.- Specified by:
awaitInterruptiblyin interfaceIoFuture<T>- Returns:
- the new status
- Throws:
java.lang.InterruptedException- if the operation is interrupted
-
awaitInterruptibly
public IoFuture.Status awaitInterruptibly(long time, java.util.concurrent.TimeUnit timeUnit) throws java.lang.InterruptedException
Wait for the operation to complete, with a timeout. This method will block until the status changes fromIoFuture.Status.WAITING, the given time elapses, or the current thread is interrupted. If the time elapses before the operation is complete,IoFuture.Status.WAITINGis returned.- Specified by:
awaitInterruptiblyin interfaceIoFuture<T>- Parameters:
time- the amount of time to waittimeUnit- the time unit- Returns:
- the new status, or
IoFuture.Status.WAITINGif the timeout expired - Throws:
java.lang.InterruptedException- if the operation is interrupted
-
get
public T get() throws java.io.IOException, java.util.concurrent.CancellationException
Get the result of the operation. If the operation is not complete, blocks until the operation completes. If the operation fails, or has already failed at the time this method is called, the failure reason is thrown.
-
getInterruptibly
public T getInterruptibly() throws java.io.IOException, java.lang.InterruptedException, java.util.concurrent.CancellationException
Get the result of the operation. If the operation is not complete, blocks until the operation completes. If the operation fails, or has already failed at the time this method is called, the failure reason is thrown. If the current thread is interrupted while waiting, an exception is thrown.- Specified by:
getInterruptiblyin interfaceIoFuture<T>- Returns:
- the result of the operation
- Throws:
java.io.IOException- if the operation failedjava.lang.InterruptedException- if the operation is interruptedjava.util.concurrent.CancellationException- if the operation was cancelled
-
getException
public java.io.IOException getException() throws java.lang.IllegalStateExceptionGet the failure reason.- Specified by:
getExceptionin interfaceIoFuture<T>- Returns:
- the failure reason
- Throws:
java.lang.IllegalStateException- if the operation did not fail
-
addNotifier
public <A> IoFuture<T> addNotifier(IoFuture.Notifier<? super T,A> notifier, A attachment)
Add a notifier to be called when this operation is complete. If the operation is already complete, the notifier is called immediately, possibly in the caller's thread. The given attachment is provided to the notifier.- Specified by:
addNotifierin interfaceIoFuture<T>- Type Parameters:
A- the attachment type- Parameters:
notifier- the notifier to be calledattachment- the attachment to pass in to the notifier- Returns:
- this instance
-
setException
protected boolean setException(java.io.IOException exception)
Set the exception for this operation. Any threads blocking on this instance will be unblocked.- Parameters:
exception- the exception to set- Returns:
falseif the operation was already completed,trueotherwise
-
setResult
protected boolean setResult(T result)
Set the result for this operation. Any threads blocking on this instance will be unblocked.- Parameters:
result- the result to set- Returns:
falseif the operation was already completed,trueotherwise
-
setCancelled
protected boolean setCancelled()
Acknowledge the cancellation of this operation.- Returns:
falseif the operation was already completed,trueotherwise
-
cancel
public IoFuture<T> cancel()
Cancel an operation. The actual cancel may be synchronous or asynchronous. Implementers will use this method to initiate the cancel; use thesetCancelled()method to indicate that the cancel was successful. The default implementation calls any registered cancel handlers.- Specified by:
cancelin interfaceCancellable- Specified by:
cancelin interfaceIoFuture<T>- Returns:
- this
IoFutureinstance
-
addCancelHandler
protected void addCancelHandler(Cancellable cancellable)
Add a cancellation handler. The argument will be cancelled whenever thisIoFutureis cancelled. If theIoFutureis already cancelled when this method is called, the handler will be called directly.- Parameters:
cancellable- the cancel handler
-
runNotifier
protected void runNotifier(java.lang.Runnable runnable)
Run a notifier. Implementors will run the notifier, preferably in another thread. The default implementation runs the notifier using theExecutorretrieved viagetNotifierExecutor().- Parameters:
runnable- the runnable task
-
getNotifierExecutor
protected java.util.concurrent.Executor getNotifierExecutor()
Get the executor used to run asynchronous notifiers. By default, this implementation simply returns the direct executor.- Returns:
- the executor to use
-
-