com.google.common.util.concurrent
Class SettableFuture<V>

java.lang.Object
  extended by com.google.common.util.concurrent.AbstractFuture<V>
      extended by com.google.common.util.concurrent.AbstractListenableFuture<V>
          extended by com.google.common.util.concurrent.SettableFuture<V>
All Implemented Interfaces:
ListenableFuture<V>, Future<V>

@Beta
public final class SettableFuture<V>
extends AbstractListenableFuture<V>

A ListenableFuture whose result may be set by a set(Object) or setException(Throwable) call.

Since:
9 (in version 1 as ValueFuture)
Author:
Sven Mawson

Method Summary
 boolean cancel(boolean mayInterruptIfRunning)
          Attempts to cancel execution of this task.
static
<V> SettableFuture<V>
create()
          Creates a new SettableFuture in the default state.
 boolean set(V newValue)
          Sets the value of this future.
 boolean setException(Throwable t)
          Sets the future to having failed with the given exception.
 
Methods inherited from class com.google.common.util.concurrent.AbstractListenableFuture
addListener, done
 
Methods inherited from class com.google.common.util.concurrent.AbstractFuture
cancel, get, get, isCancelled, isDone
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.Future
get, get, isCancelled, isDone
 

Method Detail

create

public static <V> SettableFuture<V> create()
Creates a new SettableFuture in the default state.


set

public boolean set(@Nullable
                   V newValue)
Sets the value of this future. This method will return true if the value was successfully set, or false if the future has already been set or cancelled.

Overrides:
set in class AbstractFuture<V>
Parameters:
newValue - the value the future should hold.
Returns:
true if the value was successfully set.

setException

public boolean setException(Throwable t)
Sets the future to having failed with the given exception. This exception will be wrapped in an ExecutionException and thrown from the get methods. This method will return true if the exception was successfully set, or false if the future has already been set or cancelled.

Overrides:
setException in class AbstractFuture<V>
Parameters:
t - the exception the future should hold.
Returns:
true if the exception was successfully set.

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

After this method returns, subsequent calls to Future.isDone() will always return true. Subsequent calls to Future.isCancelled() will always return true if this method returned true.

A SettableFuture is never considered in the running state, so the mayInterruptIfRunning argument is ignored.

Specified by:
cancel in interface Future<V>
Overrides:
cancel in class AbstractFuture<V>
Parameters:
mayInterruptIfRunning - true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete
Returns:
false if the task could not be cancelled, typically because it has already completed normally; true otherwise