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 ifremove()
,#add()
,#set()
} is called directly after a call topeek()
orelement()
.- Since:
- 4.0
-
-
Constructor Summary
Constructors Constructor Description PeekingIterator(List<E> list)
Constructor.PeekingIterator(ListIterator<E> iterator)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(E e)
int
currentIndex()
E
element()
Returns the next element in iteration without advancing the underlying iterator.boolean
hasNext()
boolean
hasPrevious()
E
next()
int
nextIndex()
E
peek()
Returns the next element in iteration without advancing the underlying iterator.<E> PeekingIterator<E>
peekingIterator(ListIterator<E> iterator)
Decorates the specified iterator to support one-element lookahead.E
previous()
int
previousIndex()
void
remove()
void
set(E e)
-
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
-
-
-
-
Constructor Detail
-
PeekingIterator
public PeekingIterator(ListIterator<E> iterator)
Constructor.- Parameters:
iterator
- the iterator to decorate
-
-
Method Detail
-
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
-
hasNext
public boolean hasNext()
-
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, useelement()
instead.The rationale behind this is to follow the
Queue
interface which uses the same terminology.- Specified by:
peek
in interfaceLookaheadIterator<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 interfaceLookaheadIterator<E>
- Returns:
- the next element from the iterator
- Throws:
NoSuchElementException
- if the iterator is already exhausted according tohasNext()
-
next
public E next()
-
remove
public void remove()
- Specified by:
remove
in interfaceIterator<E>
- Specified by:
remove
in interfaceListIterator<E>
- Throws:
IllegalStateException
- ifpeek()
orelement()
has been called prior to the call toremove()
-
hasPrevious
public boolean hasPrevious()
- Specified by:
hasPrevious
in interfaceListIterator<E>
-
previous
public E previous()
- Specified by:
previous
in interfaceListIterator<E>
-
nextIndex
public int nextIndex()
- Specified by:
nextIndex
in interfaceListIterator<E>
-
currentIndex
public int currentIndex()
-
previousIndex
public int previousIndex()
- Specified by:
previousIndex
in interfaceListIterator<E>
-
set
public void set(E e)
- Specified by:
set
in interfaceListIterator<E>
-
add
public void add(E e)
- Specified by:
add
in interfaceListIterator<E>
-
-