garydgregory commented on code in PR #755:
URL: 
https://github.com/apache/commons-collections/pull/755#discussion_r4542633505


##########
src/main/java/org/apache/commons/collections4/iterators/IteratorChain.java:
##########
@@ -288,26 +330,34 @@ public int size() {
     }
 
     /**
-     * Updates the current iterator field to ensure that the current Iterator 
is
-     * not exhausted
+     * Updates the current iterator field to ensure that the current Iterator 
is not exhausted.
+     * Nested instances of this class are traversed iteratively; subclasses 
are invoked normally to preserve their overrides.
      */
     protected void updateCurrentIterator() {
-        if (currentIterator == null) {
-            if (iteratorQueue.isEmpty()) {
-                currentIterator = EmptyIterator.<E>emptyIterator();
+        final Deque<IteratorChain<? extends E>> parents = new ArrayDeque<>();
+        IteratorChain<? extends E> chain = this;
+        while (true) {
+            chain.initializeCurrentIterator();
+            final IteratorChain<? extends E> child = 
asIteratorChain(chain.currentIterator);
+            if (child == null) {
+                chain.cachedHasNextValue = chain.currentIterator.hasNext();
             } else {
-                currentIterator = iteratorQueue.remove();
+                child.lockChain();
+                if (child.cachedHasNextValue == null) {
+                    parents.push(chain);
+                    chain = child;

Review Comment:
   Addressed. Traversal now tracks active ancestors by identity and throws 
IllegalStateException when it detects a cycle, while allowing shared acyclic 
children. The check also covers cached availability, and intermediate cached 
results are cleared on failure so retries cannot hide the cycle. Removal has 
its own cycle guard because a failed next() can leave cyclic removal state. 
Added tests for direct, mutual, indirect, and wrapped cycles, retries, shared 
children, and removal after failure.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to