Class NodeVisitor
- Direct Known Subclasses:
NodeGenerator
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 ClassesModifier and TypeClassDescriptionstatic enumIndicates the role of the node during traversal. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected NodeAdapterAdapter providing access to node types and children.protected longCurrent depth.protected Comparator<Map.Entry<?,?>> Comparator for map entries.protected intMaximum depth allowed during traversal.protected intMaximum number of nodes allowed to be visited.protected ObjectCurrent node.protected NodeVisitor.ContextRole of the current node.protected NodeTypeType of the current node.Stack used for traversal.static final intSentinel value indicating no maximum depth limit.static final intSentinel value indicating no maximum node visit limit.protected longNumber of nodes visited. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedNodeVisitor(Deque<Object> stack, NodeAdapter adapter) Creates a new traversal with the given stack and adapter.protectedNodeVisitor(Deque<Object> stack, NodeAdapter adapter, Comparator<Map.Entry<?, ?>> entryComparator) -
Method Summary
Modifier and TypeMethodDescriptionvoidkeyComparator(Comparator<Map.Entry<?, ?>> keyComparator) Sets the comparator used to order map property keys during traversal.intmaxDepth()Returns the maximum depth allowed during traversal.voidmaxDepth(int maxDepth) Sets the maximum depth of traversal.intReturns the maximum number of nodes that can be visited during traversal.voidmaxVisited(int maxVisitedNodes) Sets the maximum number of nodes that can be visited during traversal.static NodeVisitorof(Object root, NodeAdapter adapter) Creates a newNodeVisitorstarting at the given root node, using the default property key ordering - insertion order.static NodeVisitorof(Object root, NodeAdapter adapter, Comparator<Map.Entry<?, ?>> propertyComparator) Creates a newNodeVisitorstarting at the given root node, using a custom comparator for ordering map property keys.voidreset(Object node, NodeAdapter adapter) Resets the traversal with a new root node and adapter.booleanstep()Advances the traversal by one step.longvisited()Returns the number of nodes visited.
-
Field Details
-
UNLIMITED_DEPTH
public static final int UNLIMITED_DEPTHSentinel value indicating no maximum depth limit.- See Also:
-
UNLIMITED_NODES
public static final int UNLIMITED_NODESSentinel value indicating no maximum node visit limit.- See Also:
-
maxVisited
protected int maxVisitedMaximum number of nodes allowed to be visited. -
maxDepth
protected int maxDepthMaximum depth allowed during traversal. -
stack
Stack used for traversal. -
entryComparator
Comparator for map entries. -
adapter
Adapter providing access to node types and children. -
depth
protected long depthCurrent depth. -
visited
protected long visitedNumber of nodes visited. -
node
Current node. -
nodeType
Type of the current node. -
nodeContext
Role of the current node.
-
-
Constructor Details
-
NodeVisitor
Creates a new traversal with the given stack and adapter.- Parameters:
stack- the stack to use for traversal stateadapter- the adapter providing node access
-
NodeVisitor
protected NodeVisitor(Deque<Object> stack, NodeAdapter adapter, Comparator<Map.Entry<?, ?>> entryComparator)
-
-
Method Details
-
of
Creates a newNodeVisitorstarting at the given root node, using the default property key ordering - insertion order.- Parameters:
root- the root node to start traversal from, must not benulladapter- the adapter providing access to node types and children, must not benull- Returns:
- a new
NodeVisitorinstance positioned at the root node - Throws:
NullPointerException- ifrootoradapterisnull
-
of
public static NodeVisitor of(Object root, NodeAdapter adapter, Comparator<Map.Entry<?, ?>> propertyComparator) Creates a newNodeVisitorstarting 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 benulladapter- the adapter providing access to node types and children, must not benullpropertyComparator- comparator used to order map keys during traversal, must not benull- Returns:
- a new
NodeVisitorinstance positioned at the root node - Throws:
NullPointerException- ifroot,adapter, orpropertyComparatorisnull
-
step
public boolean step()Advances the traversal by one step.Processes exactly one node, updating
node,nodeType, andnodeContextto describe it. Subsequent calls continue traversal until the entire tree has been visited.- Returns:
trueif a node was processed, orfalseif traversal is complete- Throws:
IllegalStateException- if the traversal exceeds the maximum node count or the maximum depth configured viamaxVisited(int)ormaxDepth(int)
-
visited
public long visited()Returns the number of nodes visited. -
reset
Resets the traversal with a new root node and adapter.- Parameters:
node- the new root nodeadapter- the adapter providing access to node types
-
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 throwIllegalStateException. UseUNLIMITED_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
-