Class IteratorWithBuffer<T>

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

    public class IteratorWithBuffer<T>
    extends java.lang.Object
    implements java.util.Iterator<T>
    Iterator that delays output by N slots so you can react to the output before it's yielded. See also PeekIterator (which predates this code). See also IteratorWithHistory for an iterator that remembers what it has yielded.
    See Also:
    PeekIterator, PushbackIterator, IteratorWithHistory
    • Constructor Summary

      Constructors 
      Constructor Description
      IteratorWithBuffer​(java.util.Iterator<T> iter, int N)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int currentSize()
      Return the current size of the lookahead.
      boolean hasNext()  
      T next()  
      T peek​(int idx)
      Look at elements that will be returned by a subsequent call of .next().
      void set​(int idx, T item)
      Set the element to be returned by a subsequent .next().
      • Methods inherited from class java.lang.Object

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

        forEachRemaining, remove
    • Constructor Detail

      • IteratorWithBuffer

        public IteratorWithBuffer​(java.util.Iterator<T> iter,
                                  int N)
    • Method Detail

      • hasNext

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

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

        public T peek​(int idx)
        Look at elements that will be returned by a subsequent call of .next(). The next element is index 0, then index 1 etc. This operation is valid immediately after the constructor returns. Returns null for no such element (underlying iterator didn't yield enough elements). Throws IndexOutOfBoundsException if an attempt is made to go beyond the buffering window.
      • currentSize

        public int currentSize()
        Return the current size of the lookahead. This can be used to tell the difference between an iterator returning null and an iterator that is just short.
      • set

        public void set​(int idx,
                        T item)
        Set the element to be returned by a subsequent .next(). Use with care. The original element to be returned at this position is lost.