Polishing

This commit is contained in:
Juergen Hoeller
2014-06-04 21:49:23 +02:00
parent f7b465390c
commit 2cf88ac6a3
2 changed files with 76 additions and 43 deletions

View File

@@ -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)}.
*
* <p>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<E> implements Iterator<E> {
private List<Iterator<E>> iterators = new LinkedList<Iterator<E>>();
private final Set<Iterator<E>> iterators = new LinkedHashSet<Iterator<E>>();
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<E> 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<Iterator<E>> it = iterators.iterator(); it.hasNext();) {
if (it.next().hasNext()) {
this.inUse = true;
for (Iterator<E> iterator : this.iterators) {
if (iterator.hasNext()) {
return true;
}
}
@@ -61,18 +63,18 @@ public class CompositeIterator<E> implements Iterator<E> {
@Override
public E next() {
inUse = true;
for (Iterator<Iterator<E>> it = iterators.iterator(); it.hasNext();) {
Iterator<E> iterator = it.next();
this.inUse = true;
for (Iterator<E> 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()");
}
}