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

org.codefilarete.tool.collection.ArrayIterator Maven / Gradle / Ivy

package org.codefilarete.tool.collection;

import java.util.NoSuchElementException;

/**
 * Wraps an array into an Iterator. Allows reuse of an array in the Iterators API.
 * 
 * @author Guillaume Mary
 */
public class ArrayIterator extends ReadOnlyIterator {

	private final O[] array;
	private int currentIndex = 0;
	private final int maxIndex;
	
	@SafeVarargs // method body doesn't handle improperly varargs parameter so it would generate ClassCastException 
	public ArrayIterator(O ... array) {
		this.array = array;
		this.maxIndex = array.length;
	}

	@Override
	public boolean hasNext() {
		return currentIndex < maxIndex;
	}

	@Override
	public O next() {
		try {
			return this.array[currentIndex++];
		} catch (ArrayIndexOutOfBoundsException e) {
			throw new NoSuchElementException();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy