public class SettableListenableFuture<T> extends java.lang.Object implements ListenableFuture<T>
ListenableFuture
whose value can be set via set(Object)
or
setException(Throwable)
. It may also be cancelled.
Inspired by com.google.common.util.concurrent.SettableFuture
.
Modifier and Type | Class and Description |
---|---|
private static class |
SettableListenableFuture.SettableTask<T> |
Modifier and Type | Field and Description |
---|---|
private ListenableFutureTask<T> |
listenableFuture |
private SettableListenableFuture.SettableTask<T> |
settableTask |
Constructor and Description |
---|
SettableListenableFuture() |
Modifier and Type | Method and Description |
---|---|
void |
addCallback(ListenableFutureCallback<? super T> callback)
Registers the given callback to this
ListenableFuture . |
void |
addCallback(SuccessCallback<? super T> successCallback,
FailureCallback failureCallback)
Registers the given success and failure callbacks to this
ListenableFuture . |
boolean |
cancel(boolean mayInterruptIfRunning) |
T |
get()
Retrieve the value.
|
T |
get(long timeout,
java.util.concurrent.TimeUnit unit)
Retrieve the value.
|
protected void |
interruptTask()
Subclasses can override this method to implement interruption of the future's
computation.
|
boolean |
isCancelled() |
boolean |
isDone() |
boolean |
set(T value)
Set the value of this future.
|
boolean |
setException(java.lang.Throwable exception)
Set the exception of this future.
|
private final SettableListenableFuture.SettableTask<T> settableTask
private final ListenableFutureTask<T> listenableFuture
public boolean set(T value)
true
if
the value was set successfully, or false
if the future has already
been set or cancelled.value
- the value that will be set.true
if the value was successfully set, else false
.public boolean setException(java.lang.Throwable exception)
true
if
the exception was set successfully, or false
if the future has already
been set or cancelled.exception
- the value that will be set.true
if the exception was successfully set, else false
.public void addCallback(ListenableFutureCallback<? super T> callback)
ListenableFuture
ListenableFuture
. The callback will
be triggered when this Future
is complete or, if it is already complete,
immediately.addCallback
in interface ListenableFuture<T>
callback
- the callback to registerpublic void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback)
ListenableFuture
ListenableFuture
.
The callback will be triggered when this Future
is complete or, if it is
already complete immediately. This is a Java 8 lambdas compliant alternative to
ListenableFuture.addCallback(ListenableFutureCallback)
.addCallback
in interface ListenableFuture<T>
successCallback
- the success callback to registerfailureCallback
- the failure callback to registerpublic boolean cancel(boolean mayInterruptIfRunning)
cancel
in interface java.util.concurrent.Future<T>
public boolean isCancelled()
isCancelled
in interface java.util.concurrent.Future<T>
public boolean isDone()
isDone
in interface java.util.concurrent.Future<T>
public T get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
Will return the value if it has been set via set(Object)
,
throw an ExecutionException
if it has been
set via setException(Throwable)
or throw a
CancellationException
if it has been cancelled.
get
in interface java.util.concurrent.Future<T>
java.lang.InterruptedException
java.util.concurrent.ExecutionException
public T get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
Will return the value if it has been set via set(Object)
,
throw an ExecutionException
if it has been
set via setException(Throwable)
or throw a
CancellationException
if it has been cancelled.
get
in interface java.util.concurrent.Future<T>
timeout
- the maximum time to wait.unit
- the time unit of the timeout argument.java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException
protected void interruptTask()
cancel(true)
.
The default implementation does nothing.