com.google.common.util.concurrent
Class AbstractCheckedFuture<V,X extends java.lang.Exception>

java.lang.Object
  extended by com.google.common.util.concurrent.AbstractCheckedFuture<V,X>
All Implemented Interfaces:
CheckedFuture<V,X>, ListenableFuture<V>, java.util.concurrent.Future<V>

@Beta
public abstract class AbstractCheckedFuture<V,X extends java.lang.Exception>
extends java.lang.Object
implements CheckedFuture<V,X>

A delegating wrapper around a ListenableFuture that adds support for the checkedGet() and checkedGet(long, TimeUnit) methods.

Since:
1

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(java.lang.Runnable listener, java.util.concurrent.Executor exec)
          Registers a listener to be run on the given executor.
 boolean cancel(boolean mayInterruptIfRunning)
           
 V checkedGet()
          Exception checking version of Future.get() that will translate InterruptedException, CancellationException and ExecutionException into application-specific exceptions.
 V checkedGet(long timeout, java.util.concurrent.TimeUnit unit)
          Exception checking version of Future.get(long, TimeUnit) that will translate InterruptedException, CancellationException and ExecutionException into application-specific exceptions.
 V get()
           
 V get(long timeout, java.util.concurrent.TimeUnit unit)
           
 boolean isCancelled()
           
 boolean isDone()
           
protected abstract  X mapException(java.lang.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

delegate

protected final ListenableFuture<V> delegate
The delegate, used to pass along all our methods.

Constructor Detail

AbstractCheckedFuture

protected AbstractCheckedFuture(ListenableFuture<V> delegate)
Constructs an AbstractCheckedFuture that wraps a delegate.

Method Detail

mapException

protected abstract X mapException(java.lang.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().


checkedGet

public V checkedGet()
             throws X extends java.lang.Exception
Exception checking version of 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.

Specified by:
checkedGet in interface CheckedFuture<V,X extends java.lang.Exception>
Returns:
the result of executing the future.
Throws:
X - if get() throws an InterruptedException, CancellationException, or ExecutionException
X extends java.lang.Exception

checkedGet

public V checkedGet(long timeout,
                    java.util.concurrent.TimeUnit unit)
             throws java.util.concurrent.TimeoutException,
                    X extends java.lang.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 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.

Specified by:
checkedGet in interface CheckedFuture<V,X extends java.lang.Exception>
Returns:
the result of executing the future.
Throws:
X - if get() throws an InterruptedException, CancellationException, or ExecutionException
java.util.concurrent.TimeoutException - if retrieving the result timed out.
X extends java.lang.Exception

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Specified by:
cancel in interface java.util.concurrent.Future<V>

isCancelled

public boolean isCancelled()
Specified by:
isCancelled in interface java.util.concurrent.Future<V>

isDone

public boolean isDone()
Specified by:
isDone in interface java.util.concurrent.Future<V>

get

public V get()
      throws java.lang.InterruptedException,
             java.util.concurrent.ExecutionException
Specified by:
get in interface java.util.concurrent.Future<V>
Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException

get

public V get(long timeout,
             java.util.concurrent.TimeUnit unit)
      throws java.lang.InterruptedException,
             java.util.concurrent.ExecutionException,
             java.util.concurrent.TimeoutException
Specified by:
get in interface java.util.concurrent.Future<V>
Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException

addListener

public void addListener(java.lang.Runnable listener,
                        java.util.concurrent.Executor exec)
Description copied from interface: ListenableFuture
Registers a listener to be run on the given executor. The listener will run when the 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.

Specified by:
addListener in interface ListenableFuture<V>
Parameters:
listener - the listener to run when the computation is complete
exec - the executor to run the listener in