001    package com.softnetConsult.utils.collections;
002    
003    import java.util.Iterator;
004    import java.util.NoSuchElementException;
005    
006    /**
007     * Implements an iterator over a short array.
008     * 
009     * <p style="font-size:smaller;">This product includes software developed by the
010     *    <strong>SoftNet-Consult Java Utility Library</strong> project and its contributors.<br />
011     *    (<a href="http://java-tools.sourceforge.net" target="_blank">http://java-tools.sourceforge.net</a>)<br />
012     *    Copyright (c) 2007-2008 SoftNet-Consult.<br />
013     *    Copyright (c) 2007-2008 G. Paperin.<br />
014     *    All rights reserved.
015     * </p>
016     * <p style="font-size:smaller;">File: AttayIterator.java<br />
017     *    Library API version: {@value com.softnetConsult.utils.APIProperties#apiVersion}<br />
018     *    Java compliance version: {@value com.softnetConsult.utils.APIProperties#javaComplianceVersion}
019     * </p>
020     * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or
021     *    without modification, are permitted provided that the following terms and conditions are met:
022     * </p>
023     * <p style="font-size:smaller;">1. Redistributions of source code must retain the above
024     *    acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright
025     *    notice, this list of conditions and the following disclaimer.<br />
026     *    2. Redistributions in binary form must reproduce the above acknowledgement of the
027     *    SoftNet-Consult Java Utility Library project, the above copyright notice, this list of
028     *    conditions and the following disclaimer in the documentation and/or other materials
029     *    provided with the distribution.<br />
030     *    3. All advertising materials mentioning features or use of this software or any derived
031     *    software must display the following acknowledgement:<br />
032     *    <em>This product includes software developed by the SoftNet-Consult Java Utility Library
033     *    project and its contributors.</em>
034     * </p>
035     * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY
036     *    OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
037     *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT SHALL
038     *    THE AUTHORS, CONTRIBUTORS OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
039     *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  FROM, OUT OF OR
040     *    IN CONNECTION WITH THE SOFTWARE OR THE USE OR  OTHER DEALINGS IN THE SOFTWARE.
041     * </p> 
042     * @author Greg Paperin (<a href="http://www.paperin.org" target="_blank">http://www.paperin.org</a>)
043     * @version {@value com.softnetConsult.utils.APIProperties#apiVersion}
044     */
045    public class ArrayIteratorShort implements Iterator<Short>, ArrayIteratorPrimitive {
046    
047    /**
048     * Array being iterated.
049     */
050    private short[] array = null;
051    
052    /**
053     * Cursor into the array.
054     */
055    private int cursor = -1;
056    
057    
058    /**
059     * Creates an array iterator over the specified array.
060     * 
061     * @param array Array to be iterated.
062     */
063    public ArrayIteratorShort(short[] array) {
064            if (null == array)
065                    throw new NullPointerException("Cannot create an iterator on a null-array.");
066            this.array = array;
067            this.cursor = 0;
068    }
069    
070    /**
071     * Returns {@code true} if this iteration has more elements.
072     * 
073     * @return {@code true} if {@code next()} would return an element rather than throwing an exception,
074     * {@code false} otherwise.
075     */
076    public boolean hasNext() {      
077            if (cursor < array.length)
078                    return true;
079            return false;
080    }
081    
082    /**
083     * Returns the next element in this iteration.
084     * 
085     * @return The next element in this iteration.
086     * @throws NoSuchElementException If this iteration has no more elements.
087     */
088    public short nextValue() {      
089            if (cursor < 0 || array.length <= cursor)
090                    throw new NoSuchElementException("Array element index " + cursor + " is illegal, "
091                                                                               + "this iterator's array has " + array.length + " elements");
092            return array[cursor++];
093    }
094    
095    /**
096     * Returns the next element in this iteration converted to the appropriate wrapper type.
097     * 
098     * @return The next element in this iteration converted to the appropriate wrapper type.
099     * @throws NoSuchElementException If this iteration has no more elements.
100     */
101    public Short next() {   
102            return Short.valueOf(nextValue());
103    }
104    
105    /**
106     * Removing of array elements is obviously not supported.
107     * 
108     * @throws UnsupportedOperationException is always thrown.
109     */
110    public void remove() {
111            throw new UnsupportedOperationException("Cannot remove elements from an array");
112    }
113    
114    /**
115     * Resets the internal cursor of this iterator to point to the start of the unederlying array.
116     * This method allows traversing an array several times using the same iterator.
117     */
118    public void restart() {
119            cursor = 0;
120    }
121    
122    } // public class ArrayIteratorShort