Package jadex.commons.future
Class Future<E>
- java.lang.Object
- 
- jadex.commons.future.Future<E>
 
- 
- All Implemented Interfaces:
- IForwardCommandFuture,- IFuture<E>
 - Direct Known Subclasses:
- IntermediateFuture,- TerminableDelegationFuture,- TerminableFuture
 
 public class Future<E> extends java.lang.Object implements IFuture<E>, IForwardCommandFuture Future that includes mechanisms for callback notification. This allows a caller to decide if a) a blocking call to get() should be used b) a callback shall be invoked
- 
- 
Nested Class Summary- 
Nested classes/interfaces inherited from interface jadex.commons.future.IForwardCommandFutureIForwardCommandFuture.Type
 
- 
 - 
Field SummaryFields Modifier and Type Field Description protected static java.lang.StringCALLER_QUEUEDA caller is queued for suspension.protected static java.lang.StringCALLER_RESUMEDA caller is resumed.protected static java.lang.StringCALLER_SUSPENDEDA caller is suspended.protected java.util.Map<ISuspendable,java.lang.String>callersThe blocked callers (caller->state).protected java.lang.ExceptioncreationFor capturing call stack of future creation.static booleanDEBUGDebug flag.protected java.lang.ExceptionexceptionThe exception (if any).protected java.lang.ExceptionfirstFor capturing call stack of first setResult/Exception call.protected IResultListener<E>listenerThe first listener (for avoiding array creation).protected java.util.List<IResultListener<E>>listenersThe listeners.static booleanNO_STACK_COMPACTIONDisable Stack unfolding for easier debugging.static longNONEConstant for no timeout.protected ICommand<IResultListener<E>>notcommandprotected java.util.Queue<Tuple2<IResultListener<E>,ICommand<IResultListener<E>>>>notificationsThe scheduled notifications.protected EresultThe result.protected booleanresultavailableFlag indicating if result is available.static java.lang.ThreadLocal<java.util.Queue<Tuple3<Future<?>,IResultListener<?>,ICommand<IResultListener<?>>>>>STACKNotification stack for unwinding call stack to topmost future.protected booleanundoneThe undone flag.static longUNSETConstant for unset.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidabortGet(ISuspendable caller)Abort a blocking get call.voidaddResultListener(IResultListener<E> listener)Add a result listener.IFuture<E>catchErr(Consumer<? super java.lang.Exception> consumer)IFuture<E>catchErr(Consumer<? super java.lang.Exception> consumer, java.lang.Class<?> futuretype)<T> voidcatchErr(Future<T> delegate)Sequential execution of async methods via implicit delegation.voiddelegate(Future<E> target)protected booleandoSetException(java.lang.Exception exception, boolean undone)Set the exception (internal implementation for normal and if-undone).protected booleandoSetResult(E result, boolean undone)Set the result without notifying listeners.protected voidexecuteNotification(IResultListener<E> listener, ICommand<IResultListener<E>> command)Execute a notification.Eget()Get the result - blocking call.Eget(boolean realtime)Get the result - blocking call.Eget(long timeout)Get the result - blocking call.Eget(long timeout, boolean realtime)Get the result - blocking call.Eget(ThreadSuspendable sus)Deprecated.- From 3.0.static <T> IFuture<T>getEmptyFuture()Get the empty future of some type.java.lang.ExceptiongetException()Get the exception, if any.<T> Future<T>getFuture(java.lang.Class<?> futuretype)Sequential execution of async methods via implicit delegation.protected ICommand<IResultListener<E>>getNotificationCommand()Get the notification command.booleanhasResultListener()Check, if the future has at least one listener.booleanisDone()Test if done, i.e.protected voidnotifyListener()Notify all result listeners of the finished future (result or exception).protected voidnotifyListener(IResultListener<E> listener)Notify a specific result listener of the finished future (result or exception).protected voidresume()Resume after result or exception has been set.protected voidscheduleNotification(IResultListener<E> listener, ICommand<IResultListener<E>> command)Schedule a listener notification for a specific listener.protected voidscheduleNotification(ICommand<IResultListener<E>> command)Schedule a notification for all listeners.protected voidscheduleNotification(IFilter<IResultListener<E>> filter, ICommand<IResultListener<E>> command)Schedule a listener notification.voidsendForwardCommand(java.lang.Object command)Send a (forward) command to the listeners.voidsetException(java.lang.Exception exception)Set the exception.booleansetExceptionIfUndone(java.lang.Exception exception)Set the exception.voidsetResult(E result)Set the result.booleansetResultIfUndone(E result)Set the result.protected voidstartScheduledNotifications()Start scheduled listener notifications.IFuture<? extends E>then(Consumer<? super E> function)Applies a function after the result is available, using the result of this Future as input.IFuture<java.lang.Void>thenAccept(Consumer<? super E> consumer)IFuture<java.lang.Void>thenAccept(Consumer<? super E> consumer, java.lang.Class<?> futuretype)<T> IFuture<T>thenApply(Function<? super E,? extends T> function)Applies a function after the result is available, using the result of this Future as input.<T> IFuture<T>thenApply(Function<? super E,? extends T> function, java.lang.Class<?> futuretype)Applies a function after the result is available, using the result of this Future as input.<U,V>
 IFuture<V>thenCombine(IFuture<U> other, BiFunction<? super E,? super U,? extends V> function, java.lang.Class<?> futuretype)<T> IFuture<T>thenCompose(Function<? super E,IFuture<T>> function)The result of this future is delegated to the given (future-returning) function.<T> IFuture<T>thenCompose(Function<? super E,IFuture<T>> function, java.lang.Class<?> futuretype)The result of this future is delegated to the given (future-returning) function.
 
- 
- 
- 
Field Detail- 
STACKpublic static final java.lang.ThreadLocal<java.util.Queue<Tuple3<Future<?>,IResultListener<?>,ICommand<IResultListener<?>>>>> STACK Notification stack for unwinding call stack to topmost future.
 - 
CALLER_QUEUEDprotected static final java.lang.String CALLER_QUEUED A caller is queued for suspension.- See Also:
- Constant Field Values
 
 - 
CALLER_RESUMEDprotected static final java.lang.String CALLER_RESUMED A caller is resumed.- See Also:
- Constant Field Values
 
 - 
CALLER_SUSPENDEDprotected static final java.lang.String CALLER_SUSPENDED A caller is suspended.- See Also:
- Constant Field Values
 
 - 
DEBUGpublic static boolean DEBUG Debug flag.
 - 
NO_STACK_COMPACTIONpublic static boolean NO_STACK_COMPACTION Disable Stack unfolding for easier debugging.
 - 
NONEpublic static final long NONE Constant for no timeout.- See Also:
- Constant Field Values
 
 - 
UNSETpublic static final long UNSET Constant for unset.- See Also:
- Constant Field Values
 
 - 
resultprotected E result The result.
 - 
exceptionprotected java.lang.Exception exception The exception (if any).
 - 
resultavailableprotected volatile boolean resultavailable Flag indicating if result is available.
 - 
callersprotected java.util.Map<ISuspendable,java.lang.String> callers The blocked callers (caller->state).
 - 
listenerprotected IResultListener<E> listener The first listener (for avoiding array creation).
 - 
listenersprotected java.util.List<IResultListener<E>> listeners The listeners.
 - 
creationprotected java.lang.Exception creation For capturing call stack of future creation.
 - 
firstprotected java.lang.Exception first For capturing call stack of first setResult/Exception call.
 - 
undoneprotected boolean undone The undone flag.
 - 
notificationsprotected java.util.Queue<Tuple2<IResultListener<E>,ICommand<IResultListener<E>>>> notifications The scheduled notifications.
 - 
notcommandprotected ICommand<IResultListener<E>> notcommand 
 
- 
 - 
Constructor Detail- 
Futurepublic Future() Create a new future.
 - 
Futurepublic Future(E result) Create a future that is already done.- Parameters:
- result- The result, if any.
 
 - 
Futurepublic Future(java.lang.Exception exception) Create a future that is already failed.- Parameters:
- exception- The exception.
 
 
- 
 - 
Method Detail- 
getEmptyFuturepublic static <T> IFuture<T> getEmptyFuture() Get the empty future of some type.- Returns:
- The empty future.
 
 - 
isDonepublic boolean isDone() Test if done, i.e. result is available.
 - 
getExceptionpublic java.lang.Exception getException() Get the exception, if any.- Specified by:
- getExceptionin interface- IFuture<E>
- Returns:
- The exception, if any, or null if the future is not yet done or succeeded without exception.
 
 - 
getpublic E get(ThreadSuspendable sus) Deprecated.- From 3.0. Use the version without suspendable. Will NOT use the suspendable given as parameter. Get the result - blocking call.
 - 
getpublic E get() Get the result - blocking call.
 - 
getpublic E get(boolean realtime) Get the result - blocking call.
 - 
getpublic E get(long timeout) Get the result - blocking call.
 - 
getpublic E get(long timeout, boolean realtime) Get the result - blocking call.
 - 
doSetExceptionprotected boolean doSetException(java.lang.Exception exception, boolean undone)Set the exception (internal implementation for normal and if-undone).
 - 
setExceptionpublic void setException(java.lang.Exception exception) Set the exception. Listener notifications occur on calling thread of this method.- Parameters:
- exception- The exception.
 
 - 
setExceptionIfUndonepublic boolean setExceptionIfUndone(java.lang.Exception exception) Set the exception. Listener notifications occur on calling thread of this method.- Parameters:
- exception- The exception.
 
 - 
setResultpublic void setResult(E result) Set the result. Listener notifications occur on calling thread of this method.- Parameters:
- result- The result.
 
 - 
setResultIfUndonepublic boolean setResultIfUndone(E result) Set the result. Listener notifications occur on calling thread of this method.- Parameters:
- result- The result.
- Returns:
- True if result was set.
 
 - 
doSetResultprotected boolean doSetResult(E result, boolean undone) Set the result without notifying listeners.
 - 
resumeprotected void resume() Resume after result or exception has been set.
 - 
abortGetpublic void abortGet(ISuspendable caller) Abort a blocking get call.
 - 
scheduleNotificationprotected void scheduleNotification(IFilter<IResultListener<E>> filter, ICommand<IResultListener<E>> command) Schedule a listener notification.- Parameters:
- filter- Optional filter to select only specific listener (e.g. for forward commands). Otherwise uses all listeners.
- command- The notification command to execute for each selected listener.
 
 - 
scheduleNotificationprotected void scheduleNotification(ICommand<IResultListener<E>> command) Schedule a notification for all listeners.- Parameters:
- command- The notification command to execute for each currently registered listener.
 
 - 
scheduleNotificationprotected void scheduleNotification(IResultListener<E> listener, ICommand<IResultListener<E>> command) Schedule a listener notification for a specific listener. Can be called from synchronized block.- Parameters:
- listener- The listener to notify.
- command- The notification command to execute for the listener.
 
 - 
startScheduledNotificationsprotected final void startScheduledNotifications() Start scheduled listener notifications. Must not be called from synchronized block.
 - 
executeNotificationprotected void executeNotification(IResultListener<E> listener, ICommand<IResultListener<E>> command) Execute a notification. Override for scheduling on other threads.
 - 
addResultListenerpublic void addResultListener(IResultListener<E> listener) Add a result listener.- Specified by:
- addResultListenerin interface- IFuture<E>
- Parameters:
- listener- The listener.
 
 - 
notifyListenerprotected void notifyListener() Notify all result listeners of the finished future (result or exception).
 - 
notifyListenerprotected void notifyListener(IResultListener<E> listener) Notify a specific result listener of the finished future (result or exception).- Parameters:
- listener- The listener.
 
 - 
getNotificationCommandprotected ICommand<IResultListener<E>> getNotificationCommand() Get the notification command.
 - 
sendForwardCommandpublic void sendForwardCommand(java.lang.Object command) Send a (forward) command to the listeners.- Specified by:
- sendForwardCommandin interface- IForwardCommandFuture
- Parameters:
- command- The command.
 
 - 
hasResultListenerpublic boolean hasResultListener() Check, if the future has at least one listener.
 - 
thenpublic IFuture<? extends E> then(Consumer<? super E> function) Description copied from interface:IFutureApplies a function after the result is available, using the result of this Future as input.
 - 
thenApplypublic <T> IFuture<T> thenApply(Function<? super E,? extends T> function) Description copied from interface:IFutureApplies a function after the result is available, using the result of this Future as input.
 - 
thenApplypublic <T> IFuture<T> thenApply(Function<? super E,? extends T> function, java.lang.Class<?> futuretype) Description copied from interface:IFutureApplies a function after the result is available, using the result of this Future as input.
 - 
thenComposepublic <T> IFuture<T> thenCompose(Function<? super E,IFuture<T>> function) Description copied from interface:IFutureThe result of this future is delegated to the given (future-returning) function. The result of the function will be available in the returned future.- Specified by:
- thenComposein interface- IFuture<E>
- Parameters:
- function- Function that takes the result of this future as input and delivers future(t).
- Returns:
- Future of the result of the second async call.
 
 - 
thenComposepublic <T> IFuture<T> thenCompose(Function<? super E,IFuture<T>> function, java.lang.Class<?> futuretype) Description copied from interface:IFutureThe result of this future is delegated to the given (future-returning) function. The result of the function will be available in the returned future.- Specified by:
- thenComposein interface- IFuture<E>
- Parameters:
- function- Function that takes the result of this future as input and delivers future(t).
- futuretype- The type of the return future. If null, a default future is created.
- Returns:
- Future of the result of the second async call.
 
 - 
thenAcceptpublic IFuture<java.lang.Void> thenAccept(Consumer<? super E> consumer, java.lang.Class<?> futuretype) 
 - 
thenCombinepublic <U,V> IFuture<V> thenCombine(IFuture<U> other, BiFunction<? super E,? super U,? extends V> function, java.lang.Class<?> futuretype) 
 - 
catchErrpublic <T> void catchErr(Future<T> delegate) Sequential execution of async methods via implicit delegation.
 - 
catchErrpublic IFuture<E> catchErr(Consumer<? super java.lang.Exception> consumer, java.lang.Class<?> futuretype) 
 - 
getFuturepublic <T> Future<T> getFuture(java.lang.Class<?> futuretype) Sequential execution of async methods via implicit delegation.- Parameters:
- futuretype- The type of the result future (cannot be automatically determined).
- Returns:
- Future of the result of the second async call.
 
 
- 
 
-