/
AbstractArrayAdapter.java
567 lines (484 loc) · 17.3 KB
/
AbstractArrayAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* Copyright (c) 2016 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.list.fixed;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Optional;
import java.util.RandomAccess;
import org.eclipse.collections.api.block.HashingStrategy;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.Function3;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.tuple.Twin;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.list.mutable.AbstractMutableList;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.list.mutable.ListAdapter;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.ListIterate;
import org.eclipse.collections.impl.utility.internal.InternalArrayIterate;
import org.eclipse.collections.impl.utility.internal.RandomAccessListIterate;
public abstract class AbstractArrayAdapter<T>
extends AbstractMutableList<T>
implements RandomAccess
{
private static final Object[] OBJECTS = {};
protected T[] items;
/**
* This method must be here so subclasses can be serializable.
*/
protected AbstractArrayAdapter()
{
this.items = (T[]) OBJECTS;
}
protected AbstractArrayAdapter(T[] newElements)
{
if (newElements == null)
{
throw new IllegalArgumentException("items cannot be null");
}
this.items = newElements;
}
@Override
public boolean notEmpty()
{
return this.items.length > 0;
}
@Override
public T getFirst()
{
return this.isEmpty() ? null : this.items[0];
}
@Override
public T getLast()
{
return this.isEmpty() ? null : this.items[this.items.length - 1];
}
@Override
public void each(Procedure<? super T> procedure)
{
int size = this.size();
for (int i = 0; i < size; i++)
{
procedure.value(this.items[i]);
}
}
@Override
public void forEachWithIndex(ObjectIntProcedure<? super T> objectIntProcedure)
{
InternalArrayIterate.forEachWithIndex(this.items, this.items.length, objectIntProcedure);
}
@Override
public void forEachWithIndex(int fromIndex, int toIndex, ObjectIntProcedure<? super T> objectIntProcedure)
{
ListIterate.rangeCheck(fromIndex, toIndex, this.items.length);
InternalArrayIterate.forEachWithIndexWithoutChecks(this.items, fromIndex, toIndex, objectIntProcedure);
}
@Override
public boolean removeIf(Predicate<? super T> predicate)
{
throw new UnsupportedOperationException("Cannot call removeIf() on " + this.getClass().getSimpleName());
}
@Override
public <P> boolean removeIfWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
throw new UnsupportedOperationException("Cannot call removeIfWith() on " + this.getClass().getSimpleName());
}
@Override
public T detect(Predicate<? super T> predicate)
{
return InternalArrayIterate.detect(this.items, this.items.length, predicate);
}
@Override
public <P> T detectWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return InternalArrayIterate.detectWith(this.items, this.items.length, predicate, parameter);
}
@Override
public Optional<T> detectOptional(Predicate<? super T> predicate)
{
return InternalArrayIterate.detectOptional(this.items, this.items.length, predicate);
}
@Override
public <P> Optional<T> detectWithOptional(Predicate2<? super T, ? super P> predicate, P parameter)
{
return InternalArrayIterate.detectWithOptional(this.items, this.items.length, predicate, parameter);
}
@Override
public int detectIndex(Predicate<? super T> predicate)
{
return InternalArrayIterate.detectIndex(this.items, this.items.length, predicate);
}
@Override
public int detectLastIndex(Predicate<? super T> predicate)
{
return InternalArrayIterate.detectLastIndex(this.items, this.items.length, predicate);
}
@Override
public int count(Predicate<? super T> predicate)
{
return InternalArrayIterate.count(this.items, this.items.length, predicate);
}
@Override
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super T, ? super S> predicate)
{
return InternalArrayIterate.corresponds(this.items, this.items.length, other, predicate);
}
@Override
public boolean anySatisfy(Predicate<? super T> predicate)
{
return InternalArrayIterate.anySatisfy(this.items, this.items.length, predicate);
}
@Override
public boolean allSatisfy(Predicate<? super T> predicate)
{
return InternalArrayIterate.allSatisfy(this.items, this.items.length, predicate);
}
@Override
public boolean noneSatisfy(Predicate<? super T> predicate)
{
return InternalArrayIterate.noneSatisfy(this.items, this.items.length, predicate);
}
@Override
public <IV> IV injectInto(IV injectedValue, Function2<? super IV, ? super T, ? extends IV> function)
{
return ArrayIterate.injectInto(injectedValue, this.items, function);
}
@Override
public MutableList<T> select(Predicate<? super T> predicate)
{
return this.select(predicate, FastList.newList());
}
@Override
public <R extends Collection<T>> R select(Predicate<? super T> predicate, R target)
{
return InternalArrayIterate.select(this.items, this.items.length, predicate, target);
}
@Override
public MutableList<T> reject(Predicate<? super T> predicate)
{
return this.reject(predicate, FastList.newList());
}
@Override
public <R extends Collection<T>> R reject(Predicate<? super T> predicate, R target)
{
return InternalArrayIterate.reject(this.items, this.items.length, predicate, target);
}
@Override
public <V> MutableList<V> collect(Function<? super T, ? extends V> function)
{
return this.collect(function, FastList.newList(this.size()));
}
@Override
public <V, R extends Collection<V>> R collect(Function<? super T, ? extends V> function, R target)
{
return InternalArrayIterate.collect(this.items, this.items.length, function, target);
}
@Override
public <V> MutableList<V> collectIf(
Predicate<? super T> predicate,
Function<? super T, ? extends V> function)
{
return this.collectIf(predicate, function, FastList.newList());
}
@Override
public <V, R extends Collection<V>> R collectIf(
Predicate<? super T> predicate, Function<? super T, ? extends V> function, R target)
{
return InternalArrayIterate.collectIf(this.items, this.items.length, predicate, function, target);
}
@Override
public <V> MutableList<V> flatCollect(Function<? super T, ? extends Iterable<V>> function)
{
return InternalArrayIterate.flatCollect(this.items, this.items.length, function, FastList.newList(this.items.length));
}
@Override
public <V, R extends Collection<V>> R flatCollect(Function<? super T, ? extends Iterable<V>> function, R target)
{
return InternalArrayIterate.flatCollect(this.items, this.items.length, function, target);
}
@Override
public <P> Twin<MutableList<T>> selectAndRejectWith(
Predicate2<? super T, ? super P> predicate,
P parameter)
{
return InternalArrayIterate.selectAndRejectWith(this.items, this.items.length, predicate, parameter);
}
@Override
public int size()
{
return this.items.length;
}
@Override
public boolean isEmpty()
{
return this.items.length == 0;
}
@Override
public boolean contains(Object o)
{
return InternalArrayIterate.anySatisfyWith(this.items, this.items.length, Predicates2.equal(), o);
}
@Override
public Iterator<T> iterator()
{
return Arrays.asList(this.items).iterator();
}
@Override
public Object[] toArray()
{
return this.items.clone();
}
@Override
public <E> E[] toArray(E[] array)
{
int size = this.size();
E[] result = array.length < size
? (E[]) Array.newInstance(array.getClass().getComponentType(), size)
: array;
System.arraycopy(this.items, 0, result, 0, size);
if (result.length > size)
{
result[size] = null;
}
return result;
}
@Override
public boolean remove(Object o)
{
throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName());
}
@Override
public boolean containsAll(Collection<?> collection)
{
return Iterate.allSatisfyWith(collection, Predicates2.in(), this);
}
@Override
public boolean addAll(Collection<? extends T> collection)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
@Override
public boolean addAllIterable(Iterable<? extends T> iterable)
{
throw new UnsupportedOperationException("Cannot call addAllIterable() on " + this.getClass().getSimpleName());
}
@Override
public boolean removeAll(Collection<?> collection)
{
throw new UnsupportedOperationException("Cannot call removeAll() on " + this.getClass().getSimpleName());
}
@Override
public boolean removeAllIterable(Iterable<?> iterable)
{
throw new UnsupportedOperationException("Cannot call removeAllIterable() on " + this.getClass().getSimpleName());
}
@Override
public boolean retainAll(Collection<?> collection)
{
throw new UnsupportedOperationException("Cannot call retainAll() on " + this.getClass().getSimpleName());
}
@Override
public boolean retainAllIterable(Iterable<?> iterable)
{
throw new UnsupportedOperationException("Cannot call retainAllIterable() on " + this.getClass().getSimpleName());
}
@Override
public void clear()
{
throw new UnsupportedOperationException("Cannot call clear() on " + this.getClass().getSimpleName());
}
@Override
public boolean addAll(int index, Collection<? extends T> collection)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
@Override
public T get(int index)
{
if (index >= this.size())
{
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size());
}
return this.items[index];
}
@Override
public void add(int index, T element)
{
throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName());
}
@Override
public T remove(int index)
{
throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName());
}
@Override
public int indexOf(Object item)
{
return InternalArrayIterate.indexOf(this.items, this.items.length, item);
}
@Override
public int lastIndexOf(Object item)
{
return InternalArrayIterate.lastIndexOf(this.items, this.items.length, item);
}
@Override
public ListIterator<T> listIterator(int index)
{
return Arrays.asList(this.items).listIterator(index);
}
@Override
public MutableList<T> subList(int fromIndex, int toIndex)
{
return ListAdapter.adapt(Arrays.asList(this.items).subList(fromIndex, toIndex));
}
@Override
public boolean equals(Object that)
{
if (that == this)
{
return true;
}
if (!(that instanceof List))
{
return false;
}
if (that instanceof AbstractArrayAdapter)
{
return this.abstractArrayAdapterEquals((AbstractArrayAdapter<?>) that);
}
return InternalArrayIterate.arrayEqualsList(this.items, this.items.length, (List<?>) that);
}
public boolean abstractArrayAdapterEquals(AbstractArrayAdapter<?> list)
{
return Arrays.equals(this.items, list.items);
}
@Override
public int hashCode()
{
return Arrays.hashCode(this.items);
}
@Override
public <P> void forEachWith(Procedure2<? super T, ? super P> procedure, P parameter)
{
for (T each : this.items)
{
procedure.value(each, parameter);
}
}
@Override
public <P> MutableList<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return this.selectWith(predicate, parameter, FastList.newList());
}
@Override
public <P, R extends Collection<T>> R selectWith(
Predicate2<? super T, ? super P> predicate,
P parameter,
R target)
{
return InternalArrayIterate.selectWith(this.items, this.items.length, predicate, parameter, target);
}
@Override
public <P> MutableList<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return this.rejectWith(predicate, parameter, FastList.newList());
}
@Override
public <P, R extends Collection<T>> R rejectWith(
Predicate2<? super T, ? super P> predicate,
P parameter,
R target)
{
return InternalArrayIterate.rejectWith(this.items, this.items.length, predicate, parameter, target);
}
@Override
public <P, A> MutableList<A> collectWith(Function2<? super T, ? super P, ? extends A> function, P parameter)
{
return this.collectWith(function, parameter, FastList.newList());
}
@Override
public <P, A, R extends Collection<A>> R collectWith(
Function2<? super T, ? super P, ? extends A> function,
P parameter,
R target)
{
return InternalArrayIterate.collectWith(this.items, this.items.length, function, parameter, target);
}
@Override
public <IV, P> IV injectIntoWith(
IV injectValue, Function3<? super IV, ? super T, ? super P, ? extends IV> function, P parameter)
{
return ArrayIterate.injectIntoWith(injectValue, this.items, function, parameter);
}
@Override
public void forEach(int fromIndex, int toIndex, Procedure<? super T> procedure)
{
ListIterate.rangeCheck(fromIndex, toIndex, this.items.length);
InternalArrayIterate.forEachWithoutChecks(this.items, fromIndex, toIndex, procedure);
}
@Override
public <P> int countWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return InternalArrayIterate.countWith(this.items, this.items.length, predicate, parameter);
}
@Override
public <P> boolean anySatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return InternalArrayIterate.anySatisfyWith(this.items, this.items.length, predicate, parameter);
}
@Override
public <P> boolean allSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return InternalArrayIterate.allSatisfyWith(this.items, this.items.length, predicate, parameter);
}
@Override
public <P> boolean noneSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return InternalArrayIterate.noneSatisfyWith(this.items, this.items.length, predicate, parameter);
}
@Override
public MutableList<T> distinct()
{
return InternalArrayIterate.distinct(this.items, this.items.length);
}
@Override
public MutableList<T> distinct(HashingStrategy<? super T> hashingStrategy)
{
return InternalArrayIterate.distinct(this.items, this.items.length, hashingStrategy);
}
@Override
public void appendString(Appendable appendable, String start, String separator, String end)
{
InternalArrayIterate.appendString(this, this.items, this.items.length, appendable, start, separator, end);
}
@Override
public MutableList<T> take(int count)
{
return RandomAccessListIterate.take(this, count);
}
@Override
public MutableList<T> drop(int count)
{
return RandomAccessListIterate.drop(this, count);
}
}