Class NodeVisitor
- java.lang.Object
-
- com.apicatalog.tree.io.NodeVisitor
-
- Direct Known Subclasses:
NodeGenerator
public class NodeVisitor extends java.lang.ObjectA 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 usingNodeVisitor.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 classNodeVisitor.ContextIndicates the role of the node during traversal.
-
Field Summary
Fields Modifier and Type Field Description protected NodeAdapteradapterAdapter providing access to node types and children.protected longdepthCurrent depth.protected java.util.Comparator<java.util.Map.Entry<?,?>>entryComparatorComparator for map entries.protected intmaxDepthMaximum depth allowed during traversal.protected intmaxVisitedMaximum number of nodes allowed to be visited.protected java.lang.ObjectnodeCurrent node.protected NodeVisitor.ContextnodeContextRole of the current node.protected NodeTypenodeTypeType of the current node.protected java.util.Deque<java.lang.Object>stackStack used for traversal.static intUNLIMITED_DEPTHSentinel value indicating no maximum depth limit.static intUNLIMITED_NODESSentinel value indicating no maximum node visit limit.protected longvisitedNumber of nodes visited.
-
Constructor Summary
Constructors Modifier Constructor Description protectedNodeVisitor(java.util.Deque<java.lang.Object> stack, NodeAdapter adapter)Creates a new traversal with the given stack and adapter.protectedNodeVisitor(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 voidkeyComparator(java.util.Comparator<java.util.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.intmaxVisited()Returns 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(java.lang.Object root, NodeAdapter adapter)Creates a newNodeVisitorstarting at the given root node, using the default property key ordering - insertion order.static NodeVisitorof(java.lang.Object root, NodeAdapter adapter, java.util.Comparator<java.util.Map.Entry<?,?>> propertyComparator)Creates a newNodeVisitorstarting at the given root node, using a custom comparator for ordering map property keys.voidreset(java.lang.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 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.
-
nodeContext
protected NodeVisitor.Context nodeContext
Role 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 stateadapter- 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 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:
java.lang.NullPointerException- ifrootoradapterisnull
-
of
public static NodeVisitor of(java.lang.Object root, NodeAdapter adapter, java.util.Comparator<java.util.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:
java.lang.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:
java.lang.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
public void reset(java.lang.Object node, NodeAdapter adapter)Resets the traversal with a new root node and adapter.- Parameters:
node- the new root nodeadapter- 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 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
-
-