Package com.apicatalog.tree.io
Class TreeTraversal
java.lang.Object
com.apicatalog.tree.io.TreeTraversal
- Direct Known Subclasses:
NativeMaterializer
Provides a stateful, non-recursive, depth-first iterator for arbitrary
tree-like structures. This class decouples the traversal algorithm from the
concrete representation of the tree by operating on the
TreeAdapter
abstraction.
It can be used in two primary ways:
- Manual Iteration: By repeatedly calling the
next()method in a loop, you can process each node individually, allowing for complex logic like searching, validation, or conditional processing. - Automated Transformation: The
traverse(TreeGenerator)method provides a high-level utility to walk the entire tree and drive aTreeGenerator, effectively translating or transforming one tree representation into another.
Traversal Rules:
- When visiting a map, keys are visited before their corresponding values.
The order of keys can be controlled with a custom
Comparator. - When visiting a collection, elements are visited in their natural iteration order.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumIdentifies the role of the current node within the tree structure during traversal. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Deque<TreeAdapter>protected Objectprotected TreeTraversal.Contextprotected NodeTypeprotected intprotected Comparator<Map.Entry<?,?>> protected intprotected intstatic final intA sentinel value indicating that traversal depth is not limited.static final intA sentinel value indicating that the number of visited nodes is not limited.protected int -
Constructor Summary
ConstructorsConstructorDescriptionTreeTraversal(Deque<Object> stack) TreeTraversal(Deque<Object> stack, Comparator<Map.Entry<?, ?>> entryComparator) -
Method Summary
Modifier and TypeMethodDescriptionadapter()Gets the adapter to process the#currentNode().intmaxDepth()Gets the configured maximum traversal depth.voidmaxDepth(int maxDepth) Sets the maximum traversal depth.intGets the configured maximum number of nodes to visit.voidmaxVisited(int maxVisitedNodes) Sets the maximum number of nodes to visit.booleannext()Advances the traversal to the next node in the depth-first sequence.protected booleannext(TreeTraversal.Context stepContext) node()Gets the current node being processed.Gets the context of the current node.nodeType()Gets the type of the current node.reset()Resets the visitor's internal state, clearing the traversal stack and counters.root(Object node, TreeAdapter adapter) Sets the root node for the traversal, initializing the visitor's stack.voidtraverse(TreeGenerator generator) A high-level utility method that fully traverses the tree and drives the providedTreeGenerator.voidtraverse(Consumer<TreeTraversal> consumer) longvisited()Gets the total number of nodes visited so far.
-
Field Details
-
UNLIMITED_DEPTH
public static final int UNLIMITED_DEPTHA sentinel value indicating that traversal depth is not limited.- See Also:
-
UNLIMITED_NODES
public static final int UNLIMITED_NODESA sentinel value indicating that the number of visited nodes is not limited.- See Also:
-
adapters
-
stack
-
entryComparator
-
maxVisited
protected int maxVisited -
maxDepth
protected int maxDepth -
depth
protected int depth -
visited
protected int visited -
currentNode
-
currentNodeType
-
currentNodeContext
-
-
Constructor Details
-
TreeTraversal
public TreeTraversal() -
TreeTraversal
-
TreeTraversal
-
-
Method Details
-
traverse
-
traverse
A high-level utility method that fully traverses the tree and drives the providedTreeGenerator. This is the primary method for tree transformation, serialization, or deep cloning. It iterates through every node usingnext()and emits a corresponding event to the generator.- Parameters:
generator- the generator that will receive construction events.- Throws:
TreeIOException- if the generator encounters an I/O error.IllegalStateException- if the source tree is malformed (e.g., unclosed structures).
-
next
public boolean next()Advances the traversal to the next node in the depth-first sequence.Each call to this method processes exactly one node or structural marker. After a successful call, the visitor's state is updated, and the details of the current item can be accessed via
node(),nodeType(), andnodeContext().- Returns:
trueif the traversal advanced to a new item, orfalseif the traversal is complete.- Throws:
IllegalStateException- if the traversal exceeds configured limits (e.g., maximum depth or node count).
-
next
-
reset
Resets the visitor's internal state, clearing the traversal stack and counters. The visitor can be reused after calling this method, but a new root node must be set usingroot(Object, TreeAdapter).- Returns:
- this instance, for chaining.
-
root
Sets the root node for the traversal, initializing the visitor's stack.- Parameters:
node- the new root node.adapter- the adapter for interpreting the new tree structure.- Returns:
- this instance, for chaining.
-
visited
public long visited()Gets the total number of nodes visited so far. -
maxDepth
public void maxDepth(int maxDepth) Sets the maximum traversal depth. If the traversal reaches this depth, it will not process the children of nodes at that depth.- Parameters:
maxDepth- the maximum depth, orUNLIMITED_DEPTHfor no limit.
-
maxDepth
public int maxDepth()Gets the configured maximum traversal depth.- Returns:
- the maximum depth, or
UNLIMITED_DEPTHif no limit is set.
-
maxVisited
public void maxVisited(int maxVisitedNodes) Sets the maximum number of nodes to visit. The traversal will throw anIllegalStateExceptionif this limit is exceeded.- Parameters:
maxVisitedNodes- the maximum number of nodes, orUNLIMITED_NODESfor no limit.
-
maxVisited
public int maxVisited()Gets the configured maximum number of nodes to visit.- Returns:
- the maximum node count, or
UNLIMITED_NODESif no limit is set.
-
adapter
Gets the adapter to process the#currentNode(). -
node
Gets the current node being processed. -
nodeType
Gets the type of the current node. -
nodeContext
Gets the context of the current node.
-