001package org.clafer.collection;
002
003import gnu.trove.iterator.TIntIterator;
004import org.clafer.common.Check;
005
006/**
007 * In iterator for an array of integers in order of increasing index.
008 *
009 * @author jimmy
010 */
011public class ArrayIntIterator implements TIntIterator {
012
013    private final int[] array;
014    private int index;
015    private final int to;
016
017    /**
018     * Iterate an array in order from the first to last element of the array.
019     *
020     * @param array
021     */
022    public ArrayIntIterator(int[] array) {
023        this(array, 0, array.length);
024    }
025
026    /**
027     * Iterate an array in order starting in position from (inclusive) and
028     * ending in position to (exclusive).
029     *
030     * @param array iterate this array
031     * @param from start iterating from this index
032     * @param to stop before this index
033     */
034    public ArrayIntIterator(int[] array, int from, int to) {
035        if (to < from) {
036            throw new IllegalArgumentException();
037        }
038        if (from < 0) {
039            throw new IllegalArgumentException();
040        }
041        if (to > array.length) {
042            throw new IllegalArgumentException();
043        }
044        this.array = Check.notNull(array);
045        this.index = from;
046        this.to = to;
047    }
048
049    /**
050     * {@inheritDoc}
051     */
052    @Override
053    public boolean hasNext() {
054        return index < to;
055    }
056
057    /**
058     * {@inheritDoc}
059     */
060    @Override
061    public int next() {
062        return array[index++];
063    }
064
065    /**
066     * Not supported.
067     *
068     * @throws UnsupportedOperationException if invoked
069     */
070    @Override
071    public void remove() throws UnsupportedOperationException {
072        throw new UnsupportedOperationException();
073    }
074}