Package org.pipecraft.infra.concurrent
Class AbstractCheckedFuture<V,E extends Exception>
- java.lang.Object
-
- org.pipecraft.infra.concurrent.AbstractCheckedFuture<V,E>
-
- All Implemented Interfaces:
com.google.common.util.concurrent.ListenableFuture<V>,Future<V>,CheckedFuture<V,E>
- Direct Known Subclasses:
ImmediateFuture
public abstract class AbstractCheckedFuture<V,E extends Exception> extends Object implements CheckedFuture<V,E>
An abstract decorator onListenableFuturewhich adds checkedGet() methods which simplify Future's error handling. Exceptions originating from the future computation are extracted from theExecutionExceptionwrapper, and passed to the implementation's exception mapper, which is responsible for mapping to the proper exception of type E. Unless really required, it's strongly recommended to handle wrapped runtime exceptions differently than checked exceptions. They should be preserved and thrown unchanged, since they usually denote a bug. This class implements the CheckedFuture interface, which is a replacement for Google's deprecated CheckedFuture.- Author:
- Eyal Schneider
-
-
Constructor Summary
Constructors Constructor Description AbstractCheckedFuture(com.google.common.util.concurrent.ListenableFuture<V> future)Constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddListener(Runnable listener, Executor executor)booleancancel(boolean mayInterruptIfRunning)VcheckedGet()A simplified version ofFuture.get()which mapsExecutionExceptionto a more indicative exception typeVcheckedGet(long timeout, TimeUnit unit)A simplified version ofFuture.get(long, TimeUnit)which mapsExecutionExceptionto a more indicative exception typeVget()Vget(long timeout, TimeUnit unit)booleanisCancelled()booleanisDone()protected abstract Emap(Exception e)Maps a checked exception originating from the underlying computation into the standard exception type E.
-
-
-
Constructor Detail
-
AbstractCheckedFuture
public AbstractCheckedFuture(com.google.common.util.concurrent.ListenableFuture<V> future)
Constructor- Parameters:
future- the listenable future to wrap and add the checked getters functionality to.
-
-
Method Detail
-
checkedGet
public V checkedGet() throws InterruptedException, E extends Exception
A simplified version ofFuture.get()which mapsExecutionExceptionto a more indicative exception type- Specified by:
checkedGetin interfaceCheckedFuture<V,E extends Exception>- Returns:
- the result of executing the future.
- Throws:
InterruptedException- When the waiting for completion is interruptedE- when an execution exception is detected and mappedCancellationException- if the computation was cancelledE extends Exception
-
checkedGet
public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, InterruptedException, E extends Exception
A simplified version ofFuture.get(long, TimeUnit)which mapsExecutionExceptionto a more indicative exception type- Specified by:
checkedGetin interfaceCheckedFuture<V,E extends Exception>- Parameters:
timeout- The timeout to applyunit- The time unit of the timeout parameter- Returns:
- the result of executing the future.
- Throws:
TimeoutException- In case that the timeout period elapsesInterruptedException- When the waiting for completion is interruptedE- when an execution exception is detected and mappedCancellationException- if the computation was cancelledE extends Exception
-
map
protected abstract E map(Exception e)
Maps a checked exception originating from the underlying computation into the standard exception type E. In most cases when e is of type RuntimeException, it's recommended to simply throw it here, to avoid hiding a severe error or presenting it as a legitimate error.- Parameters:
e- The original exception- Returns:
- The mapped exception of type E
-
addListener
public void addListener(Runnable listener, Executor executor)
- Specified by:
addListenerin interfacecom.google.common.util.concurrent.ListenableFuture<V>
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfaceFuture<V>
-
get
public V get() throws InterruptedException, ExecutionException
- Specified by:
getin interfaceFuture<V>- Throws:
InterruptedExceptionExecutionException
-
get
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
getin interfaceFuture<V>- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
-