Package jadex.future

Class SubscriptionIntermediateDelegationFuture<E>

All Implemented Interfaces:
IBackwardCommandFuture, IForwardCommandFuture, IFuture<Collection<E>>, IIntermediateFuture<E>, ISubscriptionIntermediateFuture<E>, ITerminableFuture<Collection<E>>, ITerminableIntermediateFuture<E>, Supplier<Collection<E>>
Direct Known Subclasses:
PullSubscriptionIntermediateDelegationFuture

public class SubscriptionIntermediateDelegationFuture<E> extends TerminableIntermediateDelegationFuture<E> implements ISubscriptionIntermediateFuture<E>
  • Field Details

    • ownresults

      protected Map<Thread,List<E>> ownresults
      The local results for a single thread.
    • storeforfirst

      protected boolean storeforfirst
      Flag if results should be stored till first listener is.
    • resultssize

      protected int resultssize
      The number of results.
  • Constructor Details

    • SubscriptionIntermediateDelegationFuture

      public SubscriptionIntermediateDelegationFuture()
      Create a new future.
    • SubscriptionIntermediateDelegationFuture

      public SubscriptionIntermediateDelegationFuture(ITerminableIntermediateFuture<E> src)
      Create a new future.
  • Method Details

    • storeResult

      protected void storeResult(E result, boolean scheduled)
      Add a result.
      Overrides:
      storeResult in class IntermediateFuture<E>
      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)
    • getResultCount

      protected int getResultCount()
      Get the number of results already collected.
      Overrides:
      getResultCount in class IntermediateFuture<E>
      Returns:
      The number of results.
    • addQuietListener

      public void addQuietListener(IResultListener<Collection<E>> listener)
      Add a listener which is only informed about new results, i.e. the initial results are not posted to this listener, even if it is the first listener to be added to this future.
      Specified by:
      addQuietListener in interface ISubscriptionIntermediateFuture<E>
    • addResultListener

      public void addResultListener(IResultListener<Collection<E>> listener)
      Add a result listener.
      Specified by:
      addResultListener in interface IFuture<E>
      Overrides:
      addResultListener in class IntermediateFuture<E>
      Parameters:
      listener - The listener.
    • getIntermediateResults

      public Collection<E> getIntermediateResults()
      Get the intermediate results that are available. Note: The semantics of this method is different to the normal intermediate future due to the fire-and-forget-semantics!
      Specified by:
      getIntermediateResults in interface IIntermediateFuture<E>
      Specified by:
      getIntermediateResults in interface ISubscriptionIntermediateFuture<E>
      Overrides:
      getIntermediateResults in class IntermediateFuture<E>
      Returns:
      1) Non-blocking access only: An empty collection, unless if the future is in "store-for-first" mode (default) and no listeners has yet been added, in which case the results until now are returned.
      2) Also blocking access from same thread: All results since the first blocking access that have not yet been consumed by getNextIntermediateResult().
    • 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 interface IIntermediateFuture<E>
      Overrides:
      getNextIntermediateResult in class IntermediateFuture<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.
    • 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 interface IIntermediateFuture<E>
      Overrides:
      hasNextIntermediateResult in class IntermediateFuture<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.
    • doGetNextIntermediateResult

      protected E doGetNextIntermediateResult(int index, long timeout, boolean realtime)
      Perform the get without increasing the index.
      Overrides:
      doGetNextIntermediateResult in class IntermediateFuture<E>
    • catchEx

      public IIntermediateFuture<E> catchEx(Consumer<? super Exception> consumer, Class<?> futuretype)
      Called on exception.
      Specified by:
      catchEx in interface IFuture<E>
      Overrides:
      catchEx in class IntermediateFuture<E>
      Parameters:
      consumer - The function called with the exception.
      futuretype - The function called with the exception.
    • catchEx

      public <T> IIntermediateFuture<E> catchEx(Future<T> delegate)
      Called on exception.
      Specified by:
      catchEx in interface IFuture<E>
      Overrides:
      catchEx in class Future<Collection<E>>
      Parameters:
      delegate - The future the exception will be delegated to.
    • max

      public IIntermediateFuture<? extends E> max(Consumer<Integer> function)
      Called when the maximum number of results is available.
      Specified by:
      max in interface IIntermediateFuture<E>
      Overrides:
      max in class IntermediateFuture<E>
      Parameters:
      function - Called when max value arrives.
      Returns:
      The future for chaining.
    • finished

      public IIntermediateFuture<? extends E> finished(Consumer<Void> function)
      Called when the future is finished.
      Specified by:
      finished in interface IIntermediateFuture<E>
      Overrides:
      finished in class IntermediateFuture<E>
      Parameters:
      function - Called when max value arrives.
      Returns:
      The future for chaining.