All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.daisy.dotify.common.collection.CompoundIterator Maven / Gradle / Ivy

The newest version!
package org.daisy.dotify.common.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Provides an iterator for a collection of iterables
 * @author Joel Håkansson
 *
 * @param  the type of iterator
 */
public class CompoundIterator implements Iterator {
	ArrayList> iterators;
	
	/**
	 * Creates a new compound iterator
	 * @param iterables the iterables to use in this iterator
	 */
	public CompoundIterator(Iterable> iterables) {
		iterators = new ArrayList<>();
		for (Iterable e : iterables) {
			iterators.add(e.iterator());
		}
	}

	@Override
	public boolean hasNext() {
		for (Iterator e : iterators) {
			if (e.hasNext()) {
				return true;
			}
		}
		return false;
	}

	@Override
	public T next() {
		for (Iterator e : iterators) {
			if (e.hasNext()) {
				return e.next();
			}
		}
		throw new NoSuchElementException();
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
		
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy