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.listiterator.impl;
017    
018    import java.util.NoSuchElementException;
019    
020    import org.joda.primitives.IntUtils;
021    import org.joda.primitives.listiterator.IntListIterator;
022    
023    /**
024     * An iterator over an array of <code>int</code> values.
025     * <p>
026     * This class implements {@link java.util.ListIterator ListIterator} allowing
027     * seamless integration with other APIs.
028     * <p>
029     * The iterator can be reset to the start if required.
030     * <code>add()</code> and <code>remove()</code> are unsupported, but
031     * <code>set()</code> is supported.
032     *
033     * @author Stephen Colebourne
034     * @author Jason Tiscione
035     * @version CODE GENERATED
036     * @since 1.0
037     */
038    public class ArrayIntListIterator implements IntListIterator {
039        // This file is CODE GENERATED. Do not change manually.
040    
041        /** The array to iterate over */
042        protected final int[] array;
043        /** Cursor position */
044        protected int cursor = 0;
045        /** Last returned position */
046        protected int last = -1;
047    
048        /**
049         * Creates an iterator over a copy of an array of <code>int</code> values.
050         * <p>
051         * The specified array is copied, ensuring the original data is unaltered.
052         * Note that the class is not immutable due to the {@code set} methods.
053         * 
054         * @param array  the array to iterate over, must not be null
055         * @throws IllegalArgumentException if the array is null
056         */
057        public static ArrayIntListIterator copyOf(int[] array) {
058            if (array == null) {
059                throw new IllegalArgumentException("Array must not be null");
060            }
061            return new ArrayIntListIterator(array.clone());
062        }
063    
064        /**
065         * Constructs an iterator over an array of <code>int</code> values.
066         * <p>
067         * The array is assigned internally, thus the caller holds a reference to
068         * the internal state of the returned iterator. It is not recommended to
069         * modify the state of the array after construction.
070         * 
071         * @param array  the array to iterate over, must not be null
072         * @throws IllegalArgumentException if the array is null
073         */
074        public ArrayIntListIterator(int[] array) {
075            super();
076            if (array == null) {
077                throw new IllegalArgumentException("Array must not be null");
078            }
079            this.array = array;
080        }
081    
082        //-----------------------------------------------------------------------
083        public boolean isModifiable() {
084            return true;
085        }
086    
087        public boolean isResettable() {
088            return true;
089        }
090    
091        //-----------------------------------------------------------------------
092        public boolean hasNext() {
093            return (cursor < array.length);
094        }
095    
096        public int nextIndex() {
097            return cursor;
098        }
099    
100        public int nextInt() {
101            if (hasNext() == false) {
102                throw new NoSuchElementException("No more elements available");
103            }
104            last = cursor;
105            return array[cursor++];
106        }
107    
108        public Integer next() {
109            return IntUtils.toObject(nextInt());
110        }
111    
112        public boolean hasPrevious() {
113            return (cursor > 0);
114        }
115    
116        public int previousIndex() {
117            return cursor - 1;
118        }
119    
120        public int previousInt() {
121            if (hasPrevious() == false) {
122                throw new NoSuchElementException("No more elements available");
123            }
124            last = --cursor;
125            return array[cursor];
126        }
127    
128        public Integer previous() {
129            return IntUtils.toObject(previousInt());
130        }
131    
132        public void add(int value) {
133            throw new UnsupportedOperationException("ArrayIntListIterator does not support add");
134        }
135    
136        public void add(Integer value) {
137            throw new UnsupportedOperationException("ArrayIntListIterator does not support add");
138        }
139    
140        public void remove() {
141            throw new UnsupportedOperationException("ArrayIntListIterator does not support remove");
142        }
143    
144        public void set(int value) {
145            if (last < 0) {
146                throw new IllegalStateException("ArrayIntListIterator cannot be set until next is called");
147            }
148            array[last] = value;
149        }
150    
151        public void set(Integer value) {
152            set(IntUtils.toPrimitive(value));
153        }
154    
155        public void reset() {
156            cursor = 0;
157            last = -1;
158        }
159    
160    }