Class NodeVisitor

  • Direct Known Subclasses:
    NodeGenerator

    public class NodeVisitor
    extends java.lang.Object
    A non-recursive depth-first traversal for arbitrary trees.

    Traverses trees in depth-first order, visiting nodes one at a time via step(). The traversal distinguishes between the root node, map property keys and values, and collection elements using NodeVisitor.Context.

    Traversal rules:

    • When visiting a map-like node, keys are visited before their corresponding values.
    • When visiting a collection or array node, elements are visited in insertion order.
    • Other node types are visited directly.

    Traversal proceeds until all nodes have been visited or limits (maximum depth or maximum nodes) are reached.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  NodeVisitor.Context
      Indicates the role of the node during traversal.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected NodeAdapter adapter
      Adapter providing access to node types and children.
      protected long depth
      Current depth.
      protected java.util.Comparator<java.util.Map.Entry<?,​?>> entryComparator
      Comparator for map entries.
      protected int maxDepth
      Maximum depth allowed during traversal.
      protected int maxVisited
      Maximum number of nodes allowed to be visited.
      protected java.lang.Object node
      Current node.
      protected NodeVisitor.Context nodeContext
      Role of the current node.
      protected NodeType nodeType
      Type of the current node.
      protected java.util.Deque<java.lang.Object> stack
      Stack used for traversal.
      static int UNLIMITED_DEPTH
      Sentinel value indicating no maximum depth limit.
      static int UNLIMITED_NODES
      Sentinel value indicating no maximum node visit limit.
      protected long visited
      Number of nodes visited.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected NodeVisitor​(java.util.Deque<java.lang.Object> stack, NodeAdapter adapter)
      Creates a new traversal with the given stack and adapter.
      protected NodeVisitor​(java.util.Deque<java.lang.Object> stack, NodeAdapter adapter, java.util.Comparator<java.util.Map.Entry<?,​?>> entryComparator)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void keyComparator​(java.util.Comparator<java.util.Map.Entry<?,​?>> keyComparator)
      Sets the comparator used to order map property keys during traversal.
      int maxDepth()
      Returns the maximum depth allowed during traversal.
      void maxDepth​(int maxDepth)
      Sets the maximum depth of traversal.
      int maxVisited()
      Returns the maximum number of nodes that can be visited during traversal.
      void maxVisited​(int maxVisitedNodes)
      Sets the maximum number of nodes that can be visited during traversal.
      static NodeVisitor of​(java.lang.Object root, NodeAdapter adapter)
      Creates a new NodeVisitor starting at the given root node, using the default property key ordering - insertion order.
      static NodeVisitor of​(java.lang.Object root, NodeAdapter adapter, java.util.Comparator<java.util.Map.Entry<?,​?>> propertyComparator)
      Creates a new NodeVisitor starting at the given root node, using a custom comparator for ordering map property keys.
      void reset​(java.lang.Object node, NodeAdapter adapter)
      Resets the traversal with a new root node and adapter.
      boolean step()
      Advances the traversal by one step.
      long visited()
      Returns the number of nodes visited.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UNLIMITED_DEPTH

        public static final int UNLIMITED_DEPTH
        Sentinel value indicating no maximum depth limit.
        See Also:
        Constant Field Values
      • UNLIMITED_NODES

        public static final int UNLIMITED_NODES
        Sentinel value indicating no maximum node visit limit.
        See Also:
        Constant Field Values
      • maxVisited

        protected int maxVisited
        Maximum number of nodes allowed to be visited.
      • maxDepth

        protected int maxDepth
        Maximum depth allowed during traversal.
      • stack

        protected final java.util.Deque<java.lang.Object> stack
        Stack used for traversal.
      • entryComparator

        protected java.util.Comparator<java.util.Map.Entry<?,​?>> entryComparator
        Comparator for map entries.
      • adapter

        protected NodeAdapter adapter
        Adapter providing access to node types and children.
      • depth

        protected long depth
        Current depth.
      • visited

        protected long visited
        Number of nodes visited.
      • node

        protected java.lang.Object node
        Current node.
      • nodeType

        protected NodeType nodeType
        Type of the current node.
    • Constructor Detail

      • NodeVisitor

        protected NodeVisitor​(java.util.Deque<java.lang.Object> stack,
                              NodeAdapter adapter)
        Creates a new traversal with the given stack and adapter.
        Parameters:
        stack - the stack to use for traversal state
        adapter - the adapter providing node access
      • NodeVisitor

        protected NodeVisitor​(java.util.Deque<java.lang.Object> stack,
                              NodeAdapter adapter,
                              java.util.Comparator<java.util.Map.Entry<?,​?>> entryComparator)
    • Method Detail

      • of

        public static NodeVisitor of​(java.lang.Object root,
                                     NodeAdapter adapter)
        Creates a new NodeVisitor starting at the given root node, using the default property key ordering - insertion order.
        Parameters:
        root - the root node to start traversal from, must not be null
        adapter - the adapter providing access to node types and children, must not be null
        Returns:
        a new NodeVisitor instance positioned at the root node
        Throws:
        java.lang.NullPointerException - if root or adapter is null
      • of

        public static NodeVisitor of​(java.lang.Object root,
                                     NodeAdapter adapter,
                                     java.util.Comparator<java.util.Map.Entry<?,​?>> propertyComparator)
        Creates a new NodeVisitor starting at the given root node, using a custom comparator for ordering map property keys.
        Parameters:
        root - the root node to start traversal from, must not be null
        adapter - the adapter providing access to node types and children, must not be null
        propertyComparator - comparator used to order map keys during traversal, must not be null
        Returns:
        a new NodeVisitor instance positioned at the root node
        Throws:
        java.lang.NullPointerException - if root, adapter, or propertyComparator is null
      • step

        public boolean step()
        Advances the traversal by one step.

        Processes exactly one node, updating node, nodeType, and nodeContext to describe it. Subsequent calls continue traversal until the entire tree has been visited.

        Returns:
        true if a node was processed, or false if traversal is complete
        Throws:
        java.lang.IllegalStateException - if the traversal exceeds the maximum node count or the maximum depth configured via maxVisited(int) or maxDepth(int)
      • visited

        public long visited()
        Returns the number of nodes visited.
      • reset

        public void reset​(java.lang.Object node,
                          NodeAdapter adapter)
        Resets the traversal with a new root node and adapter.
        Parameters:
        node - the new root node
        adapter - the adapter providing access to node types
      • keyComparator

        public void keyComparator​(java.util.Comparator<java.util.Map.Entry<?,​?>> keyComparator)
        Sets the comparator used to order map property keys during traversal.

        The comparator determines the order in which map keys are visited. By default, keys are visited in insertion order if no comparator is set.

        Parameters:
        keyComparator - comparator for map keys; must not be null
      • maxDepth

        public void maxDepth​(int maxDepth)
        Sets the maximum depth of traversal.

        If the traversal reaches this depth, further children will not be visited. Use UNLIMITED_DEPTH (-1) to indicate no depth limit (default).

        Parameters:
        maxDepth - maximum depth allowed during traversal
      • maxDepth

        public int maxDepth()
        Returns the maximum depth allowed during traversal.
        Returns:
        maximum depth; -1 if no limit
      • maxVisited

        public void maxVisited​(int maxVisitedNodes)
        Sets the maximum number of nodes that can be visited during traversal.

        If the number of visited nodes reaches this limit, step() will throw IllegalStateException. Use UNLIMITED_NODES (-1) to indicate no limit (default).

        Parameters:
        maxVisitedNodes - maximum number of nodes to visit
      • maxVisited

        public int maxVisited()
        Returns the maximum number of nodes that can be visited during traversal.
        Returns:
        maximum number of nodes; -1 if no limit