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

java.lang.Object
  extended by com.google.common.collect.ForwardingObject
      extended by com.google.common.util.concurrent.ForwardingFuture<V>
All Implemented Interfaces:
Future<V>
Direct Known Subclasses:
ForwardingFuture.SimpleForwardingFuture, ForwardingListenableFuture

public abstract class ForwardingFuture<V>
extends ForwardingObject
implements Future<V>

A ForwardingFuture.SimpleForwardingFuture which forwards all its method calls to another future. Subclasses should override one or more methods to modify the behavior of the backing future as desired per the decorator pattern.

Most subclasses can just use ForwardingFuture.SimpleForwardingFuture.

Since:
1
Author:
Sven Mawson

Nested Class Summary
static class ForwardingFuture.SimpleForwardingFuture<V>
          A simplified version of ForwardingFuture where subclasses can pass in an already constructed Future as the delegate.
 
Constructor Summary
protected ForwardingFuture()
          Constructor for use by subclasses.
 
Method Summary
 boolean cancel(boolean mayInterruptIfRunning)
          Attempts to cancel execution of this task.
protected abstract  Future<V> delegate()
          Returns the backing delegate instance that methods are forwarded to.
 V get()
          Waits if necessary for the computation to complete, and then retrieves its result.
 V get(long timeout, TimeUnit unit)
          Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
 boolean isCancelled()
          Returns true if this task was cancelled before it completed normally.
 boolean isDone()
          Returns true if this task completed.
 
Methods inherited from class com.google.common.collect.ForwardingObject
toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ForwardingFuture

protected ForwardingFuture()
Constructor for use by subclasses.

Method Detail

delegate

protected abstract Future<V> delegate()
Description copied from class: ForwardingObject
Returns the backing delegate instance that methods are forwarded to. Abstract subclasses generally override this method with an abstract method that has a more specific return type, such as ForwardingSet.delegate(). Concrete subclasses override this method to supply the instance being decorated.

Specified by:
delegate in class ForwardingObject

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Description copied from interface: java.util.concurrent.Future
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.

Specified by:
cancel in interface Future<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

isCancelled

public boolean isCancelled()
Description copied from interface: java.util.concurrent.Future
Returns true if this task was cancelled before it completed normally.

Specified by:
isCancelled in interface Future<V>
Returns:
true if this task was cancelled before it completed

isDone

public boolean isDone()
Description copied from interface: java.util.concurrent.Future
Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.

Specified by:
isDone in interface Future<V>
Returns:
true if this task completed

get

public V get()
      throws InterruptedException,
             ExecutionException
Description copied from interface: java.util.concurrent.Future
Waits if necessary for the computation to complete, and then retrieves its result.

Specified by:
get in interface Future<V>
Returns:
the computed result
Throws:
InterruptedException - if the current thread was interrupted while waiting
ExecutionException - if the computation threw an exception

get

public V get(long timeout,
             TimeUnit unit)
      throws InterruptedException,
             ExecutionException,
             TimeoutException
Description copied from interface: java.util.concurrent.Future
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

Specified by:
get in interface Future<V>
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
the computed result
Throws:
InterruptedException - if the current thread was interrupted while waiting
ExecutionException - if the computation threw an exception
TimeoutException - if the wait timed out