Class NodeList<N extends Node>

  • Type Parameters:
    N - the type of nodes contained.
    All Implemented Interfaces:
    Observable, Visitable, HasParentNode<NodeList<N>>, java.lang.Iterable<N>, java.util.Collection<N>, java.util.List<N>

    public class NodeList<N extends Node>
    extends java.lang.Object
    implements java.util.List<N>, java.lang.Iterable<N>, HasParentNode<NodeList<N>>, Visitable, Observable
    A list of nodes. It usually has a parent node. Unlike normal Nodes, this does not mean that it is a child of that parent. Instead, this list will make every node it contains a child of its parent. This way, a NodeList does not create an extra level inside the AST.
    • Field Detail

      • innerList

        private java.util.List<N extends Node> innerList
      • parentNode

        private Node parentNode
      • observers

        private java.util.List<AstObserver> observers
    • Constructor Detail

      • NodeList

        public NodeList()
      • NodeList

        public NodeList​(java.util.Collection<N> n)
      • NodeList

        @SafeVarargs
        public NodeList​(N... n)
    • Method Detail

      • add

        public boolean add​(N node)
        Specified by:
        add in interface java.util.Collection<N extends Node>
        Specified by:
        add in interface java.util.List<N extends Node>
      • own

        private void own​(N node)
      • remove

        public boolean remove​(Node node)
      • removeFirst

        public N removeFirst()
      • removeLast

        public N removeLast()
      • nodeList

        @SafeVarargs
        public static <X extends NodeNodeList<X> nodeList​(X... nodes)
      • nodeList

        public static <X extends NodeNodeList<X> nodeList​(java.util.Collection<X> nodes)
      • contains

        public boolean contains​(N node)
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<N extends Node>
        Specified by:
        size in interface java.util.List<N extends Node>
      • get

        public N get​(int i)
        Specified by:
        get in interface java.util.List<N extends Node>
      • iterator

        public java.util.Iterator<N> iterator()
        Specified by:
        iterator in interface java.util.Collection<N extends Node>
        Specified by:
        iterator in interface java.lang.Iterable<N extends Node>
        Specified by:
        iterator in interface java.util.List<N extends Node>
      • set

        public N set​(int index,
                     N element)
        Specified by:
        set in interface java.util.List<N extends Node>
      • remove

        public N remove​(int index)
        Specified by:
        remove in interface java.util.List<N extends Node>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<N extends Node>
        Specified by:
        isEmpty in interface java.util.List<N extends Node>
      • sort

        public void sort​(java.util.Comparator<? super N> comparator)
        Specified by:
        sort in interface java.util.List<N extends Node>
      • addAll

        public void addAll​(NodeList<N> otherList)
      • add

        public void add​(int index,
                        N node)
        Specified by:
        add in interface java.util.List<N extends Node>
      • addFirst

        public NodeList<N> addFirst​(N node)
        Inserts the node before all other nodes.
      • addLast

        public NodeList<N> addLast​(N node)
        Inserts the node after all other nodes. (This is simply an alias for add.)
      • addAfter

        public NodeList<N> addAfter​(N node,
                                    N afterThisNode)
        Inserts the node after afterThisNode.
        Throws:
        java.lang.IllegalArgumentException - when afterThisNode is not in this list.
      • addBefore

        public NodeList<N> addBefore​(N node,
                                     N beforeThisNode)
        Inserts the node before beforeThisNode.
        Throws:
        java.lang.IllegalArgumentException - when beforeThisNode is not in this list.
      • getParentNode

        public java.util.Optional<Node> getParentNode()
        Description copied from interface: HasParentNode
        Returns the parent node, or Optional.empty if no parent is set.
        Specified by:
        getParentNode in interface HasParentNode<N extends Node>
      • setParentNode

        public NodeList<N> setParentNode​(Node parentNode)
        Sets the parentNode
        Specified by:
        setParentNode in interface HasParentNode<N extends Node>
        Parameters:
        parentNode - the parentNode
        Returns:
        this, the NodeList
      • getParentNodeForChildren

        public Node getParentNodeForChildren()
        Description copied from interface: HasParentNode
        Returns the parent node from the perspective of the children of this node.

        That is, this method returns this for everything except NodeList. A NodeList returns its parent node instead. This is because a NodeList sets the parent of all its children to its own parent node (see NodeList for details).

        Specified by:
        getParentNodeForChildren in interface HasParentNode<N extends Node>
      • accept

        public <R,​A> R accept​(GenericVisitor<R,​A> v,
                                    A arg)
        Description copied from interface: Visitable
        Accept method for visitor support.
        Specified by:
        accept in interface Visitable
        Type Parameters:
        R - the type of the return value of the visitor
        A - the type the user argument passed to the visitor
        Parameters:
        v - the visitor implementation
        arg - the argument passed to the visitor (of type A)
        Returns:
        the result of the visit (of type R)
      • accept

        public <A> void accept​(VoidVisitor<A> v,
                               A arg)
        Description copied from interface: Visitable
        Accept method for visitor support.
        Specified by:
        accept in interface Visitable
        Type Parameters:
        A - the type the argument passed for the visitor
        Parameters:
        v - the visitor implementation
        arg - any value relevant for the visitor (of type A)
      • forEach

        public void forEach​(java.util.function.Consumer<? super N> action)
        Specified by:
        forEach in interface java.lang.Iterable<N extends Node>
        See Also:
        Iterable.forEach(java.util.function.Consumer)
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<N extends Node>
        Specified by:
        contains in interface java.util.List<N extends Node>
        See Also:
        List.contains(java.lang.Object)
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<N extends Node>
        Specified by:
        toArray in interface java.util.List<N extends Node>
        See Also:
        List.toArray()
      • toArray

        public <T> T[] toArray​(T[] a)
        Specified by:
        toArray in interface java.util.Collection<N extends Node>
        Specified by:
        toArray in interface java.util.List<N extends Node>
        See Also:
        List.toArray(java.lang.Object[])
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<N extends Node>
        Specified by:
        remove in interface java.util.List<N extends Node>
        See Also:
        List.remove(java.lang.Object)
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<N extends Node>
        Specified by:
        containsAll in interface java.util.List<N extends Node>
        See Also:
        List.containsAll(java.util.Collection)
      • addAll

        public boolean addAll​(java.util.Collection<? extends N> c)
        Specified by:
        addAll in interface java.util.Collection<N extends Node>
        Specified by:
        addAll in interface java.util.List<N extends Node>
        See Also:
        List.addAll(java.util.Collection)
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection<? extends N> c)
        Specified by:
        addAll in interface java.util.List<N extends Node>
        See Also:
        List.addAll(int, java.util.Collection)
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<N extends Node>
        Specified by:
        removeAll in interface java.util.List<N extends Node>
        See Also:
        List.removeAll(java.util.Collection)
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<N extends Node>
        Specified by:
        retainAll in interface java.util.List<N extends Node>
        See Also:
        List.retainAll(java.util.Collection)
      • replaceAll

        public void replaceAll​(java.util.function.UnaryOperator<N> operator)
        Specified by:
        replaceAll in interface java.util.List<N extends Node>
        See Also:
        List.replaceAll(java.util.function.UnaryOperator)
      • removeIf

        public boolean removeIf​(java.util.function.Predicate<? super N> filter)
        Specified by:
        removeIf in interface java.util.Collection<N extends Node>
        See Also:
        Collection.removeIf(java.util.function.Predicate)
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<N extends Node>
        Specified by:
        clear in interface java.util.List<N extends Node>
        See Also:
        List.clear()
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Collection<N extends Node>
        Specified by:
        equals in interface java.util.List<N extends Node>
        Overrides:
        equals in class java.lang.Object
        See Also:
        List.equals(java.lang.Object)
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<N extends Node>
        Specified by:
        hashCode in interface java.util.List<N extends Node>
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        List.hashCode()
      • indexOf

        public int indexOf​(java.lang.Object o)
        Specified by:
        indexOf in interface java.util.List<N extends Node>
        See Also:
        List.indexOf(java.lang.Object)
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object o)
        Specified by:
        lastIndexOf in interface java.util.List<N extends Node>
        See Also:
        List.lastIndexOf(java.lang.Object)
      • listIterator

        public java.util.ListIterator<N> listIterator()
        Specified by:
        listIterator in interface java.util.List<N extends Node>
        See Also:
        List.listIterator()
      • listIterator

        public java.util.ListIterator<N> listIterator​(int index)
        Specified by:
        listIterator in interface java.util.List<N extends Node>
        See Also:
        List.listIterator(int)
      • parallelStream

        public java.util.stream.Stream<N> parallelStream()
        Specified by:
        parallelStream in interface java.util.Collection<N extends Node>
        See Also:
        Collection.parallelStream()
      • subList

        public java.util.List<N> subList​(int fromIndex,
                                         int toIndex)
        Specified by:
        subList in interface java.util.List<N extends Node>
        See Also:
        List.subList(int, int)
      • spliterator

        public java.util.Spliterator<N> spliterator()
        Specified by:
        spliterator in interface java.util.Collection<N extends Node>
        Specified by:
        spliterator in interface java.lang.Iterable<N extends Node>
        Specified by:
        spliterator in interface java.util.List<N extends Node>
        See Also:
        List.spliterator()
      • notifyElementAdded

        private void notifyElementAdded​(int index,
                                        Node nodeAddedOrRemoved)
      • notifyElementRemoved

        private void notifyElementRemoved​(int index,
                                          Node nodeAddedOrRemoved)
      • notifyElementReplaced

        private void notifyElementReplaced​(int index,
                                           Node nodeAddedOrRemoved)
      • unregister

        public void unregister​(AstObserver observer)
        Description copied from interface: Observable
        Unregister an observer. If the given observer was not registered there are no effects.
        Specified by:
        unregister in interface Observable
      • isRegistered

        public boolean isRegistered​(AstObserver observer)
        Description copied from interface: Observable
        Was this observer registered? Note that equals is used to determine if the given observer was registered.
        Specified by:
        isRegistered in interface Observable
      • replace

        public boolean replace​(N old,
                               N replacement)
        Replaces the first node that is equal to "old" with "replacement".
        Returns:
        true if a replacement has happened.
      • isNonEmpty

        public boolean isNonEmpty()
        Returns:
        the opposite of isEmpty()
      • ifNonEmpty

        public void ifNonEmpty​(java.util.function.Consumer<? super NodeList<N>> consumer)
      • toNodeList

        public static <T extends Node> java.util.stream.Collector<T,​NodeList<T>,​NodeList<T>> toNodeList()
      • setAsParentNodeOf

        private void setAsParentNodeOf​(java.util.List<? extends Node> childNodes)
      • setAsParentNodeOf

        private void setAsParentNodeOf​(Node childNode)
      • toString

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