1515@ Internal
1616public class Traverser <T > {
1717
18- private final RecursionState <T > stack ;
18+ private final RecursionState <T > recursionState ;
1919 private final Function <? super T , ? extends List <T >> getChildren ;
2020 private final Map <Class <?>, Object > rootVars = new ConcurrentHashMap <>();
2121
2222 /**
2323 * Instantiates a Traverser object with a given method to extract
2424 * children nodes from the current root
2525 *
26- * @param getChildren a function to extract children
27- * @param stack a queue of pended {@link TraverserContext} nodes to visit
28- * <br>
29- * * LIFO structure makes the traversal depth-first
30- * * FIFO structure makes the traversal breadth-first
26+ * @param getChildren a function to extract children
27+ * @param recursionState a queue of pended {@link TraverserContext} nodes to visit
28+ * <br>
29+ * * LIFO structure makes the traversal depth-first
30+ * * FIFO structure makes the traversal breadth-first
3131 */
32- private Traverser (RecursionState <T > stack , Function <? super T , ? extends List <T >> getChildren ) {
33- this .stack = assertNotNull (stack );
32+ private Traverser (RecursionState <T > recursionState , Function <? super T , ? extends List <T >> getChildren ) {
33+ this .recursionState = assertNotNull (recursionState );
3434 this .getChildren = assertNotNull (getChildren );
3535 }
3636
@@ -100,7 +100,7 @@ public static <T> Traverser<T> breadthFirst(Function<? super T, ? extends List<T
100100 * Resets the Traverser to the original state, so it can be re-used
101101 */
102102 public void reset () {
103- stack .clear ();
103+ recursionState .clear ();
104104 rootVars .clear ();
105105 }
106106
@@ -135,20 +135,21 @@ public <U> Object traverse(Collection<? extends T> roots, TraverserVisitor<? sup
135135 assertNotNull (roots );
136136 assertNotNull (visitor );
137137
138- stack .addNewContexts (roots , stack .newContext (null , null , rootVars ));
138+ recursionState .addNewContexts (roots , recursionState .newContext (null , null , rootVars ));
139139
140140 TraverserContext currentContext = null ;
141- while (!stack .isEmpty ()) {
142- Object top = stack .pop ();
141+ traverseLoop :
142+ while (!recursionState .isEmpty ()) {
143+ Object top = recursionState .pop ();
143144
144145 if (top == RecursionState .Marker .END_LIST ) {
145146 // end-of-list marker, we are done recursing children,
146147 // mark the current node as fully visited
147- TraversalControl traversalControl = visitor .leave ((TraverserContext ) stack .pop ());
148+ TraversalControl traversalControl = visitor .leave ((TraverserContext ) recursionState .pop ());
148149 assertNotNull (traversalControl , "result of leave must not be null" );
149150 switch (traversalControl ) {
150151 case QUIT :
151- break ;
152+ break traverseLoop ;
152153 case ABORT :
153154 case CONTINUE :
154155 continue ;
@@ -165,11 +166,11 @@ public <U> Object traverse(Collection<? extends T> roots, TraverserVisitor<? sup
165166 assertNotNull (traversalControl , "result of enter must not be null" );
166167 switch (traversalControl ) {
167168 case QUIT :
168- break ;
169+ break traverseLoop ;
169170 case ABORT :
170171 continue ;
171172 case CONTINUE :
172- stack .pushAll (currentContext , getChildren );
173+ recursionState .pushAll (currentContext , getChildren );
173174 continue ;
174175 default :
175176 assertShouldNeverHappen ();
0 commit comments