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

org.eclipse.persistence.jpa.jpql.utility.iterator.ArrayIterator Maven / Gradle / Ivy

There is a newer version: 5.0.0-B02
Show newest version
/*******************************************************************************
 * Copyright (c) 2006, 2013 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation
 *
 ******************************************************************************/
package org.eclipse.persistence.jpa.jpql.utility.iterator;

import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * An ArrayIterator provides a {@link Iterator} for an array of objects.
 *
 * @version 2.5
 * @since 2.4
 */
@SuppressWarnings("nls")
public class ArrayIterator implements Iterator {

	/**
	 * The object containing the items to iterate over.
	 */
	E[] array;

	/**
	 * The last index this {@link ArrayIterator} can iterate over.
	 */
	private int maxIndex;

	/**
	 * The position of the cursor.
	 */
	int nextIndex;

	/**
	 * Creates a new ArrayIterator for the specified array.
	 *
	 * @param array The object containing the items to iterate over
	 */
	public  ArrayIterator(T... array) {
		this(array, 0, array.length);
	}

	/**
	 * Creates a new ArrayIterator for the specified array, starting at the specified
	 * start index and continuing for the specified length.
	 *
	 * @param array The object containing the items to iterate over
	 * @param start The beginning of the iteration
	 * @param length The length of the iteration
	 * @exception IllegalArgumentException The start index is either a negative value or greater than
	 * the length of the array or the length to copy goes beyond the length of the array
	 */
	public  ArrayIterator(T[] array, int start, int length) {

		if ((start < 0) || (start > array.length)) {
			throw new IllegalArgumentException("The start index is either a negative value or greater than the length of the array: " + start);
		}

		if ((length < 0) || (length > array.length - start)) {
			throw new IllegalArgumentException("The length to copy goes beyond the length of the array: " + length);
		}

		this.array     = array;
		this.nextIndex = start;
		this.maxIndex  = start + length;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean hasNext() {
		return nextIndex < maxIndex;
	}

	/**
	 * {@inheritDoc}
	 */
	public E next() {

		if (hasNext()) {
			return array[nextIndex++];
		}

		throw new NoSuchElementException("No more elements can be retrieved.");
	}

	/**
	 * {@inheritDoc}
	 */
	public void remove() {
		throw new UnsupportedOperationException("An ArrayIterator is read-only.");
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(getClass().getSimpleName());
		sb.append("(");
		sb.append(Arrays.toString(array));
		sb.append(")");
		return sb.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy