From 2cf88ac6a37fc6f31def84fe8181bf3b4a6cc009 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 4 Jun 2014 21:49:23 +0200 Subject: [PATCH] Polishing --- .../util/CompositeIterator.java | 46 ++++++------ .../util/CompositeIteratorTests.java | 73 +++++++++++++------ 2 files changed, 76 insertions(+), 43 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/CompositeIterator.java b/spring-core/src/main/java/org/springframework/util/CompositeIterator.java index 2ce1e5306f..dea6f292a0 100644 --- a/spring-core/src/main/java/org/springframework/util/CompositeIterator.java +++ b/spring-core/src/main/java/org/springframework/util/CompositeIterator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,46 +13,48 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.util; import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import java.util.LinkedHashSet; import java.util.NoSuchElementException; +import java.util.Set; /** - * Iterator that combines multiple other iterators. - * This implementation maintains a list of iterators which are invoked in sequence until all iterators are exhausted. + * Composite iterator that combines multiple other iterators, + * as registered via {@link #add(Iterator)}. + * + *

This implementation maintains a linked set of iterators + * which are invoked in sequence until all iterators are exhausted. + * * @author Erwin Vervaet + * @author Juergen Hoeller + * @since 3.0 */ public class CompositeIterator implements Iterator { - private List> iterators = new LinkedList>(); + private final Set> iterators = new LinkedHashSet>(); private boolean inUse = false; - /** - * Create a new composite iterator. Add iterators using the {@link #add(Iterator)} method. - */ - public CompositeIterator() { - } /** * Add given iterator to this composite. */ public void add(Iterator iterator) { - Assert.state(!inUse, "You can no longer add iterator to a composite iterator that's already in use"); - if (iterators.contains(iterator)) { + Assert.state(!this.inUse, "You can no longer add iterators to a composite iterator that's already in use"); + if (this.iterators.contains(iterator)) { throw new IllegalArgumentException("You cannot add the same iterator twice"); } - iterators.add(iterator); + this.iterators.add(iterator); } @Override public boolean hasNext() { - inUse = true; - for (Iterator> it = iterators.iterator(); it.hasNext();) { - if (it.next().hasNext()) { + this.inUse = true; + for (Iterator iterator : this.iterators) { + if (iterator.hasNext()) { return true; } } @@ -61,18 +63,18 @@ public class CompositeIterator implements Iterator { @Override public E next() { - inUse = true; - for (Iterator> it = iterators.iterator(); it.hasNext();) { - Iterator iterator = it.next(); + this.inUse = true; + for (Iterator iterator : this.iterators) { if (iterator.hasNext()) { return iterator.next(); } } - throw new NoSuchElementException("Exhausted all iterators"); + throw new NoSuchElementException("All iterators exhausted"); } @Override public void remove() { - throw new UnsupportedOperationException("Remove is not supported"); + throw new UnsupportedOperationException("CompositeIterator does not support remove()"); } + } diff --git a/spring-core/src/test/java/org/springframework/util/CompositeIteratorTests.java b/spring-core/src/test/java/org/springframework/util/CompositeIteratorTests.java index d27cb93109..124bc04a3f 100644 --- a/spring-core/src/test/java/org/springframework/util/CompositeIteratorTests.java +++ b/spring-core/src/test/java/org/springframework/util/CompositeIteratorTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2002-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.util; import java.util.Arrays; @@ -5,29 +21,36 @@ import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; -import junit.framework.TestCase; +import org.junit.Test; + +import static org.junit.Assert.*; + /** * Test case for {@link CompositeIterator}. * * @author Erwin Vervaet + * @author Juergen Hoeller */ -public class CompositeIteratorTests extends TestCase { +public class CompositeIteratorTests { + @Test public void testNoIterators() { - CompositeIterator it = new CompositeIterator(); + CompositeIterator it = new CompositeIterator(); assertFalse(it.hasNext()); try { it.next(); fail(); - } catch (NoSuchElementException e) { + } + catch (NoSuchElementException ex) { // expected } } + @Test public void testSingleIterator() { - CompositeIterator it = new CompositeIterator(); - it.add(Arrays.asList(new String[] { "0", "1" }).iterator()); + CompositeIterator it = new CompositeIterator(); + it.add(Arrays.asList("0", "1").iterator()); for (int i = 0; i < 2; i++) { assertTrue(it.hasNext()); assertEquals(String.valueOf(i), it.next()); @@ -36,16 +59,18 @@ public class CompositeIteratorTests extends TestCase { try { it.next(); fail(); - } catch (NoSuchElementException e) { + } + catch (NoSuchElementException ex) { // expected } } + @Test public void testMultipleIterators() { - CompositeIterator it = new CompositeIterator(); - it.add(Arrays.asList(new String[] { "0", "1" }).iterator()); - it.add(Arrays.asList(new String[] { "2" }).iterator()); - it.add(Arrays.asList(new String[] { "3", "4" }).iterator()); + CompositeIterator it = new CompositeIterator(); + it.add(Arrays.asList("0", "1").iterator()); + it.add(Arrays.asList("2").iterator()); + it.add(Arrays.asList("3", "4").iterator()); for (int i = 0; i < 5; i++) { assertTrue(it.hasNext()); assertEquals(String.valueOf(i), it.next()); @@ -54,43 +79,49 @@ public class CompositeIteratorTests extends TestCase { try { it.next(); fail(); - } catch (NoSuchElementException e) { + } + catch (NoSuchElementException ex) { // expected } } + @Test public void testInUse() { - List list = Arrays.asList(new String[] { "0", "1" }); - CompositeIterator it = new CompositeIterator(); + List list = Arrays.asList("0", "1"); + CompositeIterator it = new CompositeIterator(); it.add(list.iterator()); it.hasNext(); try { it.add(list.iterator()); fail(); - } catch (IllegalStateException e) { + } + catch (IllegalStateException ex) { // expected } - it = new CompositeIterator(); + it = new CompositeIterator(); it.add(list.iterator()); it.next(); try { it.add(list.iterator()); fail(); - } catch (IllegalStateException e) { + } + catch (IllegalStateException ex) { // expected } } + @Test public void testDuplicateIterators() { - List list = Arrays.asList(new String[] { "0", "1" }); - Iterator iterator = list.iterator(); - CompositeIterator it = new CompositeIterator(); + List list = Arrays.asList("0", "1"); + Iterator iterator = list.iterator(); + CompositeIterator it = new CompositeIterator(); it.add(iterator); it.add(list.iterator()); try { it.add(iterator); fail(); - } catch (IllegalArgumentException e) { + } + catch (IllegalArgumentException ex) { // expected } }