-
Notifications
You must be signed in to change notification settings - Fork 587
/
ArrayListAdapter.java
531 lines (458 loc) · 16.6 KB
/
ArrayListAdapter.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
/*
* Copyright (c) 2018 Goldman Sachs and others.
* 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.mutable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
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.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
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.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.MutableBooleanList;
import org.eclipse.collections.api.list.primitive.MutableByteList;
import org.eclipse.collections.api.list.primitive.MutableCharList;
import org.eclipse.collections.api.list.primitive.MutableDoubleList;
import org.eclipse.collections.api.list.primitive.MutableFloatList;
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.api.list.primitive.MutableLongList;
import org.eclipse.collections.api.list.primitive.MutableShortList;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.list.PartitionMutableList;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.procedure.CollectionAddProcedure;
import org.eclipse.collections.impl.lazy.ReverseIterable;
import org.eclipse.collections.impl.list.mutable.primitive.BooleanArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.ByteArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.CharArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.DoubleArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.FloatArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.LongArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.ShortArrayList;
import org.eclipse.collections.impl.multimap.list.FastListMultimap;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.ArrayListIterate;
import org.eclipse.collections.impl.utility.internal.RandomAccessListIterate;
/**
* This class provides a MutableList wrapper around a JDK Collections ArrayList instance. All of the MutableList
* interface methods are supported in addition to the JDK ArrayList methods.
* <p>
* To create a new wrapper around an existing ArrayList instance, use the {@link #adapt(ArrayList)} factory method. To
* create a new empty wrapper, use the {@link #newList()} or {@link #newList(int)} factory methods.
*/
public final class ArrayListAdapter<T>
extends AbstractListAdapter<T>
implements RandomAccess, Serializable
{
private static final long serialVersionUID = 1L;
private final ArrayList<T> delegate;
private ArrayListAdapter(ArrayList<T> newDelegate)
{
if (newDelegate == null)
{
throw new NullPointerException("ArrayListAdapter may not wrap null");
}
this.delegate = newDelegate;
}
@Override
protected ArrayList<T> getDelegate()
{
return this.delegate;
}
public static <E> ArrayListAdapter<E> newList()
{
return new ArrayListAdapter<>(new ArrayList<>());
}
public static <E> ArrayListAdapter<E> newList(int size)
{
return new ArrayListAdapter<>(new ArrayList<>(size));
}
public static <E> ArrayListAdapter<E> adapt(ArrayList<E> newDelegate)
{
return new ArrayListAdapter<>(newDelegate);
}
@Override
public MutableList<T> asUnmodifiable()
{
return UnmodifiableMutableList.of(this);
}
@Override
public MutableList<T> asSynchronized()
{
return SynchronizedMutableList.of(this);
}
@Override
public ArrayListAdapter<T> clone()
{
return new ArrayListAdapter<>((ArrayList<T>) this.delegate.clone());
}
@Override
public ArrayListAdapter<T> newEmpty()
{
return ArrayListAdapter.newList();
}
@Override
public void forEach(Procedure<? super T> procedure)
{
this.each(procedure);
}
@Override
public void each(Procedure<? super T> procedure)
{
ArrayListIterate.forEach(this.delegate, procedure);
}
@Override
public void reverseForEach(Procedure<? super T> procedure)
{
ArrayListIterate.reverseForEach(this.delegate, procedure);
}
@Override
public void reverseForEachWithIndex(ObjectIntProcedure<? super T> procedure)
{
ArrayListIterate.reverseForEachWithIndex(this.delegate, procedure);
}
@Override
public void forEachWithIndex(ObjectIntProcedure<? super T> objectIntProcedure)
{
ArrayListIterate.forEachWithIndex(this.delegate, objectIntProcedure);
}
@Override
public void forEachWithIndex(int fromIndex, int toIndex, ObjectIntProcedure<? super T> objectIntProcedure)
{
ArrayListIterate.forEachWithIndex(this.delegate, fromIndex, toIndex, objectIntProcedure);
}
@Override
public T detect(Predicate<? super T> predicate)
{
return ArrayListIterate.detect(this.delegate, predicate);
}
@Override
public T detectIfNone(Predicate<? super T> predicate, Function0<? extends T> function)
{
T result = this.detect(predicate);
return result == null ? function.value() : result;
}
@Override
public int count(Predicate<? super T> predicate)
{
return ArrayListIterate.count(this.delegate, predicate);
}
@Override
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super T, ? super S> predicate)
{
return RandomAccessListIterate.corresponds(this.delegate, other, predicate);
}
@Override
public boolean anySatisfy(Predicate<? super T> predicate)
{
return ArrayListIterate.anySatisfy(this.delegate, predicate);
}
@Override
public boolean allSatisfy(Predicate<? super T> predicate)
{
return ArrayListIterate.allSatisfy(this.delegate, predicate);
}
@Override
public boolean noneSatisfy(Predicate<? super T> predicate)
{
return ArrayListIterate.noneSatisfy(this.delegate, predicate);
}
@Override
public <IV> IV injectInto(IV injectedValue, Function2<? super IV, ? super T, ? extends IV> function)
{
return ArrayListIterate.injectInto(injectedValue, this.delegate, function);
}
@Override
public void forEach(int fromIndex, int toIndex, Procedure<? super T> procedure)
{
ArrayListIterate.forEach(this.delegate, fromIndex, toIndex, procedure);
}
/**
* @since 10.0 - Override for correctness
*/
@Override
public void sort(Comparator<? super T> comparator)
{
this.delegate.sort(comparator);
}
/**
* @since 10.0 - Override for backwards compatibility
*/
@Override
public ArrayListAdapter<T> sortThis(Comparator<? super T> comparator)
{
return (ArrayListAdapter<T>) super.sortThis(comparator);
}
/**
* @since 10.0 - Override for backwards compatibility
*/
@Override
public ArrayListAdapter<T> sortThis()
{
return (ArrayListAdapter<T>) super.sortThis();
}
@Override
public ArrayListAdapter<T> with(T element)
{
this.add(element);
return this;
}
public ArrayListAdapter<T> with(T element1, T element2)
{
this.add(element1);
this.add(element2);
return this;
}
public ArrayListAdapter<T> with(T element1, T element2, T element3)
{
this.add(element1);
this.add(element2);
this.add(element3);
return this;
}
public ArrayListAdapter<T> with(T... elements)
{
ArrayIterate.forEach(elements, CollectionAddProcedure.on(this.delegate));
return this;
}
@Override
public ArrayListAdapter<T> without(T element)
{
this.remove(element);
return this;
}
@Override
public ArrayListAdapter<T> withAll(Iterable<? extends T> elements)
{
this.addAllIterable(elements);
return this;
}
@Override
public ArrayListAdapter<T> withoutAll(Iterable<? extends T> elements)
{
this.removeAllIterable(elements);
return this;
}
private <E> ArrayListAdapter<E> wrap(ArrayList<E> list)
{
return ArrayListAdapter.adapt(list);
}
@Override
public ArrayListAdapter<T> select(Predicate<? super T> predicate)
{
return this.wrap(ArrayListIterate.select(this.delegate, predicate));
}
@Override
public ArrayListAdapter<T> reject(Predicate<? super T> predicate)
{
return this.wrap(ArrayListIterate.reject(this.delegate, predicate));
}
@Override
public PartitionMutableList<T> partition(Predicate<? super T> predicate)
{
return ArrayListIterate.partition(this.delegate, predicate);
}
@Override
public <P> PartitionMutableList<T> partitionWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return ArrayListIterate.partitionWith(this.getDelegate(), predicate, parameter);
}
@Override
public <S> MutableList<S> selectInstancesOf(Class<S> clazz)
{
return ArrayListIterate.selectInstancesOf(this.delegate, clazz);
}
@Override
public <V> ArrayListAdapter<V> collect(Function<? super T, ? extends V> function)
{
return this.wrap(ArrayListIterate.collect(this.delegate, function));
}
@Override
public MutableBooleanList collectBoolean(BooleanFunction<? super T> booleanFunction)
{
BooleanArrayList result = new BooleanArrayList(this.size());
this.each(each -> result.add(booleanFunction.booleanValueOf(each)));
return result;
}
@Override
public MutableByteList collectByte(ByteFunction<? super T> byteFunction)
{
ByteArrayList result = new ByteArrayList(this.size());
this.each(each -> result.add(byteFunction.byteValueOf(each)));
return result;
}
@Override
public MutableCharList collectChar(CharFunction<? super T> charFunction)
{
CharArrayList result = new CharArrayList(this.size());
this.each(each -> result.add(charFunction.charValueOf(each)));
return result;
}
@Override
public MutableDoubleList collectDouble(DoubleFunction<? super T> doubleFunction)
{
DoubleArrayList result = new DoubleArrayList(this.size());
this.each(each -> result.add(doubleFunction.doubleValueOf(each)));
return result;
}
@Override
public MutableFloatList collectFloat(FloatFunction<? super T> floatFunction)
{
FloatArrayList result = new FloatArrayList(this.size());
this.each(each -> result.add(floatFunction.floatValueOf(each)));
return result;
}
@Override
public MutableIntList collectInt(IntFunction<? super T> intFunction)
{
IntArrayList result = new IntArrayList(this.size());
this.each(each -> result.add(intFunction.intValueOf(each)));
return result;
}
@Override
public MutableLongList collectLong(LongFunction<? super T> longFunction)
{
LongArrayList result = new LongArrayList(this.size());
this.each(each -> result.add(longFunction.longValueOf(each)));
return result;
}
@Override
public MutableShortList collectShort(ShortFunction<? super T> shortFunction)
{
ShortArrayList result = new ShortArrayList(this.size());
this.each(each -> result.add(shortFunction.shortValueOf(each)));
return result;
}
@Override
public <V> ArrayListAdapter<V> flatCollect(Function<? super T, ? extends Iterable<V>> function)
{
return this.wrap(ArrayListIterate.flatCollect(this.delegate, function));
}
@Override
public <V> ArrayListAdapter<V> collectIf(Predicate<? super T> predicate, Function<? super T, ? extends V> function)
{
return this.wrap(ArrayListIterate.collectIf(this.delegate, predicate, function));
}
@Override
public int detectIndex(Predicate<? super T> predicate)
{
return ArrayListIterate.detectIndex(this.delegate, predicate);
}
@Override
public int detectLastIndex(Predicate<? super T> predicate)
{
return ArrayListIterate.detectLastIndex(this.delegate, predicate);
}
@Override
public <V> FastListMultimap<V, T> groupBy(Function<? super T, ? extends V> function)
{
return ArrayListIterate.groupBy(this.delegate, function);
}
@Override
public <V> FastListMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function)
{
return ArrayListIterate.groupByEach(this.delegate, function);
}
@Override
public <P> ArrayListAdapter<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return this.wrap(ArrayListIterate.selectWith(this.delegate, predicate, parameter));
}
@Override
public <P> ArrayListAdapter<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return this.wrap(ArrayListIterate.rejectWith(this.delegate, predicate, parameter));
}
@Override
public <P, A> ArrayListAdapter<A> collectWith(Function2<? super T, ? super P, ? extends A> function, P parameter)
{
return this.wrap(ArrayListIterate.collectWith(this.delegate, function, parameter));
}
@Override
public ArrayListAdapter<T> distinct()
{
return this.wrap(ArrayListIterate.distinct(this.delegate));
}
@Override
public ArrayListAdapter<T> distinct(HashingStrategy<? super T> hashingStrategy)
{
return this.wrap(ArrayListIterate.distinct(this.delegate, hashingStrategy));
}
/**
* @since 9.0
*/
@Override
public <V> ArrayListAdapter<T> distinctBy(Function<? super T, ? extends V> function)
{
return this.wrap(ArrayListIterate.distinctBy(this.delegate, function));
}
@Override
public <S> MutableList<Pair<T, S>> zip(Iterable<S> that)
{
return ArrayListIterate.zip(this.delegate, that);
}
@Override
public MutableList<Pair<T, Integer>> zipWithIndex()
{
return ArrayListIterate.zipWithIndex(this.delegate);
}
@Override
public MutableList<T> take(int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
return ArrayListIterate.take(this.delegate, count, FastList.newList(Math.min(this.size(), count)));
}
@Override
public MutableList<T> takeWhile(Predicate<? super T> predicate)
{
return ArrayListIterate.takeWhile(this.delegate, predicate);
}
@Override
public MutableList<T> drop(int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
return ArrayListIterate.drop(this.delegate, count, FastList.newList(this.size() - Math.min(this.size(), count)));
}
@Override
public MutableList<T> dropWhile(Predicate<? super T> predicate)
{
return ArrayListIterate.dropWhile(this.delegate, predicate);
}
@Override
public PartitionMutableList<T> partitionWhile(Predicate<? super T> predicate)
{
return ArrayListIterate.partitionWhile(this.delegate, predicate);
}
@Override
public ReverseIterable<T> asReversed()
{
return ReverseIterable.adapt(this);
}
}