Class TextElementIterator
java.lang.Object
com.github.javaparser.printer.lexicalpreservation.TextElementIterator
- All Implemented Interfaces:
Iterator<TextElement>, ListIterator<TextElement>
Iterator that tracks its current position in a TextElement list.
Unlike standard ListIterator, this class provides direct access
to the current index via currentIndex(), which returns the index of
the element that was returned by the most recent call to next() or
previous().
This iterator is a replacement for the internal ArrayIterator class,
with clearer semantics and better naming. It correctly handles bidirectional
iteration by maintaining the current index internally.
- Since:
- 3.28.0
-
Constructor Summary
ConstructorsConstructorDescriptionTextElementIterator(List<TextElement> elements, int fromIndex) Creates an iterator starting at the specified index. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(TextElement element) intReturns the index of the element that was returned by the most recent call tonext()orprevious().booleanhasNext()booleannext()intprevious()intvoidremove()voidset(TextElement element) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterator
forEachRemaining
-
Constructor Details
-
TextElementIterator
Creates an iterator starting at the specified index. The current index is initialized to -1, indicating that no element has been read yet.- Parameters:
elements- the list to iterate overfromIndex- the starting index (cursor position)- Throws:
IndexOutOfBoundsException- if fromIndex is out of range
-
-
Method Details
-
currentIndex
public int currentIndex()Returns the index of the element that was returned by the most recent call tonext()orprevious().This method can be called multiple times without side effects - it will always return the same value until the next call to
next(),previous(), orremove().Important: If neither
next()norprevious()has been called yet, or ifremove()was called after the last call tonext()orprevious(), this method returns -1.- Returns:
- the index of the current element, or -1 if no element has been read or the current element was removed
-
hasNext
public boolean hasNext()- Specified by:
hasNextin interfaceIterator<TextElement>- Specified by:
hasNextin interfaceListIterator<TextElement>
-
next
- Specified by:
nextin interfaceIterator<TextElement>- Specified by:
nextin interfaceListIterator<TextElement>
-
hasPrevious
public boolean hasPrevious()- Specified by:
hasPreviousin interfaceListIterator<TextElement>
-
previous
- Specified by:
previousin interfaceListIterator<TextElement>
-
nextIndex
public int nextIndex()- Specified by:
nextIndexin interfaceListIterator<TextElement>
-
previousIndex
public int previousIndex()- Specified by:
previousIndexin interfaceListIterator<TextElement>
-
remove
public void remove()- Specified by:
removein interfaceIterator<TextElement>- Specified by:
removein interfaceListIterator<TextElement>
-
set
- Specified by:
setin interfaceListIterator<TextElement>
-
add
- Specified by:
addin interfaceListIterator<TextElement>
-