1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.primitives.collection.impl;
17
18 import java.util.Collection;
19 import java.util.Iterator;
20 import java.util.NoSuchElementException;
21
22 import org.joda.primitives.LongUtils;
23 import org.joda.primitives.collection.LongCollection;
24 import org.joda.primitives.iterator.LongIterator;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class ArrayLongCollection extends AbstractLongCollection implements Cloneable {
44
45
46
47 private static final int MIN_GROWTH_SIZE = 4;
48
49 private static final int GROWTH_FACTOR_MULTIPLIER = 3;
50
51 private static final int GROWTH_FACTOR_DIVISOR = 2;
52
53
54 private long[] data;
55
56 private int size;
57
58
59
60
61 public ArrayLongCollection() {
62 super();
63 data = LongUtils.EMPTY_LONG_ARRAY;
64 }
65
66
67
68
69
70
71 public ArrayLongCollection(int initialSize) {
72 super();
73 if (initialSize <= 0) {
74 data = LongUtils.EMPTY_LONG_ARRAY;
75 } else {
76 data = new long[initialSize];
77 }
78 }
79
80
81
82
83
84
85 public ArrayLongCollection(long[] values) {
86 super();
87 if (values == null) {
88 data = LongUtils.EMPTY_LONG_ARRAY;
89 } else {
90 data = (long[]) values.clone();
91 size = values.length;
92 }
93 }
94
95
96
97
98
99
100 public ArrayLongCollection(Collection<?> coll) {
101 super();
102 if (coll == null) {
103 data = LongUtils.EMPTY_LONG_ARRAY;
104 } else if (coll instanceof LongCollection) {
105 LongCollection c = (LongCollection) coll;
106 size = c.size();
107 data = new long[size];
108 c.toLongArray(data, 0);
109 } else {
110 data = toPrimitiveArray(coll);
111 size = coll.size();
112 }
113 }
114
115
116
117
118
119
120 public ArrayLongCollection(Iterator<Long> it) {
121 super();
122 if (it == null) {
123 data = LongUtils.EMPTY_LONG_ARRAY;
124 } else if (it instanceof LongIterator) {
125 LongIterator typed = (LongIterator) it;
126 data = new long[MIN_GROWTH_SIZE];
127 while (typed.hasNext()) {
128 add(typed.nextLong());
129 }
130 } else {
131 data = new long[MIN_GROWTH_SIZE];
132 while (it.hasNext()) {
133 add(it.next());
134 }
135 }
136 }
137
138
139
140
141
142
143
144
145 public int size() {
146 return size;
147 }
148
149
150
151
152
153
154 public LongIterator iterator() {
155 return new PIterator(this);
156 }
157
158
159
160
161
162
163
164
165 public boolean add(long value) {
166 ensureCapacity(size + 1);
167 data[size++] = value;
168 return true;
169 }
170
171
172
173
174
175
176
177
178
179 public void optimize() {
180 if (size < data.length) {
181 long[] array = new long[size];
182 System.arraycopy(data, 0, array, 0, size);
183 data = array;
184 }
185 }
186
187
188
189
190
191
192 protected boolean isAddModifiable() {
193 return true;
194 }
195
196
197
198
199
200
201 protected boolean isRemoveModifiable() {
202 return true;
203 }
204
205
206
207
208
209
210 public boolean isModifiable() {
211 return true;
212 }
213
214
215
216
217
218
219
220
221
222 public boolean contains(long value) {
223 for (int i = 0; i < size; i++) {
224 if (data[i] == value) {
225 return true;
226 }
227 }
228 return false;
229 }
230
231
232
233
234
235
236
237 public void clear() {
238 size = 0;
239 }
240
241
242
243
244
245
246
247 public boolean addAll(long[] values) {
248 checkAddModifiable();
249 if (values == null || values.length == 0) {
250 return false;
251 }
252 return doAdd(0, values);
253 }
254
255
256
257
258
259
260
261 public boolean addAll(LongCollection values) {
262 checkAddModifiable();
263 if (values == null || values.size() == 0) {
264 return false;
265 }
266 int len = values.size();
267 ensureCapacity(size + len);
268 values.toLongArray(data, size);
269 size += len;
270 return true;
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285 public boolean addAll(long startInclusive, long endInclusive) {
286 long increaseLong = endInclusive - startInclusive + 1;
287 if (increaseLong < 0L) {
288 return false;
289 }
290 long newSize = size + increaseLong;
291 if (newSize > Integer.MAX_VALUE) {
292 throw new IllegalArgumentException("Range too large");
293 }
294 ensureCapacity((int) newSize);
295 long i = startInclusive;
296 while (i < endInclusive) {
297 data[size++] = i++;
298 }
299 data[size++] = i;
300 return true;
301 }
302
303
304
305
306
307
308 public Object clone() {
309 ArrayLongCollection cloned = (ArrayLongCollection) super.clone();
310 cloned.data = (long[]) data.clone();
311 return cloned;
312 }
313
314
315
316
317
318
319
320
321
322
323 protected void arrayCopy(int fromIndex, long[] dest, int destIndex, int size) {
324 System.arraycopy(data, fromIndex, dest, destIndex, size);
325 }
326
327
328
329
330
331
332
333
334
335
336
337 protected boolean doAdd(int index, long[] values) {
338 int len = values.length;
339 ensureCapacity(size + len);
340 System.arraycopy(values, 0, data, size, len);
341 size += len;
342 return (len > 0);
343 }
344
345
346
347
348
349
350 protected void doRemoveIndex(int index) {
351 System.arraycopy(data, index + 1, data, index, size - 1 - index);
352 size--;
353 }
354
355
356
357
358
359
360
361 protected void ensureCapacity(int reqCapacity) {
362 int curCapacity = data.length;
363 if (reqCapacity <= curCapacity) {
364 return;
365 }
366 int newCapacity = curCapacity * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR;
367 if ((newCapacity - curCapacity) < MIN_GROWTH_SIZE) {
368 newCapacity = curCapacity + MIN_GROWTH_SIZE;
369 }
370 if (newCapacity < reqCapacity) {
371 newCapacity = reqCapacity;
372 }
373 long[] newArray = new long[newCapacity];
374 System.arraycopy(data, 0, newArray, 0, curCapacity);
375 data = newArray;
376 }
377
378
379
380
381
382
383 protected static class PIterator implements LongIterator {
384
385 private final ArrayLongCollection collection;
386 private int cursor = 0;
387 private boolean canRemove = false;
388
389 protected PIterator(ArrayLongCollection coll) {
390 super();
391 this.collection = coll;
392 }
393
394 public boolean hasNext() {
395 return (cursor < collection.size);
396 }
397
398 public long nextLong() {
399 if (hasNext() == false) {
400 throw new NoSuchElementException("No more elements available");
401 }
402 canRemove = true;
403 return collection.data[cursor++];
404 }
405
406 public Long next() {
407 return collection.toObject(nextLong());
408 }
409
410 public void remove() {
411 if (canRemove == false) {
412 throw new IllegalStateException("Element cannot be removed");
413 }
414 collection.doRemoveIndex(--cursor);
415 canRemove = false;
416 }
417
418 public boolean isModifiable() {
419 return collection.isModifiable();
420 }
421
422 public boolean isResettable() {
423 return true;
424 }
425
426 public void reset() {
427 cursor = 0;
428 }
429 }
430
431 }