Class PushbackIterator<E>

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

    public class PushbackIterator<E>
    extends java.lang.Object
    implements java.util.Iterator<E>
    Decorates an iterator to support pushback of elements.

    The decorator stores the pushed back elements in a LIFO manner: the last element that has been pushed back, will be returned as the next element in a call to next().

    The decorator does not support the removal operation. Any call to remove() will result in an UnsupportedOperationException.

    Since:
    4.0
    • Constructor Summary

      Constructors 
      Constructor Description
      PushbackIterator​(java.util.Iterator<? extends E> iterator)
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean hasNext()  
      E next()  
      void pushback​(E item)
      Push back the given element to the iterator.
      static <E> PushbackIterator<E> pushbackIterator​(java.util.Iterator<? extends E> iterator)
      Decorates the specified iterator to support one-element lookahead.
      void remove()
      This iterator will always throw an UnsupportedOperationException.
      • Methods inherited from class java.lang.Object

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

        forEachRemaining
    • Constructor Detail

      • PushbackIterator

        public PushbackIterator​(java.util.Iterator<? extends E> iterator)
        Constructor.
        Parameters:
        iterator - the iterator to decorate
    • Method Detail

      • pushbackIterator

        public static <E> PushbackIterator<E> pushbackIterator​(java.util.Iterator<? extends E> iterator)
        Decorates the specified iterator to support one-element lookahead.

        If the iterator is already a PushbackIterator it is returned directly.

        Type Parameters:
        E - the element type
        Parameters:
        iterator - the iterator to decorate
        Returns:
        a new peeking iterator
        Throws:
        java.lang.NullPointerException - if the iterator is null
      • pushback

        public void pushback​(E item)
        Push back the given element to the iterator.

        Calling next() immediately afterwards will return exactly this element.

        Parameters:
        item - the element to push back to the iterator
      • hasNext

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

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

        public void remove()
        This iterator will always throw an UnsupportedOperationException.
        Specified by:
        remove in interface java.util.Iterator<E>
        Throws:
        java.lang.UnsupportedOperationException - always