Class ForwardingFuture<V>
- java.lang.Object
-
- com.google.common.collect.ForwardingObject
-
- com.google.common.util.concurrent.ForwardingFuture<V>
-
- All Implemented Interfaces:
Future<V>
- Direct Known Subclasses:
ForwardingFuture.SimpleForwardingFuture
,ForwardingListenableFuture
@CanIgnoreReturnValue @GwtCompatible public abstract class ForwardingFuture<V> extends ForwardingObject implements Future<V>
AFuture
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.0
- Author:
- Sven Mawson
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ForwardingFuture.SimpleForwardingFuture<V>
A simplified version ofForwardingFuture
where subclasses can pass in an already constructedFuture
as the delegate.
-
Constructor Summary
Constructors Modifier Constructor Description protected
ForwardingFuture()
Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task.protected abstract Future<? extends 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()
Returnstrue
if this task was cancelled before it completed normally.boolean
isDone()
Returnstrue
if this task completed.-
Methods inherited from class com.google.common.collect.ForwardingObject
toString
-
-
-
-
Constructor Detail
-
ForwardingFuture
protected ForwardingFuture()
Constructor for use by subclasses.
-
-
Method Detail
-
delegate
protected abstract Future<? extends 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 asForwardingSet.delegate()
. Concrete subclasses override this method to supply the instance being decorated.- Specified by:
delegate
in classForwardingObject
-
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 whencancel
is called, this task should never run. If the task has already started, then themayInterruptIfRunning
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 returntrue
. Subsequent calls toFuture.isCancelled()
will always returntrue
if this method returnedtrue
.- Specified by:
cancel
in interfaceFuture<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
Returnstrue
if this task was cancelled before it completed normally.- Specified by:
isCancelled
in interfaceFuture<V>
- Returns:
true
if this task was cancelled before it completed
-
isDone
public boolean isDone()
Description copied from interface:java.util.concurrent.Future
Returnstrue
if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will returntrue
.
-
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 interfaceFuture<V>
- Returns:
- the computed result
- Throws:
InterruptedException
- if the current thread was interrupted while waitingExecutionException
- 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 interfaceFuture<V>
- Parameters:
timeout
- the maximum time to waitunit
- the time unit of the timeout argument- Returns:
- the computed result
- Throws:
InterruptedException
- if the current thread was interrupted while waitingExecutionException
- if the computation threw an exceptionTimeoutException
- if the wait timed out
-
-