Class BoundedIterator<E>

  • All Implemented Interfaces:
    java.util.Iterator<E>

    public class BoundedIterator<E>
    extends java.lang.Object
    implements java.util.Iterator<E>
    Decorates another iterator to return elements in a specific range.

    The decorated iterator is bounded in the range [offset, offset+max). The offset corresponds to the position of the first element to be returned from the decorated iterator, and max is the maximum number of elements to be returned at most.

    In case an offset parameter other than 0 is provided, the decorated iterator is immediately advanced to this position, skipping all elements before that position.

    Since:
    4.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Iterator<? extends E> iterator
      The iterator being decorated.
      private long max
      The max number of elements to return
      private long offset
      The offset to bound the first element return
      private long pos
      The position of the current element
    • Constructor Summary

      Constructors 
      Constructor Description
      BoundedIterator​(java.util.Iterator<? extends E> iterator, long offset, long max)
      Decorates the specified iterator to return at most the given number of elements, skipping all elements until the iterator reaches the position at offset.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private boolean checkBounds()
      Checks whether the iterator is still within its bounded range.
      boolean hasNext()  
      private void init()
      Advances the underlying iterator to the beginning of the bounded range.
      E next()  
      void remove()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Field Detail

      • iterator

        private final java.util.Iterator<? extends E> iterator
        The iterator being decorated.
      • offset

        private final long offset
        The offset to bound the first element return
      • max

        private final long max
        The max number of elements to return
      • pos

        private long pos
        The position of the current element
    • Constructor Detail

      • BoundedIterator

        public BoundedIterator​(java.util.Iterator<? extends E> iterator,
                               long offset,
                               long max)
        Decorates the specified iterator to return at most the given number of elements, skipping all elements until the iterator reaches the position at offset.

        The iterator is immediately advanced until it reaches the position at offset, incurring O(n) time.

        Parameters:
        iterator - the iterator to be decorated
        offset - the index of the first element of the decorated iterator to return
        max - the maximum number of elements of the decorated iterator to return
        Throws:
        java.lang.NullPointerException - if iterator is null
        java.lang.IllegalArgumentException - if either offset or max is negative
    • Method Detail

      • init

        private void init()
        Advances the underlying iterator to the beginning of the bounded range.
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<E>
      • checkBounds

        private boolean checkBounds()
        Checks whether the iterator is still within its bounded range.
        Returns:
        true if the iterator is within its bounds, false otherwise
      • next

        public E next()
        Specified by:
        next in interface java.util.Iterator<E>
      • remove

        public void remove()

        In case an offset other than 0 was specified, the underlying iterator will be advanced to this position upon creation. A call to remove() will still result in an IllegalStateException if no explicit call to next() has been made prior to calling remove().

        Specified by:
        remove in interface java.util.Iterator<E>