Package sk.ipesoft.d2000.async.base
Class AdjustableFuture<V>
- java.lang.Object
-
- sk.ipesoft.d2000.async.base.AdjustableFuture<V>
-
- Type Parameters:
V- type of the result
- All Implemented Interfaces:
java.util.concurrent.Future<V>,FutureEvent<V>
public class AdjustableFuture<V> extends java.lang.Object implements FutureEvent<V>
Default implementation ofFutureEvent.
-
-
Constructor Summary
Constructors Constructor Description AdjustableFuture()Create new future.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEventHandler(FutureEventHandler<V> futureEventHandler)Add handler for handling result of the future.voidaddEventHandler(FutureEventHandler<V> futureEventHandler, java.util.concurrent.Executor executor)Add handler for handling result of the future and executor with thread for execution of handling.static FutureEvent<java.lang.Void>allOf(java.util.Collection<? extends FutureEvent<?>> futures)Create new instance that completes when all of futures are complete.static FutureEvent<java.lang.Void>anyOf(java.util.Collection<? extends FutureEvent<?>> futures)Create new instance of future that completes when first of futures completes.booleancancel(boolean force)static <T> FutureEvent<T>completedFuture(T value)Create new instance with value already set.Vget()Vget(long timeout, java.util.concurrent.TimeUnit units)booleanisCancelled()booleanisDone()static <T> FutureEvent<T>runTaskInNewThread(java.util.concurrent.Callable<T> task)Create new thread, executetaskwithin the thread and obtain result as aFutureobject.voidsetException(java.lang.Throwable error)Set exception that has been thrown during evaluation of future result.voidsetValue(V value)Set value of future.
-
-
-
Method Detail
-
runTaskInNewThread
public static <T> FutureEvent<T> runTaskInNewThread(java.util.concurrent.Callable<T> task)
Create new thread, executetaskwithin the thread and obtain result as aFutureobject.- Type Parameters:
T- type of the result- Parameters:
task- callable to be executed- Returns:
- future object
-
completedFuture
public static <T> FutureEvent<T> completedFuture(T value)
Create new instance with value already set.- Type Parameters:
T- type of value- Parameters:
value- value of future- Returns:
- completed future
-
allOf
public static FutureEvent<java.lang.Void> allOf(java.util.Collection<? extends FutureEvent<?>> futures)
Create new instance that completes when all of futures are complete.- Parameters:
futures- collection of futures- Returns:
- new instance of future
-
anyOf
public static FutureEvent<java.lang.Void> anyOf(java.util.Collection<? extends FutureEvent<?>> futures)
Create new instance of future that completes when first of futures completes.- Parameters:
futures- collection of future values- Returns:
- new instance
-
setValue
public void setValue(V value)
Set value of future.- Parameters:
value- value of future result
-
setException
public void setException(java.lang.Throwable error)
Set exception that has been thrown during evaluation of future result.- Parameters:
error- thrown exception
-
cancel
public boolean cancel(boolean force)
- Specified by:
cancelin interfacejava.util.concurrent.Future<V>
-
get
public V get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
- Specified by:
getin interfacejava.util.concurrent.Future<V>- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionException
-
get
public V get(long timeout, java.util.concurrent.TimeUnit units) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
- Specified by:
getin interfacejava.util.concurrent.Future<V>- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutException
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfacejava.util.concurrent.Future<V>
-
isDone
public boolean isDone()
- Specified by:
isDonein interfacejava.util.concurrent.Future<V>
-
addEventHandler
public void addEventHandler(FutureEventHandler<V> futureEventHandler)
Description copied from interface:FutureEventAdd handler for handling result of the future.In case that result of the future has been available prior this call, the
futureEventHandlerwill be invoked immediately on calling thread. Otherwise it will be invoked when result of the future is available on thread that provided result of the future.Warning: handling of the result should be quick, because it can slow-down the system. Use
FutureEvent.addEventHandler(FutureEventHandler, Executor)for adding long-running handlers.- Specified by:
addEventHandlerin interfaceFutureEvent<V>- Parameters:
futureEventHandler- handler of future result
-
addEventHandler
public void addEventHandler(FutureEventHandler<V> futureEventHandler, java.util.concurrent.Executor executor)
Description copied from interface:FutureEventAdd handler for handling result of the future and executor with thread for execution of handling.In case that result of the future has been available prior this call, the
futureEventHandlerwill be passed to executor immediately. Otherwise it will be passed when result of the future is available.- Specified by:
addEventHandlerin interfaceFutureEvent<V>- Parameters:
futureEventHandler- handler of future resultexecutor- encapsulation of thread that will execute result's handling
-
-