JoergBudi commented on code in PR #628:
URL: 
https://github.com/apache/commons-collections/pull/628#discussion_r2263540907


##########
src/main/java/org/apache/commons/collections4/iterators/IteratorChain.java:
##########
@@ -163,7 +163,29 @@ public IteratorChain(final Iterator<? extends E> first, 
final Iterator<? extends
      */
     public void addIterator(final Iterator<? extends E> iterator) {
         checkLocked();
-        iteratorQueue.add(Objects.requireNonNull(iterator, "iterator"));
+        Objects.requireNonNull(iterator, "iterator");
+        if (iterator instanceof UnmodifiableIterator) {
+            final Iterator<? extends E> underlyingIterator = 
((UnmodifiableIterator) iterator).unwrap();
+            if (underlyingIterator instanceof IteratorChain) {
+                // in case it is an IteratorChain, wrap every underlying 
iterators as unmodifiable
+                // multiple rechainings would otherwise lead to exponential 
growing number of function calls
+                // when the iteratorChain gets used.
+                for (Iterator<? extends E> nestedIterator : ((IteratorChain<? 
extends E>) underlyingIterator).iteratorQueue) {
+                    
iteratorQueue.add(UnmodifiableIterator.unmodifiableIterator(nestedIterator));
+                }
+            } else {
+                // we don't know anything about the underlying iterator, 
simply add it here
+                iteratorQueue.add(iterator);
+            }

Review Comment:
   @ppkarwasz : I had a lot of problems to wrap my head around your suggestion 
:-), so I put the change as I understood you on a throw-away branch just to 
discuss and compare: 
https://github.com/JoergBudi/commons-collections/pull/3/files  (showing the 
merge into this PR). 
   
   To make that work, one needs to copy construct the IteratorChain within 
unmodifiableIterator(). You then don't need to check anymore in addIterator() 
for UnmodifiableIterator with iteratorChain in it, as it will never get invoked 
by all tests, so I removed that. All tests work, also the unionset is still 
fast.
   
   However, as unmodifiableIterator() does not return a UnidentifiableIterator 
instance anymore in all cases, that makes it really hard to read the code when 
you try to understand which "instanceof" if branch hits when ..., so I am not 
so convinced, that it is a good idea to change it this way.
   Also the copy construction (might be solved more elegantly, sure) looks a 
bit strange and might be a problem in future in case someone wants to extend 
IteratorChain for some reason.
   
   So, first, is that what you meant ? Do you think I shall go with this 
solution ?  Then I would beautify it a little and add one or two special tests.



-- 
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