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>
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()
} is called directly after a call to
invalid reference
#set()peek() or element().
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanIndicates that the decorated iterator is exhausted.private final ListIterator<E> The iterator being decorated.private EThe current slot for lookahead.private booleanIndicates if the lookahead slot is filled. -
Constructor Summary
ConstructorsConstructorDescriptionPeekingIterator(List<E> list) Constructor.PeekingIterator(ListIterator<E> iterator) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidintelement()Returns the next element in iteration without advancing the underlying iterator.private voidfill()booleanhasNext()booleannext()intpeek()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.previous()intvoidremove()voidMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterator
forEachRemaining
-
Field Details
-
iterator
The iterator being decorated. -
exhausted
private boolean exhaustedIndicates that the decorated iterator is exhausted. -
slotFilled
private boolean slotFilledIndicates if the lookahead slot is filled. -
slot
The current slot for lookahead.
-
-
Constructor Details
-
PeekingIterator
Constructor.- Parameters:
iterator- the iterator to decorate
-
PeekingIterator
-
-
Method Details
-
peekingIterator
Decorates the specified iterator to support one-element lookahead.If the iterator is already a
PeekingIteratorit 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
-
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
NoSuchElementExceptionif the iterator is already exhausted. If you want such a behavior, useelement()instead.The rationale behind this is to follow the
Queueinterface which uses the same terminology.- Specified by:
peekin interfaceLookaheadIterator<E>- Returns:
- the next element from the iterator
-
element
Returns the next element in iteration without advancing the underlying iterator. If the iterator is already exhausted, null will be returned.- Specified by:
elementin interfaceLookaheadIterator<E>- Returns:
- the next element from the iterator
- Throws:
NoSuchElementException- if the iterator is already exhausted according tohasNext()
-
next
-
remove
public void remove()- Specified by:
removein interfaceIterator<E>- Specified by:
removein interfaceListIterator<E>- Throws:
IllegalStateException- ifpeek()orelement()has been called prior to the call toremove()
-
hasPrevious
public boolean hasPrevious()- Specified by:
hasPreviousin interfaceListIterator<E>
-
previous
- Specified by:
previousin interfaceListIterator<E>
-
nextIndex
public int nextIndex()- Specified by:
nextIndexin interfaceListIterator<E>
-
currentIndex
public int currentIndex() -
previousIndex
public int previousIndex()- Specified by:
previousIndexin interfaceListIterator<E>
-
set
- Specified by:
setin interfaceListIterator<E>
-
add
- Specified by:
addin interfaceListIterator<E>
-