Class PeekingIterator<E>

java.lang.Object
com.github.javaparser.printer.lexicalpreservation.PeekingIterator<E>
Type Parameters:
E - the type of elements returned by this iterator.
All Implemented Interfaces:
LookaheadIterator<E>, Iterator<E>, ListIterator<E>

public class PeekingIterator<E> extends Object implements ListIterator<E>, LookaheadIterator<E>
Decorates an iterator to support one-element lookahead while iterating.

The decorator supports the removal operation, but an IllegalStateException will be thrown if remove(),

invalid reference
#add()
,
invalid reference
#set()
} is called directly after a call to peek() or element().
Since:
4.0
  • Field Details

    • iterator

      private final ListIterator<E> iterator
      The iterator being decorated.
    • exhausted

      private boolean exhausted
      Indicates that the decorated iterator is exhausted.
    • slotFilled

      private boolean slotFilled
      Indicates if the lookahead slot is filled.
    • slot

      private E slot
      The current slot for lookahead.
  • Constructor Details

    • PeekingIterator

      public PeekingIterator(ListIterator<E> iterator)
      Constructor.
      Parameters:
      iterator - the iterator to decorate
    • PeekingIterator

      public PeekingIterator(List<E> list)
      Constructor.
      Parameters:
      list - the provider of the iterator to decorate
  • Method Details

    • peekingIterator

      public <E> PeekingIterator<E> peekingIterator(ListIterator<E> iterator)
      Decorates the specified iterator to support one-element lookahead.

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

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

      private void fill()
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<E>
      Specified by:
      hasNext in interface ListIterator<E>
    • peek

      public E peek()
      Returns the next element in iteration without advancing the underlying iterator. If the iterator is already exhausted, null will be returned.

      Note: this method does not throw a NoSuchElementException if the iterator is already exhausted. If you want such a behavior, use element() instead.

      The rationale behind this is to follow the Queue interface which uses the same terminology.

      Specified by:
      peek in interface LookaheadIterator<E>
      Returns:
      the next element from the iterator
    • element

      public E element()
      Returns the next element in iteration without advancing the underlying iterator. If the iterator is already exhausted, null will be returned.
      Specified by:
      element in interface LookaheadIterator<E>
      Returns:
      the next element from the iterator
      Throws:
      NoSuchElementException - if the iterator is already exhausted according to hasNext()
    • next

      public E next()
      Specified by:
      next in interface Iterator<E>
      Specified by:
      next in interface ListIterator<E>
    • remove

      public void remove()
      Specified by:
      remove in interface Iterator<E>
      Specified by:
      remove in interface ListIterator<E>
      Throws:
      IllegalStateException - if peek() or element() has been called prior to the call to remove()
    • hasPrevious

      public boolean hasPrevious()
      Specified by:
      hasPrevious in interface ListIterator<E>
    • previous

      public E previous()
      Specified by:
      previous in interface ListIterator<E>
    • nextIndex

      public int nextIndex()
      Specified by:
      nextIndex in interface ListIterator<E>
    • currentIndex

      public int currentIndex()
    • previousIndex

      public int previousIndex()
      Specified by:
      previousIndex in interface ListIterator<E>
    • set

      public void set(E e)
      Specified by:
      set in interface ListIterator<E>
    • add

      public void add(E e)
      Specified by:
      add in interface ListIterator<E>