ppkarwasz commented on code in PR #628:
URL:
https://github.com/apache/commons-collections/pull/628#discussion_r2262955560
##########
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:
Maybe we could put this logic into
`UnmodifiableIterator.unmodifiableIterator` instead?
Pushing the unmodifiable wrapper up the tree, might help also other
iterator-collapsing logic. What do you think?
https://github.com/apache/commons-collections/blob/79ca330e5bdffe080d2bd6f4d18c43e3d1cc53f6/src/main/java/org/apache/commons/collections4/iterators/UnmodifiableIterator.java#L47-L55
--
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]