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 decreasing index.
008 *
009 * @author jimmy
010 */
011public class ReverseArrayIntIterator implements TIntIterator {
012
013    private final int[] array;
014    private int index;
015    private final int from;
016
017    /**
018     * Iterate an array in reverse order from the last to first element of the
019     * array.
020     *
021     * @param array
022     */
023    public ReverseArrayIntIterator(int[] array) {
024        this(array, 0, array.length);
025    }
026
027    /**
028     * Iterate an array in reverse order starting in position to (exclusive) and
029     * ending in position from (inclusive).
030     *
031     * @param array iterate this array
032     * @param from stop iterating at this index
033     * @param to start after this index
034     */
035    public ReverseArrayIntIterator(int[] array, int from, int to) {
036        if (to < from) {
037            throw new IllegalArgumentException();
038        }
039        if (from < 0) {
040            throw new IllegalArgumentException();
041        }
042        if (to > array.length) {
043            throw new IllegalArgumentException();
044        }
045        this.array = Check.notNull(array);
046        this.index = to;
047        this.from = from;
048    }
049
050    /**
051     * {@inheritDoc}
052     */
053    @Override
054    public boolean hasNext() {
055        return index > from;
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public int next() {
063        return array[--index];
064    }
065
066    /**
067     * Not supported.
068     *
069     * @throws UnsupportedOperationException if invoked
070     */
071    @Override
072    public void remove() throws UnsupportedOperationException {
073        throw new UnsupportedOperationException();
074    }
075}