001    /*
002     *  Copyright 2001-2010 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.primitives.iterator.impl;
017    
018    import java.util.NoSuchElementException;
019    
020    import org.joda.primitives.IntUtils;
021    import org.joda.primitives.iterator.IntIterator;
022    
023    /**
024     * An iterator over an array of <code>int</code> values.
025     * <p>
026     * This class implements {@link java.util.Iterator Iterator} allowing
027     * seamless integration with other APIs.
028     * <p>
029     * The iterator can be reset to the start if required.
030     * It is unmodifiable and <code>remove()</code> is unsupported.
031     *
032     * @author Stephen Colebourne
033     * @author Jason Tiscione
034     * @version CODE GENERATED
035     * @since 1.0
036     */
037    public class ArrayIntIterator implements IntIterator {
038        // This file is CODE GENERATED. Do not change manually.
039    
040        /** The array to iterate over */
041        protected final int[] array;
042        /** Cursor position */
043        protected int cursor = 0;
044    
045        /**
046         * Creates an iterator over a copy of an array of <code>int</code> values.
047         * <p>
048         * The specified array is copied, making this class effectively immutable.
049         * Note that the class is not {@code final} thus it is not truly immutable.
050         * 
051         * @param array  the array to iterate over, must not be null
052         * @throws IllegalArgumentException if the array is null
053         */
054        public static ArrayIntIterator copyOf(int[] array) {
055            if (array == null) {
056                throw new IllegalArgumentException("Array must not be null");
057            }
058            return new ArrayIntIterator(array.clone());
059        }
060    
061        /**
062         * Constructs an iterator over an array of <code>int</code> values.
063         * <p>
064         * The array is assigned internally, thus the caller holds a reference to
065         * the internal state of the returned iterator. It is not recommended to
066         * modify the state of the array after construction.
067         * 
068         * @param array  the array to iterate over, must not be null
069         * @throws IllegalArgumentException if the array is null
070         */
071        public ArrayIntIterator(int[] array) {
072            super();
073            if (array == null) {
074                throw new IllegalArgumentException("Array must not be null");
075            }
076            this.array = array;
077        }
078    
079        //-----------------------------------------------------------------------
080        public boolean isModifiable() {
081            return false;
082        }
083    
084        public boolean isResettable() {
085            return true;
086        }
087    
088        //-----------------------------------------------------------------------
089        public boolean hasNext() {
090            return (cursor < array.length);
091        }
092    
093        public int nextInt() {
094            if (hasNext() == false) {
095                throw new NoSuchElementException("No more elements available");
096            }
097            return array[cursor++];
098        }
099    
100        public Integer next() {
101            return IntUtils.toObject(nextInt());
102        }
103    
104        public void remove() {
105            throw new UnsupportedOperationException("ArrayIntIterator does not support remove");
106        }
107    
108        public void reset() {
109            cursor = 0;
110        }
111    
112    }