Class IndexTrackingIterator<T>

java.lang.Object
com.github.javaparser.printer.lexicalpreservation.IndexTrackingIterator<T>
Type Parameters:
T - the type of elements in the list
All Implemented Interfaces:
Iterator<T>, ListIterator<T>

public class IndexTrackingIterator<T> extends Object implements ListIterator<T>
A generic iterator that tracks the index of the current element.

This iterator wraps a standard ListIterator and maintains the index of the most recently read element, which can be accessed via currentIndex().

This class provides a generic implementation that can be used with any type of list elements. It correctly handles bidirectional iteration by tracking the current index internally.

This iterator is particularly useful for operations that need to know the position of elements during iteration, such as calculating correspondences between lists or tracking modifications.

Since:
3.28.0
  • Constructor Details

    • IndexTrackingIterator

      public IndexTrackingIterator(List<T> elements)
      Creates an iterator starting at the beginning of the list.
      Parameters:
      elements - the list to iterate over
    • IndexTrackingIterator

      public IndexTrackingIterator(List<T> 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:
      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 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.

      Note: In the legacy ArrayIterator class, this method was named index(). It has been renamed to currentIndex() for better clarity and consistency.

      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 Iterator<T>
      Specified by:
      hasNext in interface ListIterator<T>
    • next

      public T next()
      Specified by:
      next in interface Iterator<T>
      Specified by:
      next in interface ListIterator<T>
    • hasPrevious

      public boolean hasPrevious()
      Specified by:
      hasPrevious in interface ListIterator<T>
    • previous

      public T previous()
      Specified by:
      previous in interface ListIterator<T>
    • nextIndex

      public int nextIndex()
      Specified by:
      nextIndex in interface ListIterator<T>
    • previousIndex

      public int previousIndex()
      Specified by:
      previousIndex in interface ListIterator<T>
    • remove

      public void remove()
      Specified by:
      remove in interface Iterator<T>
      Specified by:
      remove in interface ListIterator<T>
    • set

      public void set(T element)
      Specified by:
      set in interface ListIterator<T>
    • add

      public void add(T element)
      Specified by:
      add in interface ListIterator<T>