Class 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 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 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 over
        fromIndex - 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 to next() or previous().

        This method can be called multiple times without side effects - it will always return the same value until the next call to next(), previous(), or remove().

        Important: If neither next() nor previous() has been called yet, or if remove() was called after the last call to next() or previous(), 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:
        hasNext in interface java.util.Iterator<TextElement>
        Specified by:
        hasNext in interface java.util.ListIterator<TextElement>
      • next

        public TextElement next()
        Specified by:
        next in interface java.util.Iterator<TextElement>
        Specified by:
        next in interface java.util.ListIterator<TextElement>
      • hasPrevious

        public boolean hasPrevious()
        Specified by:
        hasPrevious in interface java.util.ListIterator<TextElement>
      • previous

        public TextElement previous()
        Specified by:
        previous in interface java.util.ListIterator<TextElement>
      • nextIndex

        public int nextIndex()
        Specified by:
        nextIndex in interface java.util.ListIterator<TextElement>
      • previousIndex

        public int previousIndex()
        Specified by:
        previousIndex in interface java.util.ListIterator<TextElement>
      • remove

        public void remove()
        Specified by:
        remove in interface java.util.Iterator<TextElement>
        Specified by:
        remove in interface java.util.ListIterator<TextElement>
      • set

        public void set​(TextElement element)
        Specified by:
        set in interface java.util.ListIterator<TextElement>
      • add

        public void add​(TextElement element)
        Specified by:
        add in interface java.util.ListIterator<TextElement>