Class TextElementList
- java.lang.Object
-
- com.github.javaparser.printer.lexicalpreservation.TextElementList
-
- All Implemented Interfaces:
TextElementSequence
public class TextElementList extends java.lang.Object implements TextElementSequence
Default mutable implementation ofTextElementSequence.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 booleanallMatch(java.util.function.Predicate<TextElement> predicate)Tests whether all elements in this sequence match the given predicate.booleananyMatch(java.util.function.Predicate<TextElement> predicate)Tests whether any element in this sequence matches the given predicate.static TextElementListcopyOf(java.util.List<TextElement> elements)Creates a new TextElementList with a copy of the given list.static TextElementListempty()Creates an empty mutable TextElementList.booleanequals(java.lang.Object obj)intfindFirst(java.util.function.Predicate<TextElement> predicate)Finds the first index where the predicate matches, searching forward from index 0.intfindLast(java.util.function.Predicate<TextElement> predicate)Finds the last index where the predicate matches, searching backward from the end.intfindNext(int fromIndex, java.util.function.Predicate<TextElement> predicate)Finds the next index where the predicate matches, searching forward from fromIndex (inclusive).intfindPrevious(int fromIndex, java.util.function.Predicate<TextElement> predicate)Finds the previous index where the predicate matches, searching backward from fromIndex (inclusive).TextElementget(int index)Returns the element at the specified index.inthashCode()voidinsert(int index, TextElement element)Inserts element at the specified index.voidinsertAll(int index, java.util.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 <= index < size).TextElementIteratoriterator(int fromIndex)Returns an iterator starting at the specified index.booleannoneMatch(java.util.function.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(java.util.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.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.StringtoString()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.github.javaparser.printer.lexicalpreservation.TextElementSequence
indexOf, indexOf, iterator, lastIndexOf, lastIndexOf, stream
-
-
-
-
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: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
public int findLast(java.util.function.Predicate<TextElement> predicate)
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
public int findNext(int fromIndex, java.util.function.Predicate<TextElement> predicate)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
public int findPrevious(int fromIndex, java.util.function.Predicate<TextElement> predicate)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
public java.util.List<TextElement> takeWhile(java.util.function.Predicate<TextElement> predicate)
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
public java.util.List<TextElement> subList(int fromIndex, int toIndex)
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
public void insert(int index, TextElement element)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
public void insertAll(int index, java.util.List<TextElement> elementsToInsert)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
public TextElement get(int index)
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 <= index < 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
public java.util.List<TextElement> 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
public java.util.List<TextElement> 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
public boolean anyMatch(java.util.function.Predicate<TextElement> predicate)
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
public boolean allMatch(java.util.function.Predicate<TextElement> predicate)
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
public boolean noneMatch(java.util.function.Predicate<TextElement> predicate)
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
public TextElementIterator iterator(int fromIndex)
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
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-