@Beta public abstract class AbstractCheckedFuture<V,X extends Exception> extends ForwardingListenableFuture.SimpleForwardingListenableFuture<V> implements CheckedFuture<V,X>
A delegating wrapper around a ListenableFuture
that adds support for the checkedGet()
and checkedGet(long, TimeUnit)
methods.
ForwardingListenableFuture.SimpleForwardingListenableFuture<V>
ForwardingFuture.SimpleForwardingFuture<V>
Modifier | Constructor and Description |
---|---|
protected |
AbstractCheckedFuture(ListenableFuture<V> delegate)
Constructs an
AbstractCheckedFuture that wraps a delegate. |
Modifier and Type | Method and Description |
---|---|
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. |
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 . |
delegate
addListener
cancel, get, get, isCancelled, isDone
toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addListener
protected AbstractCheckedFuture(ListenableFuture<V> delegate)
Constructs an AbstractCheckedFuture
that wraps a delegate.
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
. 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()
.
Subclasses may choose to throw, rather than return, a subclass of RuntimeException
to allow creating a CheckedFuture that throws both checked and unchecked exceptions.
public V checkedGet() throws X extends Exception
Exception checking version of Future.get()
that will translate InterruptedException
, CancellationException
and ExecutionException
into application-specific exceptions.
This implementation calls ForwardingFuture.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 ForwardingFuture.get()
throws an InterruptedException
, CancellationException
, or ExecutionException
X extends Exception
public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X extends Exception
Exception checking version of 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 ForwardingFuture.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 ForwardingFuture.get()
throws an InterruptedException
, CancellationException
, or ExecutionException
TimeoutException
- if retrieving the result timed out.X extends Exception