com.google.common.util.concurrent
Class AbstractCheckedFuture<V,E extends Exception>

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

@Beta
public abstract class AbstractCheckedFuture<V,E extends Exception>
extends Object
implements CheckedFuture<V,E>

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

Since:
1
Author:
Sven Mawson

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)
          Adds a listener and executor to the ListenableFuture.
 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, 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, TimeUnit unit)
           
 boolean isCancelled()
           
 boolean isDone()
           
protected abstract  E mapException(Exception e)
          Translate from an InterruptedException, CancellationException or ExecutionException to an exception of type E.
 
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 E mapException(Exception e)
Translate from an InterruptedException, CancellationException or ExecutionException to an exception of type E. Subclasses must implement the mapping themselves. The e parameter can be an instance of InterruptedException, CancellationException, or ExecutionException.


checkedGet

public V checkedGet()
             throws E extends Exception
Description copied from interface: CheckedFuture
Exception checking version of Future.get() that will translate InterruptedException, CancellationException and ExecutionException into application-specific exceptions.

Specified by:
checkedGet in interface CheckedFuture<V,E extends Exception>
Returns:
the result of executing the future.
Throws:
E - on interruption, cancellation or execution exceptions.
E extends Exception

checkedGet

public V checkedGet(long timeout,
                    TimeUnit unit)
             throws TimeoutException,
                    E extends Exception
Description copied from interface: CheckedFuture
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.

Specified by:
checkedGet in interface CheckedFuture<V,E extends Exception>
Returns:
the result of executing the future.
Throws:
TimeoutException - if retrieving the result timed out.
E - on interruption, cancellation or execution exceptions.
E extends Exception

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Specified by:
cancel in interface Future<V>

isCancelled

public boolean isCancelled()
Specified by:
isCancelled in interface Future<V>

isDone

public boolean isDone()
Specified by:
isDone in interface Future<V>

get

public V get()
      throws InterruptedException,
             ExecutionException
Specified by:
get in interface Future<V>
Throws:
InterruptedException
ExecutionException

get

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

addListener

public void addListener(Runnable listener,
                        Executor exec)
Description copied from interface: ListenableFuture

Adds a listener and executor to the ListenableFuture. The listener will be passed to the executor for execution when the Future's computation is complete.

There is no guaranteed ordering of execution of listeners, they may get called in the order they were added and they may get called out of order, but any listener added through this method is guaranteed to be called once the computation is complete.

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.