Package jadex.commons.future
Class IntermediateFuture<E>
- java.lang.Object
-
- jadex.commons.future.Future<java.util.Collection<E>>
-
- jadex.commons.future.IntermediateFuture<E>
-
- All Implemented Interfaces:
IForwardCommandFuture
,IFuture<java.util.Collection<E>>
,IIntermediateFuture<E>
- Direct Known Subclasses:
TerminableIntermediateDelegationFuture
,TerminableIntermediateFuture
,Tuple2Future
public class IntermediateFuture<E> extends Future<java.util.Collection<E>> implements IIntermediateFuture<E>
Default implementation of an intermediate future.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface jadex.commons.future.IForwardCommandFuture
IForwardCommandFuture.Type
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<ISuspendable,java.lang.String>
icallers
The blocked intermediate callers (caller->state).protected java.util.Map<java.lang.Thread,java.lang.Integer>
indices
The index of the next result for a thread.protected boolean
intermediate
Flag indicating that addIntermediateResult()has been called.protected int
maxresultcnt
The max result count (if given by the producer).protected ICommand<IResultListener<java.util.Collection<E>>>
notcommand
protected java.util.List<E>
results
The intermediate results.-
Fields inherited from class jadex.commons.future.Future
CALLER_QUEUED, CALLER_RESUMED, CALLER_SUSPENDED, callers, creation, DEBUG, exception, first, listener, listeners, NO_STACK_COMPACTION, NONE, notifications, result, resultavailable, STACK, undone, UNSET
-
Fields inherited from interface jadex.commons.future.IIntermediateFuture
DONE
-
-
Constructor Summary
Constructors Constructor Description IntermediateFuture()
Create a future that is already done.IntermediateFuture(java.lang.Exception exception)
Create a future that is already done (failed).IntermediateFuture(java.util.Collection<E> results)
Create a future that is already done.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addIntermediateResult(E result)
Add an intermediate result.boolean
addIntermediateResultIfUndone(E result)
Set the result.void
addIntermediateResultListener(IIntermediateResultListener<E> listener)
Add an result listener, which called on intermediate results.void
addResultListener(IResultListener<java.util.Collection<E>> listener)
Add a result listener.IFuture<java.util.Collection<E>>
catchErr(Consumer<? super java.lang.Exception> consumer, java.lang.Class<?> futuretype)
protected boolean
doAddIntermediateResult(E result, boolean undone)
Set the result and schedule listener notifications.protected E
doGetNextIntermediateResult(int index, long timeout, boolean realtime)
Perform the get without increasing the index.protected boolean
doSetFinished(boolean undone)
Declare that the future is finished.protected boolean
doSetResult(java.util.Collection<E> result, boolean undone)
Set the result.IIntermediateFuture<? extends E>
finished(Consumer<? super E> function)
<R> IIntermediateFuture<R>
flatMapAsync(Function<E,IIntermediateFuture<R>> function)
Implements async loop and applies a an async multi-function to each element.java.util.Collection<E>
getIntermediateResults()
Get the intermediate results that are available.E
getNextIntermediateResult()
Iterate over the intermediate results in a blocking fashion.E
getNextIntermediateResult(boolean realtime)
Iterate over the intermediate results in a blocking fashion.E
getNextIntermediateResult(long timeout, boolean realtime)
Iterate over the intermediate results in a blocking fashion.protected ICommand<IResultListener<java.util.Collection<E>>>
getNotificationCommand()
Get the notification command.protected int
getResultCount()
Get the number of results already collected.boolean
hasNextIntermediateResult()
Check if there are more results for iteration for the given caller.boolean
hasNextIntermediateResult(long timeout, boolean realtime)
Check if there are more results for iteration for the given caller.<R> IIntermediateFuture<R>
mapAsync(Function<E,IFuture<R>> function)
Implements async loop and applies a an async function to each element.<R> IIntermediateFuture<R>
mapAsync(Function<E,IFuture<R>> function, java.lang.Class<?> futuretype)
Implements async loop and applies a an async function to each element.IIntermediateFuture<? extends E>
max(Consumer<java.lang.Integer> function)
IIntermediateFuture<? extends E>
next(Consumer<? super E> function)
Add a functional result listener, which called on intermediate results.protected void
notifyIntermediateResult(IIntermediateResultListener<E> listener, E result)
Notify a result listener.protected void
resume()
Resume also intermediate waiters.protected void
resumeIntermediate()
Resume after intermediate result.protected boolean
scheduleMaxNotification(IResultListener<java.util.Collection<E>> lis)
void
setFinished()
Declare that the future is finished.boolean
setFinishedIfUndone()
Declare that the future is finished.void
setMaxResultCount(int max)
Set the maximum number of results.protected boolean
storeResult(E result)
Add a result.-
Methods inherited from class jadex.commons.future.Future
abortGet, catchErr, catchErr, delegate, doSetException, executeNotification, get, get, get, get, get, getEmptyFuture, getException, getFuture, hasResultListener, isDone, notifyListener, notifyListener, scheduleNotification, scheduleNotification, scheduleNotification, sendForwardCommand, setException, setExceptionIfUndone, setResult, setResultIfUndone, startScheduledNotifications, then, thenAccept, thenAccept, thenApply, thenApply, thenCombine, thenCompose, thenCompose
-
-
-
-
Field Detail
-
results
protected java.util.List<E> results
The intermediate results.
-
intermediate
protected boolean intermediate
Flag indicating that addIntermediateResult()has been called.
-
icallers
protected java.util.Map<ISuspendable,java.lang.String> icallers
The blocked intermediate callers (caller->state).
-
indices
protected java.util.Map<java.lang.Thread,java.lang.Integer> indices
The index of the next result for a thread.
-
maxresultcnt
protected int maxresultcnt
The max result count (if given by the producer).
-
notcommand
protected ICommand<IResultListener<java.util.Collection<E>>> notcommand
-
-
Constructor Detail
-
IntermediateFuture
public IntermediateFuture()
Create a future that is already done.- Parameters:
result
- The result, if any.
-
IntermediateFuture
public IntermediateFuture(java.util.Collection<E> results)
Create a future that is already done.- Parameters:
result
- The result, if any.
-
IntermediateFuture
public IntermediateFuture(java.lang.Exception exception)
Create a future that is already done (failed).- Parameters:
exception
- The exception.
-
-
Method Detail
-
getIntermediateResults
public java.util.Collection<E> getIntermediateResults()
Get the intermediate results that are available.- Specified by:
getIntermediateResults
in interfaceIIntermediateFuture<E>
- Returns:
- The current intermediate results (copy of the list).
-
addIntermediateResult
public void addIntermediateResult(E result)
Add an intermediate result.
-
addIntermediateResultIfUndone
public boolean addIntermediateResultIfUndone(E result)
Set the result.- Parameters:
result
- The result.- Returns:
- True if result was set.
-
doAddIntermediateResult
protected boolean doAddIntermediateResult(E result, boolean undone)
Set the result and schedule listener notifications.- Returns:
- true, when the result was added (finished and undone otherwise).
-
storeResult
protected boolean storeResult(E result)
Add a result.- Parameters:
result
- The result.
-
doSetResult
protected boolean doSetResult(java.util.Collection<E> result, boolean undone)
Set the result. Listener notifications occur on calling thread of this method.- Overrides:
doSetResult
in classFuture<java.util.Collection<E>>
- Parameters:
result
- The result.
-
setFinished
public void setFinished()
Declare that the future is finished.
-
setFinishedIfUndone
public boolean setFinishedIfUndone()
Declare that the future is finished.
-
doSetFinished
protected boolean doSetFinished(boolean undone)
Declare that the future is finished.
-
addResultListener
public void addResultListener(IResultListener<java.util.Collection<E>> listener)
Add a result listener.- Specified by:
addResultListener
in interfaceIFuture<E>
- Overrides:
addResultListener
in classFuture<java.util.Collection<E>>
- Parameters:
listener
- The listener.
-
getNotificationCommand
protected ICommand<IResultListener<java.util.Collection<E>>> getNotificationCommand()
Get the notification command.- Overrides:
getNotificationCommand
in classFuture<java.util.Collection<E>>
-
addIntermediateResultListener
public void addIntermediateResultListener(IIntermediateResultListener<E> listener)
Add an result listener, which called on intermediate results.- Parameters:
listener
- The intermediate listener.
-
hasNextIntermediateResult
public boolean hasNextIntermediateResult()
Check if there are more results for iteration for the given caller. If there are currently no unprocessed results and future is not yet finished, the caller is blocked until either new results are available and true is returned or the future is finished, thus returning false.- Specified by:
hasNextIntermediateResult
in interfaceIIntermediateFuture<E>
- Returns:
- True, when there are more intermediate results for the caller.
-
hasNextIntermediateResult
public boolean hasNextIntermediateResult(long timeout, boolean realtime)
Check if there are more results for iteration for the given caller. If there are currently no unprocessed results and future is not yet finished, the caller is blocked until either new results are available and true is returned or the future is finished, thus returning false.- Specified by:
hasNextIntermediateResult
in interfaceIIntermediateFuture<E>
- Parameters:
timeout
- The timeout in millis.realtime
- Flag, if wait should be realtime (in constrast to simulation time).- Returns:
- True, when there are more intermediate results for the caller.
-
getNextIntermediateResult
public E getNextIntermediateResult()
Iterate over the intermediate results in a blocking fashion. Manages results independently for different callers, i.e. when called from different threads, each thread receives all intermediate results. The operation is guaranteed to be non-blocking, if hasNextIntermediateResult() has returned true before for the same caller. Otherwise the caller is blocked until a result is available or the future is finished.- Specified by:
getNextIntermediateResult
in interfaceIIntermediateFuture<E>
- Returns:
- The next intermediate result.
-
getNextIntermediateResult
public E getNextIntermediateResult(boolean realtime)
Iterate over the intermediate results in a blocking fashion. Manages results independently for different callers, i.e. when called from different threads, each thread receives all intermediate results. The operation is guaranteed to be non-blocking, if hasNextIntermediateResult() has returned true before for the same caller. Otherwise the caller is blocked until a result is available or the future is finished.- Returns:
- The next intermediate result.
-
getNextIntermediateResult
public E getNextIntermediateResult(long timeout, boolean realtime)
Iterate over the intermediate results in a blocking fashion. Manages results independently for different callers, i.e. when called from different threads, each thread receives all intermediate results. The operation is guaranteed to be non-blocking, if hasNextIntermediateResult() has returned true before for the same caller. Otherwise the caller is blocked until a result is available or the future is finished.- Specified by:
getNextIntermediateResult
in interfaceIIntermediateFuture<E>
- Parameters:
timeout
- The timeout in millis.realtime
- Flag, if wait should be realtime (in constrast to simulation time).- Returns:
- The next intermediate result.
-
setMaxResultCount
public void setMaxResultCount(int max)
Set the maximum number of results.- Parameters:
max
- The maximum number of results.
-
scheduleMaxNotification
protected boolean scheduleMaxNotification(IResultListener<java.util.Collection<E>> lis)
- Parameters:
max
-
-
getResultCount
protected int getResultCount()
Get the number of results already collected.- Returns:
- The number of results.
-
doGetNextIntermediateResult
protected E doGetNextIntermediateResult(int index, long timeout, boolean realtime)
Perform the get without increasing the index.
-
notifyIntermediateResult
protected void notifyIntermediateResult(IIntermediateResultListener<E> listener, E result)
Notify a result listener.- Parameters:
listener
- The listener.
-
resume
protected void resume()
Resume also intermediate waiters.
-
resumeIntermediate
protected void resumeIntermediate()
Resume after intermediate result.
-
catchErr
public IFuture<java.util.Collection<E>> catchErr(Consumer<? super java.lang.Exception> consumer, java.lang.Class<?> futuretype)
-
next
public IIntermediateFuture<? extends E> next(Consumer<? super E> function)
Description copied from interface:IIntermediateFuture
Add a functional result listener, which called on intermediate results. Exceptions will be logged.- Specified by:
next
in interfaceIIntermediateFuture<E>
-
max
public IIntermediateFuture<? extends E> max(Consumer<java.lang.Integer> function)
- Specified by:
max
in interfaceIIntermediateFuture<E>
-
finished
public IIntermediateFuture<? extends E> finished(Consumer<? super E> function)
- Specified by:
finished
in interfaceIIntermediateFuture<E>
-
mapAsync
public <R> IIntermediateFuture<R> mapAsync(Function<E,IFuture<R>> function)
Implements async loop and applies a an async function to each element.- Specified by:
mapAsync
in interfaceIIntermediateFuture<E>
- Parameters:
function
- The function.- Returns:
- True result intermediate future.
-
mapAsync
public <R> IIntermediateFuture<R> mapAsync(Function<E,IFuture<R>> function, java.lang.Class<?> futuretype)
Implements async loop and applies a an async function to each element.- Specified by:
mapAsync
in interfaceIIntermediateFuture<E>
- Parameters:
function
- The function.- Returns:
- True result intermediate future.
-
flatMapAsync
public <R> IIntermediateFuture<R> flatMapAsync(Function<E,IIntermediateFuture<R>> function)
Implements async loop and applies a an async multi-function to each element.- Specified by:
flatMapAsync
in interfaceIIntermediateFuture<E>
- Parameters:
function
- The function.- Returns:
- True result intermediate future.
-
-