Package jadex.future
Class IntermediateFuture<E>
- All Implemented Interfaces:
IForwardCommandFuture
,IFuture<Collection<E>>
,IIntermediateFuture<E>
,Supplier<Collection<E>>
- Direct Known Subclasses:
TerminableIntermediateDelegationFuture
,TerminableIntermediateFuture
,Tuple2Future
Default implementation of an intermediate future.
-
Nested Class Summary
Nested classes/interfaces inherited from interface jadex.future.IForwardCommandFuture
IForwardCommandFuture.Type
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Map
<ISuspendable, String> The blocked intermediate callers (caller->state).The index of the next result for a thread.protected boolean
Flag indicating that addIntermediateResult()has been called.protected int
The max result count (if given by the producer).protected jadex.common.ICommand
<IResultListener<Collection<E>>> The intermediate results.Fields inherited from class jadex.future.Future
CALLER_QUEUED, CALLER_RESUMED, CALLER_SUSPENDED, callers, creation, DEBUG, exception, first, listeners, NO_STACK_COMPACTION, NONE, NOTIFICATIONS, notificount, notified, result, resultavailable, undone, UNSET
Fields inherited from interface jadex.future.IIntermediateFuture
DONE
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a future that is already done.IntermediateFuture
(Exception exception) Create a future that is already done (failed).IntermediateFuture
(Collection<E> results) Create a future that is already done. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addIntermediateResult
(E result) Add an intermediate result.boolean
addIntermediateResultIfUndone
(E result) Set the result.void
addResultListener
(IResultListener<Collection<E>> listener) Add a result listener.asStream()
Return a stream of the results of this future.asStream
(long timeout, boolean realtime) Return a stream of the results of this future.Called on exception.<T> IIntermediateFuture
<E> delegateEx
(Future<T> delegate) Called on exception.void
delegateFrom
(IFuture<Collection<E>> source) Delegate the result and exception from another future.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.IIntermediateFuture
<? extends E> Called when the future is done (finished or exception occurred).protected boolean
doSetException
(Exception exception, boolean undone) Set the exception (internal implementation for normal and if-undone).protected boolean
doSetFinished
(boolean undone) Declare that the future is finished.protected boolean
doSetResult
(Collection<E> result, boolean undone) Set the result.IIntermediateFuture
<? extends E> Called when the future is finished.Get the intermediate results that are available.Iterate over the intermediate results in a blocking fashion.getNextIntermediateResult
(boolean realtime) Iterate over the intermediate results in a blocking fashion.getNextIntermediateResult
(long timeout, boolean realtime) Iterate over the intermediate results in a blocking fashion.protected jadex.common.ICommand
<IResultListener<Collection<E>>> Get the notification command.protected int
Get the number of results already collected.boolean
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.IIntermediateFuture
<? extends E> Called when the maximum number of results is available.IIntermediateFuture
<? extends E> Called when the next intermediate value is available.protected void
notifyIntermediateResult
(IIntermediateResultListener<E> listener, E result) Notify a result listener.protected void
resume()
Resume also intermediate waiters.protected void
Resume after intermediate result.protected boolean
Schedule max notificationvoid
Declare that the future is finished.boolean
Declare that the future is finished.void
setMaxResultCount
(int max) Set the maximum number of results.protected void
storeResult
(E result, boolean scheduled) Add a result.then
(Consumer<? super Collection<E>> function) Applies a function after the result is available, using the result of this Future as input.Methods inherited from class jadex.future.Future
abortGet, catchEx, catchEx, delegate, delegateTo, doAddResultListener, doStartScheduledNotifications, executeNotification, get, get, get, get, get, getEmptyFuture, getException, getFuture, hasResultListener, isDone, isNotified, printOnEx, scheduleNotification, scheduleNotification, sendForwardCommand, setException, setExceptionIfUndone, setResult, setResultIfUndone, startScheduledNotifications, thenAccept, thenAccept, thenApply, thenApply, thenCombine, thenCompose, thenCompose, throwException
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface jadex.future.IFuture
catchEx, catchEx, delegate, delegateTo, get, get, get, get, get, getException, isDone, printOnEx, thenApply, thenApply, thenCompose, thenCompose
-
Field Details
-
results
The intermediate results. -
intermediate
protected boolean intermediateFlag indicating that addIntermediateResult()has been called. -
icallers
The blocked intermediate callers (caller->state). -
indices
The index of the next result for a thread. -
maxresultcnt
protected int maxresultcntThe max result count (if given by the producer). -
notcommand
-
-
Constructor Details
-
IntermediateFuture
public IntermediateFuture()Create a future that is already done. -
IntermediateFuture
Create a future that is already done.- Parameters:
results
- The results, if any.
-
IntermediateFuture
Create a future that is already done (failed).- Parameters:
exception
- The exception.
-
-
Method Details
-
getIntermediateResults
Get the intermediate results that are available.- Specified by:
getIntermediateResults
in interfaceIIntermediateFuture<E>
- Returns:
- The current intermediate results (copy of the list).
-
addIntermediateResult
Add an intermediate result. -
addIntermediateResultIfUndone
Set the result.- Parameters:
result
- The result.- Returns:
- True if result was set.
-
doAddIntermediateResult
Set the result and schedule listener notifications.- Returns:
- true, when the result was added (finished and undone otherwise).
-
storeResult
Add a result.- Parameters:
result
- The result.scheduled
- True, if any listener notification has been scheduled for this result. (used for subscription futures to check for lost values)
-
doSetResult
Set the result. Listener notifications occur on calling thread of this method.- Overrides:
doSetResult
in classFuture<Collection<E>>
- Parameters:
result
- The result.undone
- The undone flag.- Returns:
- True, if exception was set.
-
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. -
doSetException
Description copied from class:Future
Set the exception (internal implementation for normal and if-undone).- Overrides:
doSetException
in classFuture<Collection<E>>
- Parameters:
exception
- The exception.undone
- The undone flag.- Returns:
- True, if exception was set.
-
addResultListener
Add a result listener.- Specified by:
addResultListener
in interfaceIFuture<E>
- Overrides:
addResultListener
in classFuture<Collection<E>>
- Parameters:
listener
- The listener.
-
getNotificationCommand
Get the notification command.- Overrides:
getNotificationCommand
in classFuture<Collection<E>>
-
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
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.
- Throws:
NoSuchElementException
- when there are no more intermediate results and the future is finished.
-
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.- Returns:
- The next intermediate result.
- Throws:
NoSuchElementException
- when there are no more intermediate results and the future is finished.
-
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>
- Parameters:
timeout
- The timeout in millis.realtime
- Flag, if wait should be realtime (in constrast to simulation time).- Returns:
- The next intermediate result.
- Throws:
NoSuchElementException
- when there are no more intermediate results and the future is finished.
-
setMaxResultCount
public void setMaxResultCount(int max) Set the maximum number of results.- Parameters:
max
- The maximum number of results.
-
scheduleMaxNotification
Schedule max notification- Parameters:
lis
- The result listener
-
getResultCount
protected int getResultCount()Get the number of results already collected.- Returns:
- The number of results.
-
doGetNextIntermediateResult
Perform the get without increasing the index. -
notifyIntermediateResult
Notify a result listener.- Parameters:
listener
- The listener.
-
resume
protected void resume()Resume also intermediate waiters.- Overrides:
resume
in classFuture<Collection<E>>
-
resumeIntermediate
protected void resumeIntermediate()Resume after intermediate result. -
delegateFrom
Delegate the result and exception from another future.- Overrides:
delegateFrom
in classFuture<Collection<E>>
- Parameters:
source
- The source future.
-
catchEx
Description copied from class:Future
Called on exception. -
delegateEx
Called on exception.- Parameters:
delegate
- The future the exception will be delegated to.
-
then
Description copied from interface:IFuture
Applies a function after the result is available, using the result of this Future as input. -
next
Called when the next intermediate value is available.- Specified by:
next
in interfaceIIntermediateFuture<E>
- Parameters:
function
- Called when value arrives.- Returns:
- The future for chaining.
-
max
Called when the maximum number of results is available.- Specified by:
max
in interfaceIIntermediateFuture<E>
- Parameters:
function
- Called when max value arrives.- Returns:
- The future for chaining.
-
finished
Called when the future is finished.- Specified by:
finished
in interfaceIIntermediateFuture<E>
- Parameters:
function
- Called when max value arrives.- Returns:
- The future for chaining.
-
done
Called when the future is done (finished or exception occurred). Exception parameter will be set if the cause was an exception, null otherwise.- Specified by:
done
in interfaceIIntermediateFuture<E>
- Parameters:
consumer
- Called future is done.- Returns:
- The future for chaining.
-
asStream
Return a stream of the results of this future. Although this method itself is non-blocking, all terminal stream methods (e.g. forEach) will block until the future is finished!- Specified by:
asStream
in interfaceIIntermediateFuture<E>
-
asStream
Return a stream of the results of this future. Use the given timeout settings when waiting for elements in the stream. Although this method itself is non-blocking, all terminal stream methods (e.g. forEach) will block until the future is finished!- Specified by:
asStream
in interfaceIIntermediateFuture<E>
- Parameters:
timeout
- The timeout in millis.realtime
- Flag, if wait should be realtime (in constrast to simulation time).
-