net.java.truecommons.shed.CompoundIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truecommons-shed Show documentation
Show all versions of truecommons-shed Show documentation
Hosts small but useful tools and utilities that didn't fit anywhere else.
/*
* Copyright (C) 2005-2015 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truecommons.shed;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Concatenates two iterators.
*
* @param the type of the iterated elements.
* @author Christian Schlichtherle
*/
@NotThreadSafe
public final class CompoundIterator implements Iterator {
private Iterator extends E> first, second;
public CompoundIterator(
final Iterator extends E> first,
final Iterator extends E> second) {
if (null == (this.first = first)) throw new NullPointerException();
if (null == (this.second = second)) throw new NullPointerException();
}
@Override
public boolean hasNext() {
return first.hasNext() || (first != second && (first = second).hasNext());
}
@Override
public E next() {
try {
return first.next();
} catch (NoSuchElementException ex) {
if (first == second) throw ex;
return (first = second).next();
}
}
@Override
public void remove() {
first.remove();
}
}