View Javadoc

1   /*
2    *  Copyright 2001-2010 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.primitives.iterator.impl;
17  
18  import java.util.NoSuchElementException;
19  
20  import org.joda.primitives.DoubleUtils;
21  import org.joda.primitives.iterator.DoubleIterator;
22  
23  /**
24   * An iterator over an array of <code>double</code> values.
25   * <p>
26   * This class implements {@link java.util.Iterator Iterator} allowing
27   * seamless integration with other APIs.
28   * <p>
29   * The iterator can be reset to the start if required.
30   * It is unmodifiable and <code>remove()</code> is unsupported.
31   *
32   * @author Stephen Colebourne
33   * @author Jason Tiscione
34   * @version CODE GENERATED
35   * @since 1.0
36   */
37  public class ArrayDoubleIterator implements DoubleIterator {
38      // This file is CODE GENERATED. Do not change manually.
39  
40      /** The array to iterate over */
41      protected final double[] array;
42      /** Cursor position */
43      protected int cursor = 0;
44  
45      /**
46       * Creates an iterator over a copy of an array of <code>double</code> values.
47       * <p>
48       * The specified array is copied, making this class effectively immutable.
49       * Note that the class is not {@code final} thus it is not truly immutable.
50       * 
51       * @param array  the array to iterate over, must not be null
52       * @throws IllegalArgumentException if the array is null
53       */
54      public static ArrayDoubleIterator copyOf(double[] array) {
55          if (array == null) {
56              throw new IllegalArgumentException("Array must not be null");
57          }
58          return new ArrayDoubleIterator(array.clone());
59      }
60  
61      /**
62       * Constructs an iterator over an array of <code>double</code> values.
63       * <p>
64       * The array is assigned internally, thus the caller holds a reference to
65       * the internal state of the returned iterator. It is not recommended to
66       * modify the state of the array after construction.
67       * 
68       * @param array  the array to iterate over, must not be null
69       * @throws IllegalArgumentException if the array is null
70       */
71      public ArrayDoubleIterator(double[] array) {
72          super();
73          if (array == null) {
74              throw new IllegalArgumentException("Array must not be null");
75          }
76          this.array = array;
77      }
78  
79      //-----------------------------------------------------------------------
80      public boolean isModifiable() {
81          return false;
82      }
83  
84      public boolean isResettable() {
85          return true;
86      }
87  
88      //-----------------------------------------------------------------------
89      public boolean hasNext() {
90          return (cursor < array.length);
91      }
92  
93      public double nextDouble() {
94          if (hasNext() == false) {
95              throw new NoSuchElementException("No more elements available");
96          }
97          return array[cursor++];
98      }
99  
100     public Double next() {
101         return DoubleUtils.toObject(nextDouble());
102     }
103 
104     public void remove() {
105         throw new UnsupportedOperationException("ArrayDoubleIterator does not support remove");
106     }
107 
108     public void reset() {
109         cursor = 0;
110     }
111 
112 }