Class NodeList<T extends ASTNode>

  • 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 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 elements
        initialCapacity - 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 elements
        elements - the initial elements
    • Method Detail

      • remove

        public boolean remove​(Object o)
        Removes a specific node, matching by identity rather than by equals as List specifies. Types compare by value, so a list such as the type arguments of Map<String, String> holds equal entries that an equals-based search cannot tell apart.
        Specified by:
        remove in interface Collection<T extends ASTNode>
        Specified by:
        remove in interface List<T extends ASTNode>
        Overrides:
        remove in class AbstractCollection<T extends ASTNode>
        Parameters:
        o - the node instance to remove
        Returns:
        true if the instance was present
      • 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 replace
        newNode - 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
      • nodeStream

        public Stream<T> nodeStream()
        Returns:
        a stream over the elements
      • empty

        public static <T extends ASTNodeNodeList<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 ASTNodeNodeList<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 elements
        elements - the initial elements
        Returns:
        the populated list
      • copyOf

        public static <T extends ASTNodeNodeList<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 elements
        elements - the elements to copy
        Returns:
        the populated list