Class IteratorWrapper<T>

  • Type Parameters:
    T - The type of iterator handled by this iterator wrapper.
    All Implemented Interfaces:
    Runnable

    public class IteratorWrapper<T>
    extends Object
    implements Runnable
    A wrapper for an iterator and also a runnable for removing the iterator from an AbstractIteratorHandler. Note that a ScheduledExecutorService does not allow to remove a scheduled runnable. Therefore this remover checks the remaining alive time of its iterator and either removes it or reschedules itself for another run.
    As soon as the alive time elapses, this wrapper removes the iterator from the data structure and drops it. Dropping may fail in case the underlying iterator has its own drop mechanism.
    • Field Detail

      • iteratorId

        protected final UUID iteratorId
        The ID assigned to the corresponding iterator.
      • iterator

        protected final T iterator
        The wrapped iterator of this wrapper.
      • remAliveTimeExtension

        protected final long remAliveTimeExtension
        The time in milliseconds the alive time is extended if requested.
      • dropTime

        protected long dropTime
        The time the iterator is dropped. This may not be used but overridden by the drop time of the underlying iterator.
      • dropper

        protected final Consumer<T> dropper
        The method called for dropping the iterator. If this is null, this wrapper will not drop the iterator but just remove it from the data structure.
      • iterators

        protected final Map<UUID,​?> iterators
        The data structure providing the current iterators indexed by the assigned iterator ID. This is just for removing iterators of which the alive time has elapsed.
      • removerService

        protected final ScheduledExecutorService removerService
        The scheduled executor service executing this wrapper. This is used for re-scheduling in case the alive time has been extended the time the wrapper is executed.
    • Constructor Detail

      • IteratorWrapper

        public IteratorWrapper​(UUID iteratorId,
                               T iterator,
                               long remAliveTimeExtension,
                               Consumer<T> dropper,
                               Map<UUID,​?> iterators,
                               ScheduledExecutorService removerService)
        Creates a new wrapper for the designated iterator having the designated ID.
        Parameters:
        iteratorId - The ID of the iterator.
        iterator - The iterator to wrap.
        remAliveTimeExtension - The time in milliseconds the alive time is extended if requested.
        dropper - The method called for dropping the iterator. If this is null, this wrapper will not drop the iterator but just remove it from the data structure.
        iterators - The data structure providing the current iterators indexed by the assigned iterator ID. This is just for removing iterators of which the alive time has elapsed.
        removerService - The scheduled executor service executing this wrapper. This is used for re-scheduling in case the alive time has been extended the time the wrapper is executed.
    • Method Detail

      • getRemainingAliveTime

        protected long getRemainingAliveTime()
        Gets the remaining alive time in milliseconds before this wrapper will removed and possibly drop the iterator. The alive time may be extended in the future.
        To use the alive time of the underlying iterator, override this method appropriately.
        Returns:
        The remaining alive time in milliseconds before this wrapper will removed and possibly drop the iterator.
      • extendRemainingAliveTime

        public long extendRemainingAliveTime()
        Extends the remaining alive time by the usual period for extension and returns the new remaining time.
        To extend the alive time of the underlying iterator, override this method appropriately.
        Returns:
        The remaining alive time in milliseconds after extending the alive time of this wrapper and/or the underlying iterator.
      • getIterator

        public T getIterator()
        Gets the wrapped iterator.
        Returns:
        The wrapped iterator.
      • run

        public void run()
        Specified by:
        run in interface Runnable