diff --git a/src/main/java/htsjdk/samtools/util/MergingIterator.java b/src/main/java/htsjdk/samtools/util/MergingIterator.java index 14da4b0345..7962da643b 100644 --- a/src/main/java/htsjdk/samtools/util/MergingIterator.java +++ b/src/main/java/htsjdk/samtools/util/MergingIterator.java @@ -132,9 +132,11 @@ public void remove() { */ @Override public void close() { - for (final ComparableIterator iterator : this.queue) { - iterator.close(); - this.queue.remove(iterator); + final Iterator iterator = this.queue.iterator(); + while (iterator.hasNext()) { + final ComparableIterator subIterator = iterator.next(); + subIterator.close(); + iterator.remove(); } } diff --git a/src/test/java/htsjdk/samtools/util/MergingIteratorTest.java b/src/test/java/htsjdk/samtools/util/MergingIteratorTest.java index e5964acf7d..853ea80c78 100644 --- a/src/test/java/htsjdk/samtools/util/MergingIteratorTest.java +++ b/src/test/java/htsjdk/samtools/util/MergingIteratorTest.java @@ -173,4 +173,40 @@ public void testOutOfOrderIterators() { Assert.assertEquals(mergingIterator.next().intValue(), 4); mergingIterator.next(); // fails, because the next element would be "2" } + + @Test() + public void testCloseMultipleIteratorsMidIteration() { + final Queue queueOne = new LinkedList(); + queueOne.add(1); + queueOne.add(4); + queueOne.add(7); + + final Queue queueTwo = new LinkedList(); + queueTwo.add(2); + queueTwo.add(5); + queueTwo.add(8); + + final Queue queueThree = new LinkedList(); + queueThree.add(3); + queueThree.add(6); + queueThree.add(9); + + final Collection> iterators = new ArrayList>(3); + Collections.addAll( + iterators, + new QueueBackedIterator<>(queueOne), + new QueueBackedIterator<>(queueTwo), + new QueueBackedIterator<>(queueThree)); + + final MergingIterator mergingIterator = new MergingIterator( + INTEGER_COMPARATOR, + iterators); + + Assert.assertEquals(mergingIterator.next().intValue(), 1); + Assert.assertEquals(mergingIterator.next().intValue(), 2); + Assert.assertEquals(mergingIterator.next().intValue(), 3); + + mergingIterator.close(); + Assert.assertFalse(mergingIterator.hasNext()); + } }