|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.common.util.concurrent.AbstractCheckedFuture<V,X>
@Beta public abstract class AbstractCheckedFuture<V,X extends Exception>
A delegating wrapper around a ListenableFuture
that adds support for
the checkedGet()
and checkedGet(long, TimeUnit)
methods.
Field Summary | |
---|---|
protected ListenableFuture<V> |
delegate
The delegate, used to pass along all our methods. |
Constructor Summary | |
---|---|
protected |
AbstractCheckedFuture(ListenableFuture<V> delegate)
Constructs an AbstractCheckedFuture that wraps a delegate. |
Method Summary | |
---|---|
void |
addListener(Runnable listener,
Executor exec)
Registers a listener to be run on the given executor. |
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task. |
V |
checkedGet()
Exception checking version of Future.get() that will translate
InterruptedException , CancellationException and
ExecutionException into application-specific exceptions. |
V |
checkedGet(long timeout,
TimeUnit unit)
Exception checking version of Future.get(long, TimeUnit) that will
translate InterruptedException , CancellationException and
ExecutionException into application-specific exceptions. |
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. |
protected abstract X |
mapException(Exception e)
Translates from an InterruptedException ,
CancellationException or ExecutionException thrown by
get to an exception of type X to be thrown by
checkedGet . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final ListenableFuture<V> delegate
Constructor Detail |
---|
protected AbstractCheckedFuture(ListenableFuture<V> delegate)
AbstractCheckedFuture
that wraps a delegate.
Method Detail |
---|
protected abstract X mapException(Exception e)
InterruptedException
,
CancellationException
or ExecutionException
thrown by
get
to an exception of type X
to be thrown by
checkedGet
. Subclasses must implement this method.
If e
is an InterruptedException
, the calling
checkedGet
method has already restored the interrupt after catching
the exception. If an implementation of mapException(Exception)
wishes to swallow the interrupt, it can do so by calling
Thread.interrupted()
.
public V checkedGet() throws X extends Exception
Future.get()
that will translate
InterruptedException
, CancellationException
and
ExecutionException
into application-specific exceptions.
This implementation calls get()
and maps that method's standard
exceptions to instances of type X
using mapException(java.lang.Exception)
.
In addition, if get
throws an InterruptedException
, this
implementation will set the current thread's interrupt status before
calling mapException
.
checkedGet
in interface CheckedFuture<V,X extends Exception>
X
- if get()
throws an InterruptedException
,
CancellationException
, or ExecutionException
X extends Exception
public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X extends Exception
Future.get(long, TimeUnit)
that will
translate InterruptedException
, CancellationException
and
ExecutionException
into application-specific exceptions. On
timeout this method throws a normal TimeoutException
.
This implementation calls get(long, TimeUnit)
and maps that
method's standard exceptions (excluding TimeoutException
, which is
propagated) to instances of type X
using mapException(java.lang.Exception)
.
In addition, if get
throws an InterruptedException
, this
implementation will set the current thread's interrupt status before
calling mapException
.
checkedGet
in interface CheckedFuture<V,X extends Exception>
X
- if get()
throws an InterruptedException
,
CancellationException
, or ExecutionException
TimeoutException
- if retrieving the result timed out.
X extends Exception
public boolean cancel(boolean mayInterruptIfRunning)
java.util.concurrent.Future
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.
cancel
in interface Future<V>
mayInterruptIfRunning
- true if the thread executing this
task should be interrupted; otherwise, in-progress tasks are allowed
to complete
public boolean isCancelled()
java.util.concurrent.Future
isCancelled
in interface Future<V>
public boolean isDone()
java.util.concurrent.Future
isDone
in interface Future<V>
public V get() throws InterruptedException, ExecutionException
java.util.concurrent.Future
get
in interface Future<V>
InterruptedException
- if the current thread was interrupted
while waiting
ExecutionException
- if the computation threw an
exceptionpublic V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
java.util.concurrent.Future
get
in interface Future<V>
timeout
- the maximum time to waitunit
- the time unit of the timeout argument
InterruptedException
- if the current thread was interrupted
while waiting
ExecutionException
- if the computation threw an
exception
TimeoutException
- if the wait timed outpublic void addListener(Runnable listener, Executor exec)
ListenableFuture
Future
's
computation is complete or, if the computation
is already complete, immediately.
There is no guaranteed ordering of execution of listeners, but any listener added through this method is guaranteed to be called once the computation is complete.
Listeners cannot throw checked exceptions and should not throw RuntimeException
unless their executors are prepared to handle it.
Listeners that will execute in MoreExecutors.sameThreadExecutor()
should take special care, since they may run during the call to addListener
or during the call that sets the future's value.
addListener
in interface ListenableFuture<V>
listener
- the listener to run when the computation is completeexec
- the executor to run the listener in
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |