V
- The result type returned by the Future's get
method.final class AsyncSettableFuture<V> extends ForwardingListenableFuture<V>
setFuture(com.google.common.util.concurrent.ListenableFuture<? extends V>)
.
A similar effect could be accomplished by adding a listener to the delegate
future that sets a normal settable future after the delegate is complete.
This approach gains us the ability to keep track of whether a delegate has
been set (i.e. so that we can prevent collisions from setting it twice and
can know before the computation is done whether it has been set), as well
as improved cancellation semantics (i.e. if either future is cancelled,
then the other one is too). This class is thread-safe.Modifier and Type | Class and Description |
---|---|
private static class |
AsyncSettableFuture.NestedFuture<V> |
ForwardingListenableFuture.SimpleForwardingListenableFuture<V>
ForwardingFuture.SimpleForwardingFuture<V>
Modifier and Type | Field and Description |
---|---|
private ListenableFuture<V> |
dereferenced |
private AsyncSettableFuture.NestedFuture<V> |
nested |
Modifier | Constructor and Description |
---|---|
private |
AsyncSettableFuture() |
Modifier and Type | Method and Description |
---|---|
static <V> AsyncSettableFuture<V> |
create()
Creates a new asynchronously-settable future.
|
protected ListenableFuture<V> |
delegate()
Returns the backing delegate instance that methods are forwarded to.
|
boolean |
isSet()
Returns
true if this future has been (possibly asynchronously) set. |
boolean |
setException(java.lang.Throwable exception)
Convenience method that calls
setFuture(com.google.common.util.concurrent.ListenableFuture<? extends V>) on a Futures.immediateFailedFuture(java.lang.Throwable) . |
boolean |
setFuture(ListenableFuture<? extends V> future)
Sets this future to forward to the given future.
|
boolean |
setValue(V value)
Convenience method that calls
setFuture(com.google.common.util.concurrent.ListenableFuture<? extends V>) on a Futures.immediateFuture(V) . |
addListener
cancel, get, get, isCancelled, isDone
toString
private final AsyncSettableFuture.NestedFuture<V> nested
private final ListenableFuture<V> dereferenced
public static <V> AsyncSettableFuture<V> create()
protected ListenableFuture<V> delegate()
ForwardingObject
ForwardingSet.delegate()
. Concrete subclasses override this method to supply
the instance being decorated.delegate
in class ForwardingListenableFuture<V>
public boolean setFuture(ListenableFuture<? extends V> future)
true
if the future was able to be set (i.e. it hasn't been set already).public boolean setValue(@Nullable V value)
setFuture(com.google.common.util.concurrent.ListenableFuture<? extends V>)
on a Futures.immediateFuture(V)
. Returns true
if the future
was able to be set (i.e. it hasn't been set already).public boolean setException(java.lang.Throwable exception)
setFuture(com.google.common.util.concurrent.ListenableFuture<? extends V>)
on a Futures.immediateFailedFuture(java.lang.Throwable)
. Returns true
if the
future was able to be set (i.e. it hasn't been set already).public boolean isSet()
true
if this future has been (possibly asynchronously) set.
Note that a false
result in no way gaurantees that a later call
to, e.g., setFuture(com.google.common.util.concurrent.ListenableFuture<? extends V>)
will succeed, since another thread could
make the call in between. This is somewhat analogous to ForwardingFuture.isDone()
,
but since setting and completing are not the same event, it is useful to
have this method broken out.