Class TextElementList

  • All Implemented Interfaces:
    TextElementSequence

    public class TextElementList
    extends java.lang.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 Summary

      Constructors 
      Constructor Description
      TextElementList​(java.util.List<TextElement> elements)
      Creates a wrapper around the given list.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean allMatch​(java.util.function.Predicate<TextElement> predicate)
      Tests whether all elements in this sequence match the given predicate.
      boolean anyMatch​(java.util.function.Predicate<TextElement> predicate)
      Tests whether any element in this sequence matches the given predicate.
      static TextElementList copyOf​(java.util.List<TextElement> elements)
      Creates a new TextElementList with a copy of the given list.
      static TextElementList empty()
      Creates an empty mutable TextElementList.
      boolean equals​(java.lang.Object obj)  
      int findFirst​(java.util.function.Predicate<TextElement> predicate)
      Finds the first index where the predicate matches, searching forward from index 0.
      int findLast​(java.util.function.Predicate<TextElement> predicate)
      Finds the last index where the predicate matches, searching backward from the end.
      int findNext​(int fromIndex, java.util.function.Predicate<TextElement> predicate)
      Finds the next index where the predicate matches, searching forward from fromIndex (inclusive).
      int findPrevious​(int fromIndex, java.util.function.Predicate<TextElement> predicate)
      Finds the previous index where the predicate matches, searching backward from fromIndex (inclusive).
      TextElement get​(int index)
      Returns the element at the specified index.
      int hashCode()  
      void insert​(int index, TextElement element)
      Inserts element at the specified index.
      void insertAll​(int index, java.util.List<TextElement> elementsToInsert)
      Inserts all elements at the specified index.
      boolean isEmpty()
      Checks if this sequence is empty.
      boolean isValidIndex​(int index)
      Checks if the index is valid (0 <= index < size).
      TextElementIterator iterator​(int fromIndex)
      Returns an iterator starting at the specified index.
      boolean noneMatch​(java.util.function.Predicate<TextElement> predicate)
      Tests whether no elements in this sequence match the given predicate.
      static TextElementList of​(TextElement... elements)
      Creates a new TextElementList containing the given elements.
      static TextElementList of​(java.util.List<TextElement> elements)
      Creates a new TextElementList wrapping the given list.
      void remove​(int index)
      Removes the element at the specified index.
      void removeRange​(int fromIndex, int toIndex)
      Removes elements in range [fromIndex, toIndex] (inclusive on both ends).
      int size()
      Returns the number of elements in this sequence.
      java.util.List<TextElement> subList​(int fromIndex, int toIndex)
      Returns a sublist view [fromIndex, toIndex).
      java.util.List<TextElement> takeWhile​(java.util.function.Predicate<TextElement> predicate)
      Returns a new list containing elements from the start until the predicate fails.
      java.util.List<TextElement> toList()
      Returns an unmodifiable view of the underlying list.
      java.util.List<TextElement> toMutableList()
      Returns the underlying mutable list.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • TextElementList

        public TextElementList​(java.util.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:
        java.lang.NullPointerException - if elements is null
    • Method Detail

      • 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​(java.util.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:
        java.lang.NullPointerException - if elements is null
      • empty

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

        public static TextElementList copyOf​(java.util.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:
        java.lang.NullPointerException - if elements is null
      • findFirst

        public int findFirst​(java.util.function.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​(java.util.function.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,
                            java.util.function.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,
                                java.util.function.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 java.util.List<TextElement> takeWhile​(java.util.function.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 java.util.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,
                              java.util.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 <= index < size).
        Specified by:
        isValidIndex in interface TextElementSequence
        Parameters:
        index - the index to check
        Returns:
        true if index is valid
      • toList

        public java.util.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 java.util.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​(java.util.function.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​(java.util.function.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​(java.util.function.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
      • toString

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

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object