Class TextElementList

java.lang.Object
com.github.javaparser.printer.lexicalpreservation.TextElementList
All Implemented Interfaces:
TextElementSequence

public class TextElementList extends Object implements TextElementSequence
Default mutable implementation of TextElementSequence.

This class wraps an existing List<TextElement> without copying it. All mutations directly affect the underlying list.

The implementation provides optimized search operations and clear semantics for index-based manipulations required by lexical preservation operations.

Since:
3.28.0
  • Constructor Details

    • TextElementList

      public TextElementList(List<TextElement> elements)
      Creates a wrapper around the given list. The list is NOT copied, mutations affect the original.
      Parameters:
      elements - the list to wrap
      Throws:
      NullPointerException - if elements is null
  • Method Details

    • of

      public static TextElementList of(TextElement... elements)
      Creates a new TextElementList containing the given elements.
      Parameters:
      elements - varargs of elements
      Returns:
      a new list
    • of

      public static TextElementList of(List<TextElement> elements)
      Creates a new TextElementList wrapping the given list.

      IMPORTANT: This method wraps the list directly without copying. Modifications to the TextElementList will affect the original list. Use copyOf(List) if you need an independent copy.

      This method is useful for chaining operations:

      List<TextElement> result = TextElementList.of(list.subList(0, 10))
          .takeWhile(TextElement::isSpaceOrTab);
      
      Parameters:
      elements - the list to wrap (not copied)
      Returns:
      a new TextElementList wrapping the given list
      Throws:
      NullPointerException - if elements is null
    • empty

      public static TextElementList empty()
      Creates an empty mutable TextElementList.
      Returns:
      an empty list
    • copyOf

      public static TextElementList copyOf(List<TextElement> elements)
      Creates a new TextElementList with a copy of the given list.
      Parameters:
      elements - the list to copy
      Returns:
      a new list with copied elements
      Throws:
      NullPointerException - if elements is null
    • findFirst

      public int findFirst(Predicate<TextElement> predicate)
      Description copied from interface: TextElementSequence
      Finds the first index where the predicate matches, searching forward from index 0.
      Specified by:
      findFirst in interface TextElementSequence
      Parameters:
      predicate - the condition to test
      Returns:
      the first matching index, or -1 if no match found
    • findLast

      public int findLast(Predicate<TextElement> predicate)
      Description copied from interface: TextElementSequence
      Finds the last index where the predicate matches, searching backward from the end.
      Specified by:
      findLast in interface TextElementSequence
      Parameters:
      predicate - the condition to test
      Returns:
      the last matching index, or -1 if no match found
    • findNext

      public int findNext(int fromIndex, Predicate<TextElement> predicate)
      Description copied from interface: TextElementSequence
      Finds the next index where the predicate matches, searching forward from fromIndex (inclusive).
      Specified by:
      findNext in interface TextElementSequence
      Parameters:
      fromIndex - the starting index (inclusive)
      predicate - the condition to test
      Returns:
      the next matching index, or -1 if no match found
    • findPrevious

      public int findPrevious(int fromIndex, Predicate<TextElement> predicate)
      Description copied from interface: TextElementSequence
      Finds the previous index where the predicate matches, searching backward from fromIndex (inclusive).
      Specified by:
      findPrevious in interface TextElementSequence
      Parameters:
      fromIndex - the starting index (inclusive)
      predicate - the condition to test
      Returns:
      the previous matching index, or -1 if no match found
    • takeWhile

      public List<TextElement> takeWhile(Predicate<TextElement> predicate)
      Description copied from interface: TextElementSequence
      Returns a new list containing elements from the start until the predicate fails. The returned list is independent of this sequence.
      Specified by:
      takeWhile in interface TextElementSequence
      Parameters:
      predicate - the condition to test
      Returns:
      a new list of matching elements
    • subList

      public List<TextElement> subList(int fromIndex, int toIndex)
      Description copied from interface: TextElementSequence
      Returns a sublist view [fromIndex, toIndex). The returned list is backed by this sequence, so changes affect both.
      Specified by:
      subList in interface TextElementSequence
      Parameters:
      fromIndex - low endpoint (inclusive)
      toIndex - high endpoint (exclusive)
      Returns:
      a sublist view
    • insert

      public void insert(int index, TextElement element)
      Description copied from interface: TextElementSequence
      Inserts element at the specified index. WARNING: Caller must adjust subsequent indices manually.
      Specified by:
      insert in interface TextElementSequence
      Parameters:
      index - position to insert at
      element - element to insert
    • insertAll

      public void insertAll(int index, List<TextElement> elementsToInsert)
      Description copied from interface: TextElementSequence
      Inserts all elements at the specified index. WARNING: Caller must adjust subsequent indices manually.
      Specified by:
      insertAll in interface TextElementSequence
      Parameters:
      index - position to insert at
      elementsToInsert - elements to insert
    • remove

      public void remove(int index)
      Description copied from interface: TextElementSequence
      Removes the element at the specified index. WARNING: Caller must adjust subsequent indices manually.
      Specified by:
      remove in interface TextElementSequence
      Parameters:
      index - position to remove from
    • removeRange

      public void removeRange(int fromIndex, int toIndex)
      Description copied from interface: TextElementSequence
      Removes elements in range [fromIndex, toIndex] (inclusive on both ends). WARNING: Caller must adjust subsequent indices manually.
      Specified by:
      removeRange in interface TextElementSequence
      Parameters:
      fromIndex - start of range (inclusive)
      toIndex - end of range (inclusive)
    • get

      public TextElement get(int index)
      Description copied from interface: TextElementSequence
      Returns the element at the specified index.
      Specified by:
      get in interface TextElementSequence
      Parameters:
      index - the index
      Returns:
      the element at that position
    • isValidIndex

      public boolean isValidIndex(int index)
      Description copied from interface: TextElementSequence
      Checks if the index is valid (0 invalid input: '<'= index invalid input: '<' size).
      Specified by:
      isValidIndex in interface TextElementSequence
      Parameters:
      index - the index to check
      Returns:
      true if index is valid
    • size

      public int size()
      Description copied from interface: TextElementSequence
      Returns the number of elements in this sequence.
      Specified by:
      size in interface TextElementSequence
      Returns:
      the size
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: TextElementSequence
      Checks if this sequence is empty.
      Specified by:
      isEmpty in interface TextElementSequence
      Returns:
      true if size is 0
    • toList

      public List<TextElement> toList()
      Description copied from interface: TextElementSequence
      Returns an unmodifiable view of the underlying list. Changes to the original list are visible in the returned view.
      Specified by:
      toList in interface TextElementSequence
      Returns:
      an unmodifiable list view
    • toMutableList

      public List<TextElement> toMutableList()
      Description copied from interface: TextElementSequence
      Returns the underlying mutable list.

      WARNING: This exposes the internal list directly. Modifications will affect this sequence.

      Specified by:
      toMutableList in interface TextElementSequence
      Returns:
      the mutable list
    • anyMatch

      public boolean anyMatch(Predicate<TextElement> predicate)
      Description copied from interface: TextElementSequence
      Tests whether any element in this sequence matches the given predicate.

      This is a short-circuiting terminal operation: it stops as soon as a matching element is found and returns true immediately.

      Examples:

      // Check if list contains any comment
      boolean hasComment = list.anyMatch(TextElement::isComment);
      
      // Check if list contains any token with specific text
      boolean hasIdentifier = list.anyMatch(el ->
          el instanceof TokenTextElement &&
          ((TokenTextElement) el).getText().equals("myVar")
      );
      
      Specified by:
      anyMatch in interface TextElementSequence
      Parameters:
      predicate - the predicate to test elements against
      Returns:
      true if any element matches the predicate, false otherwise (returns false for empty sequences)
    • allMatch

      public boolean allMatch(Predicate<TextElement> predicate)
      Description copied from interface: TextElementSequence
      Tests whether all elements in this sequence match the given predicate.

      This is a short-circuiting terminal operation: it stops as soon as a non-matching element is found and returns false immediately.

      Returns true for empty sequences (vacuous truth).

      Examples:

      // Check if all elements are whitespace
      boolean allWhitespace = list.allMatch(TextElement::isSpaceOrTab);
      
      // Check if all elements are comments
      boolean allComments = list.allMatch(TextElement::isComment);
      
      Specified by:
      allMatch in interface TextElementSequence
      Parameters:
      predicate - the predicate to test elements against
      Returns:
      true if all elements match the predicate (or sequence is empty), false otherwise
    • noneMatch

      public boolean noneMatch(Predicate<TextElement> predicate)
      Description copied from interface: TextElementSequence
      Tests whether no elements in this sequence match the given predicate.

      This is a short-circuiting terminal operation: it stops as soon as a matching element is found and returns false immediately.

      Returns true for empty sequences.

      Equivalent to !anyMatch(predicate).

      Examples:

      // Check if list has no comments
      boolean noComments = list.noneMatch(TextElement::isComment);
      
      // Check if list has no newlines
      boolean noNewlines = list.noneMatch(TextElement::isNewline);
      
      Specified by:
      noneMatch in interface TextElementSequence
      Parameters:
      predicate - the predicate to test elements against
      Returns:
      true if no elements match the predicate (or sequence is empty), false otherwise
    • iterator

      public TextElementIterator iterator(int fromIndex)
      Description copied from interface: TextElementSequence
      Returns an iterator starting at the specified index.
      Specified by:
      iterator in interface TextElementSequence
      Parameters:
      fromIndex - the starting position
      Returns:
      an iterator with position tracking
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object