Class TextElementIterator
- java.lang.Object
-
- com.github.javaparser.printer.lexicalpreservation.TextElementIterator
-
- All Implemented Interfaces:
java.util.Iterator<TextElement>,java.util.ListIterator<TextElement>
public class TextElementIterator extends java.lang.Object implements java.util.ListIterator<TextElement>
Iterator that tracks its current position in a TextElement list.Unlike standard
ListIterator, this class provides direct access to the current index viacurrentIndex(), which returns the index of the element that was returned by the most recent call tonext()orprevious().This iterator is a replacement for the internal
ArrayIteratorclass, with clearer semantics and better naming. It correctly handles bidirectional iteration by maintaining the current index internally.- Since:
- 3.28.0
-
-
Constructor Summary
Constructors Constructor Description TextElementIterator(java.util.List<TextElement> elements, int fromIndex)Creates an iterator starting at the specified index.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(TextElement element)intcurrentIndex()Returns the index of the element that was returned by the most recent call tonext()orprevious().booleanhasNext()booleanhasPrevious()TextElementnext()intnextIndex()TextElementprevious()intpreviousIndex()voidremove()voidset(TextElement element)
-
-
-
Constructor Detail
-
TextElementIterator
public TextElementIterator(java.util.List<TextElement> elements, int fromIndex)
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:
java.lang.IndexOutOfBoundsException- if fromIndex is out of range
-
-
Method Detail
-
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 interfacejava.util.Iterator<TextElement>- Specified by:
hasNextin interfacejava.util.ListIterator<TextElement>
-
next
public TextElement next()
- Specified by:
nextin interfacejava.util.Iterator<TextElement>- Specified by:
nextin interfacejava.util.ListIterator<TextElement>
-
hasPrevious
public boolean hasPrevious()
- Specified by:
hasPreviousin interfacejava.util.ListIterator<TextElement>
-
previous
public TextElement previous()
- Specified by:
previousin interfacejava.util.ListIterator<TextElement>
-
nextIndex
public int nextIndex()
- Specified by:
nextIndexin interfacejava.util.ListIterator<TextElement>
-
previousIndex
public int previousIndex()
- Specified by:
previousIndexin interfacejava.util.ListIterator<TextElement>
-
remove
public void remove()
- Specified by:
removein interfacejava.util.Iterator<TextElement>- Specified by:
removein interfacejava.util.ListIterator<TextElement>
-
set
public void set(TextElement element)
- Specified by:
setin interfacejava.util.ListIterator<TextElement>
-
add
public void add(TextElement element)
- Specified by:
addin interfacejava.util.ListIterator<TextElement>
-
-