Class NodeIterator<T extends Node>

  • All Implemented Interfaces:
    java.util.Iterator<T>

    public class NodeIterator<T extends Node>
    extends java.lang.Object
    implements java.util.Iterator<T>
    Iterate through a Node and its tree of descendants, in document order, and returns nodes of the specified type. This iterator supports structural changes to the tree during the traversal, such as Node.remove(), Node.replaceWith(Node), Node.wrap(String), etc.

    See also the NodeTraversor if head and tail callbacks are desired for each node.

    Since:
    1.17.1
    • Constructor Summary

      Constructors 
      Constructor Description
      NodeIterator​(Node start, java.lang.Class<T> type)
      Create a NoteIterator that will iterate the supplied node, and all of its descendants.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private T findNextNode()  
      static NodeIterator<Node> from​(Node start)
      Create a NoteIterator that will iterate the supplied node, and all of its descendants.
      boolean hasNext()  
      private void maybeFindNext()
      If next is not null, looks for and sets next.
      T next()  
      void remove()  
      void restart​(Node start)
      Restart this Iterator from the specified start node.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Field Detail

      • root

        private Node root
      • next

        private T extends Node next
      • current

        private Node current
      • previous

        private Node previous
      • currentParent

        private Node currentParent
      • type

        private final java.lang.Class<T extends Node> type
    • Constructor Detail

      • NodeIterator

        public NodeIterator​(Node start,
                            java.lang.Class<T> type)
        Create a NoteIterator that will iterate the supplied node, and all of its descendants. The returned next type will be filtered to the input type.
        Parameters:
        start - initial node
        type - node type to filter for
    • Method Detail

      • from

        public static NodeIterator<Node> from​(Node start)
        Create a NoteIterator that will iterate the supplied node, and all of its descendants. All node types will be returned.
        Parameters:
        start - initial node
      • restart

        public void restart​(Node start)
        Restart this Iterator from the specified start node. Will act as if it were newly constructed. Useful for e.g. to save some GC if the iterator is used in a tight loop.
        Parameters:
        start - the new start node.
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<T extends Node>
      • next

        public T next()
        Specified by:
        next in interface java.util.Iterator<T extends Node>
      • maybeFindNext

        private void maybeFindNext()
        If next is not null, looks for and sets next. If next is null after this, we have reached the end.
      • findNextNode

        private T findNextNode()
      • remove

        public void remove()
        Specified by:
        remove in interface java.util.Iterator<T extends Node>