Package com.tonic.analysis.source.ast
Class NodeList<T extends ASTNode>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<T>
-
- com.tonic.analysis.source.ast.NodeList<T>
-
- Type Parameters:
T- the type of AST nodes stored in this list
- All Implemented Interfaces:
Iterable<T>,Collection<T>,List<T>,RandomAccess
public class NodeList<T extends ASTNode> extends AbstractList<T> implements RandomAccess
List of AST nodes that keeps parent links consistent: adding sets each element's parent to the owner, removing clears it.
-
-
Constructor Summary
Constructors Constructor Description NodeList(ASTNode owner)Creates an empty list attached to an owner.NodeList(ASTNode owner, int initialCapacity)Creates an empty list attached to an owner with a backing capacity hint.NodeList(ASTNode owner, Collection<? extends T> elements)Creates a list attached to an owner, adopting the given elements.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, T element)booleanadd(T element)booleanaddAll(int index, Collection<? extends T> c)booleanaddAll(Collection<? extends T> c)NodeList<T>addNode(T element)Adds an element and returns this list for chaining.NodeList<T>addNodes(T... elements)Adds all given elements and returns this list for chaining.voidclear()booleancontains(T node)Tests whether a node is present.static <T extends ASTNode>
NodeList<T>copyOf(ASTNode owner, Collection<? extends T> elements)Creates a list copying the given elements, attached to an owner.static <T extends ASTNode>
NodeList<T>empty(ASTNode owner)Creates an empty list attached to an owner.voidforEachNode(Consumer<? super T> action)Applies an action to each element.Tget(int index)TgetFirst()Returns the first element.Optional<T>getFirstOptional()TgetLast()Returns the last element.Optional<T>getLastOptional()ASTNodegetOwner()Stream<T>nodeStream()static <T extends ASTNode>
NodeList<T>of(ASTNode owner, T... elements)Creates a list of the given elements attached to an owner.Tremove(int index)booleanremove(Object o)Removes a specific node, matching by identity rather than by equals asListspecifies.booleanremoveAll(Collection<?> c)booleanremoveIf(Predicate<? super T> filter)voidreplace(T oldNode, T newNode)Swaps one node for another in place, doing nothing if the old node is absent.booleanretainAll(Collection<?> c)Tset(int index, T element)intsize()-
Methods inherited from class java.util.AbstractList
equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, subList
-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, toArray, toArray, toString
-
Methods inherited from interface java.util.Collection
parallelStream, stream, toArray
-
Methods inherited from interface java.util.List
contains, containsAll, isEmpty, replaceAll, sort, spliterator, toArray, toArray
-
-
-
-
Constructor Detail
-
NodeList
public NodeList(ASTNode owner)
Creates an empty list attached to an owner.- Parameters:
owner- the node that parents added elements
-
NodeList
public NodeList(ASTNode owner, int initialCapacity)
Creates an empty list attached to an owner with a backing capacity hint.- Parameters:
owner- the node that parents added elementsinitialCapacity- the initial backing capacity
-
NodeList
public NodeList(ASTNode owner, Collection<? extends T> elements)
Creates a list attached to an owner, adopting the given elements.- Parameters:
owner- the node that parents added elementselements- the initial elements
-
-
Method Detail
-
get
public T get(int index)
-
size
public int size()
-
add
public boolean add(T element)
-
add
public void add(int index, T element)
-
remove
public T remove(int index)
-
remove
public boolean remove(Object o)
Removes a specific node, matching by identity rather than by equals asListspecifies. Types compare by value, so a list such as the type arguments ofMap<String, String>holds equal entries that an equals-based search cannot tell apart.
-
clear
public void clear()
-
addAll
public boolean addAll(Collection<? extends T> c)
-
addAll
public boolean addAll(int index, Collection<? extends T> c)
-
removeAll
public boolean removeAll(Collection<?> c)
-
retainAll
public boolean retainAll(Collection<?> c)
-
removeIf
public boolean removeIf(Predicate<? super T> filter)
- Specified by:
removeIfin interfaceCollection<T extends ASTNode>
-
addNode
public NodeList<T> addNode(T element)
Adds an element and returns this list for chaining.- Parameters:
element- the node to add- Returns:
- this list
-
addNodes
@SafeVarargs public final NodeList<T> addNodes(T... elements)
Adds all given elements and returns this list for chaining.- Parameters:
elements- the nodes to add- Returns:
- this list
-
getOwner
public ASTNode getOwner()
- Returns:
- the owner
-
getFirst
public T getFirst()
Returns the first element.- Returns:
- the first element
- Throws:
NoSuchElementException- if the list is empty
-
getFirstOptional
public Optional<T> getFirstOptional()
- Returns:
- the first element, or empty if the list is empty
-
getLast
public T getLast()
Returns the last element.- Returns:
- the last element
- Throws:
NoSuchElementException- if the list is empty
-
getLastOptional
public Optional<T> getLastOptional()
- Returns:
- the last element, or empty if the list is empty
-
replace
public void replace(T oldNode, T newNode)
Swaps one node for another in place, doing nothing if the old node is absent. The old node is matched by identity, so a value-equal sibling is never swapped by mistake.- Parameters:
oldNode- the node instance to replacenewNode- the replacement
-
contains
public boolean contains(T node)
Tests whether a node is present.- Parameters:
node- the node to look for- Returns:
- true if the node is in this list
-
forEachNode
public void forEachNode(Consumer<? super T> action)
Applies an action to each element.- Parameters:
action- the action to apply
-
empty
public static <T extends ASTNode> NodeList<T> empty(ASTNode owner)
Creates an empty list attached to an owner.- Type Parameters:
T- the element node type- Parameters:
owner- the node that parents added elements- Returns:
- the empty list
-
of
@SafeVarargs public static <T extends ASTNode> NodeList<T> of(ASTNode owner, T... elements)
Creates a list of the given elements attached to an owner.- Type Parameters:
T- the element node type- Parameters:
owner- the node that parents added elementselements- the initial elements- Returns:
- the populated list
-
copyOf
public static <T extends ASTNode> NodeList<T> copyOf(ASTNode owner, Collection<? extends T> elements)
Creates a list copying the given elements, attached to an owner.- Type Parameters:
T- the element node type- Parameters:
owner- the node that parents added elementselements- the elements to copy- Returns:
- the populated list
-
-