Class TextElementList
- All Implemented Interfaces:
TextElementSequence
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
ConstructorsConstructorDescriptionTextElementList(List<TextElement> elements) Creates a wrapper around the given list. -
Method Summary
Modifier and TypeMethodDescriptionbooleanallMatch(Predicate<TextElement> predicate) Tests whether all elements in this sequence match the given predicate.booleananyMatch(Predicate<TextElement> predicate) Tests whether any element in this sequence matches the given predicate.static TextElementListcopyOf(List<TextElement> elements) Creates a new TextElementList with a copy of the given list.static TextElementListempty()Creates an empty mutable TextElementList.booleanintfindFirst(Predicate<TextElement> predicate) Finds the first index where the predicate matches, searching forward from index 0.intfindLast(Predicate<TextElement> predicate) Finds the last index where the predicate matches, searching backward from the end.intfindNext(int fromIndex, Predicate<TextElement> predicate) Finds the next index where the predicate matches, searching forward from fromIndex (inclusive).intfindPrevious(int fromIndex, Predicate<TextElement> predicate) Finds the previous index where the predicate matches, searching backward from fromIndex (inclusive).get(int index) Returns the element at the specified index.inthashCode()voidinsert(int index, TextElement element) Inserts element at the specified index.voidinsertAll(int index, List<TextElement> elementsToInsert) Inserts all elements at the specified index.booleanisEmpty()Checks if this sequence is empty.booleanisValidIndex(int index) Checks if the index is valid (0 invalid input: '<'= index invalid input: '<' size).iterator(int fromIndex) Returns an iterator starting at the specified index.booleannoneMatch(Predicate<TextElement> predicate) Tests whether no elements in this sequence match the given predicate.static TextElementListof(TextElement... elements) Creates a new TextElementList containing the given elements.static TextElementListof(List<TextElement> elements) Creates a new TextElementList wrapping the given list.voidremove(int index) Removes the element at the specified index.voidremoveRange(int fromIndex, int toIndex) Removes elements in range [fromIndex, toIndex] (inclusive on both ends).intsize()Returns the number of elements in this sequence.subList(int fromIndex, int toIndex) Returns a sublist view [fromIndex, toIndex).takeWhile(Predicate<TextElement> predicate) Returns a new list containing elements from the start until the predicate fails.toList()Returns an unmodifiable view of the underlying list.Returns the underlying mutable list.toString()Methods inherited from interface TextElementSequence
indexOf, indexOf, iterator, lastIndexOf, lastIndexOf, stream
-
Constructor Details
-
TextElementList
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
Creates a new TextElementList containing the given elements.- Parameters:
elements- varargs of elements- Returns:
- a new list
-
of
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
Creates an empty mutable TextElementList.- Returns:
- an empty list
-
copyOf
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
Description copied from interface:TextElementSequenceFinds the first index where the predicate matches, searching forward from index 0.- Specified by:
findFirstin interfaceTextElementSequence- Parameters:
predicate- the condition to test- Returns:
- the first matching index, or -1 if no match found
-
findLast
Description copied from interface:TextElementSequenceFinds the last index where the predicate matches, searching backward from the end.- Specified by:
findLastin interfaceTextElementSequence- Parameters:
predicate- the condition to test- Returns:
- the last matching index, or -1 if no match found
-
findNext
Description copied from interface:TextElementSequenceFinds the next index where the predicate matches, searching forward from fromIndex (inclusive).- Specified by:
findNextin interfaceTextElementSequence- Parameters:
fromIndex- the starting index (inclusive)predicate- the condition to test- Returns:
- the next matching index, or -1 if no match found
-
findPrevious
Description copied from interface:TextElementSequenceFinds the previous index where the predicate matches, searching backward from fromIndex (inclusive).- Specified by:
findPreviousin interfaceTextElementSequence- Parameters:
fromIndex- the starting index (inclusive)predicate- the condition to test- Returns:
- the previous matching index, or -1 if no match found
-
takeWhile
Description copied from interface:TextElementSequenceReturns a new list containing elements from the start until the predicate fails. The returned list is independent of this sequence.- Specified by:
takeWhilein interfaceTextElementSequence- Parameters:
predicate- the condition to test- Returns:
- a new list of matching elements
-
subList
Description copied from interface:TextElementSequenceReturns a sublist view [fromIndex, toIndex). The returned list is backed by this sequence, so changes affect both.- Specified by:
subListin interfaceTextElementSequence- Parameters:
fromIndex- low endpoint (inclusive)toIndex- high endpoint (exclusive)- Returns:
- a sublist view
-
insert
Description copied from interface:TextElementSequenceInserts element at the specified index. WARNING: Caller must adjust subsequent indices manually.- Specified by:
insertin interfaceTextElementSequence- Parameters:
index- position to insert atelement- element to insert
-
insertAll
Description copied from interface:TextElementSequenceInserts all elements at the specified index. WARNING: Caller must adjust subsequent indices manually.- Specified by:
insertAllin interfaceTextElementSequence- Parameters:
index- position to insert atelementsToInsert- elements to insert
-
remove
public void remove(int index) Description copied from interface:TextElementSequenceRemoves the element at the specified index. WARNING: Caller must adjust subsequent indices manually.- Specified by:
removein interfaceTextElementSequence- Parameters:
index- position to remove from
-
removeRange
public void removeRange(int fromIndex, int toIndex) Description copied from interface:TextElementSequenceRemoves elements in range [fromIndex, toIndex] (inclusive on both ends). WARNING: Caller must adjust subsequent indices manually.- Specified by:
removeRangein interfaceTextElementSequence- Parameters:
fromIndex- start of range (inclusive)toIndex- end of range (inclusive)
-
get
Description copied from interface:TextElementSequenceReturns the element at the specified index.- Specified by:
getin interfaceTextElementSequence- Parameters:
index- the index- Returns:
- the element at that position
-
isValidIndex
public boolean isValidIndex(int index) Description copied from interface:TextElementSequenceChecks if the index is valid (0 invalid input: '<'= index invalid input: '<' size).- Specified by:
isValidIndexin interfaceTextElementSequence- Parameters:
index- the index to check- Returns:
- true if index is valid
-
size
public int size()Description copied from interface:TextElementSequenceReturns the number of elements in this sequence.- Specified by:
sizein interfaceTextElementSequence- Returns:
- the size
-
isEmpty
public boolean isEmpty()Description copied from interface:TextElementSequenceChecks if this sequence is empty.- Specified by:
isEmptyin interfaceTextElementSequence- Returns:
- true if size is 0
-
toList
Description copied from interface:TextElementSequenceReturns an unmodifiable view of the underlying list. Changes to the original list are visible in the returned view.- Specified by:
toListin interfaceTextElementSequence- Returns:
- an unmodifiable list view
-
toMutableList
Description copied from interface:TextElementSequenceReturns the underlying mutable list.WARNING: This exposes the internal list directly. Modifications will affect this sequence.
- Specified by:
toMutableListin interfaceTextElementSequence- Returns:
- the mutable list
-
anyMatch
Description copied from interface:TextElementSequenceTests 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:
anyMatchin interfaceTextElementSequence- Parameters:
predicate- the predicate to test elements against- Returns:
- true if any element matches the predicate, false otherwise (returns false for empty sequences)
-
allMatch
Description copied from interface:TextElementSequenceTests 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:
allMatchin interfaceTextElementSequence- Parameters:
predicate- the predicate to test elements against- Returns:
- true if all elements match the predicate (or sequence is empty), false otherwise
-
noneMatch
Description copied from interface:TextElementSequenceTests 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:
noneMatchin interfaceTextElementSequence- Parameters:
predicate- the predicate to test elements against- Returns:
- true if no elements match the predicate (or sequence is empty), false otherwise
-
iterator
Description copied from interface:TextElementSequenceReturns an iterator starting at the specified index.- Specified by:
iteratorin interfaceTextElementSequence- Parameters:
fromIndex- the starting position- Returns:
- an iterator with position tracking
-
toString
-
equals
-
hashCode
-