001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
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
017 package jet.runtime;
018
019 import jet.*;
020
021 import java.util.Iterator;
022
023 public abstract class ArrayIterator<T> implements Iterator<T> {
024 private final int size;
025 protected int index;
026
027 protected ArrayIterator(int size) {
028 this.size = size;
029 }
030
031 @Override
032 public boolean hasNext() {
033 return index < size;
034 }
035
036 private static class GenericIterator<T> extends ArrayIterator<T> {
037 private final T[] array;
038
039 private GenericIterator(T[] array) {
040 super(array.length);
041 this.array = array;
042 }
043
044 @Override
045 public T next() {
046 return array[index++];
047 }
048
049 @Override
050 public void remove() {
051 throw new UnsupportedOperationException("Mutating method called on a Kotlin Iterator");
052 }
053 }
054
055 public static <T> Iterator<T> iterator(T[] array) {
056 return new GenericIterator<T>(array);
057 }
058
059 private static class ArrayByteIterator extends ByteIterator {
060 private final byte[] array;
061 private int index;
062
063 @Override
064 public boolean hasNext() {
065 return index < array.length;
066 }
067
068 private ArrayByteIterator(byte[] array) {
069 this.array = array;
070 }
071
072 @Override
073 public byte nextByte() {
074 return array[index++];
075 }
076 }
077
078 public static ByteIterator iterator(byte[] array) {
079 return new ArrayByteIterator(array);
080 }
081
082 private static class ArrayShortIterator extends ShortIterator {
083 private final short[] array;
084
085 private int index;
086
087 @Override
088 public boolean hasNext() {
089 return index < array.length;
090 }
091
092 private ArrayShortIterator(short[] array) {
093 this.array = array;
094 }
095
096 @Override
097 public short nextShort() {
098 return array[index++];
099 }
100 }
101
102 public static ShortIterator iterator(short[] array) {
103 return new ArrayShortIterator(array);
104 }
105
106 private static class ArrayIntIterator extends IntIterator {
107 private final int[] array;
108
109 private int index;
110
111 @Override
112 public boolean hasNext() {
113 return index < array.length;
114 }
115
116 private ArrayIntIterator(int[] array) {
117 this.array = array;
118 }
119
120 @Override
121 public int nextInt() {
122 return array[index++];
123 }
124 }
125
126 public static IntIterator iterator(int[] array) {
127 return new ArrayIntIterator(array);
128 }
129
130 private static class ArrayLongIterator extends LongIterator {
131 private final long[] array;
132
133 private int index;
134
135 @Override
136 public boolean hasNext() {
137 return index < array.length;
138 }
139
140 private ArrayLongIterator(long[] array) {
141 this.array = array;
142 }
143
144 @Override
145 public long nextLong() {
146 return array[index++];
147 }
148 }
149
150 public static LongIterator iterator(long[] array) {
151 return new ArrayLongIterator(array);
152 }
153
154 private static class ArrayFloatIterator extends FloatIterator {
155 private final float[] array;
156
157 private int index;
158
159 @Override
160 public boolean hasNext() {
161 return index < array.length;
162 }
163
164 private ArrayFloatIterator(float[] array) {
165 this.array = array;
166 }
167
168 @Override
169 public float nextFloat() {
170 return array[index++];
171 }
172 }
173
174 public static FloatIterator iterator(float[] array) {
175 return new ArrayFloatIterator(array);
176 }
177
178 private static class ArrayDoubleIterator extends DoubleIterator {
179 private final double[] array;
180
181 private int index;
182
183 @Override
184 public boolean hasNext() {
185 return index < array.length;
186 }
187
188 private ArrayDoubleIterator(double[] array) {
189 this.array = array;
190 }
191
192 @Override
193 public double nextDouble() {
194 return array[index++];
195 }
196 }
197
198 public static DoubleIterator iterator(double[] array) {
199 return new ArrayDoubleIterator(array);
200 }
201
202 private static class ArrayCharacterIterator extends CharIterator {
203 private final char[] array;
204
205 private int index;
206
207 @Override
208 public boolean hasNext() {
209 return index < array.length;
210 }
211
212 private ArrayCharacterIterator(char[] array) {
213 this.array = array;
214 }
215
216 @Override
217 public char nextChar() {
218 return array[index++];
219 }
220 }
221
222 public static CharIterator iterator(char[] array) {
223 return new ArrayCharacterIterator(array);
224 }
225
226 private static class ArrayBooleanIterator extends BooleanIterator {
227 private final boolean[] array;
228
229 private int index;
230
231 @Override
232 public boolean hasNext() {
233 return index < array.length;
234 }
235
236 private ArrayBooleanIterator(boolean[] array) {
237 this.array = array;
238 }
239
240 @Override
241 public boolean nextBoolean() {
242 return array[index++];
243 }
244 }
245
246 public static BooleanIterator iterator(boolean[] array) {
247 return new ArrayBooleanIterator(array);
248 }
249 }