/
ImmutableBooleanArrayList.java
673 lines (604 loc) · 17 KB
/
ImmutableBooleanArrayList.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
* 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.immutable.primitive;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.BitSet;
import java.util.NoSuchElementException;
import org.eclipse.collections.api.BooleanIterable;
import org.eclipse.collections.api.LazyBooleanIterable;
import org.eclipse.collections.api.bag.primitive.MutableBooleanBag;
import org.eclipse.collections.api.block.function.primitive.BooleanToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectBooleanIntToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectBooleanToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.BooleanPredicate;
import org.eclipse.collections.api.block.procedure.primitive.BooleanIntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.BooleanProcedure;
import org.eclipse.collections.api.iterator.BooleanIterator;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.primitive.BooleanList;
import org.eclipse.collections.api.list.primitive.ImmutableBooleanList;
import org.eclipse.collections.api.list.primitive.MutableBooleanList;
import org.eclipse.collections.api.set.primitive.MutableBooleanSet;
import org.eclipse.collections.impl.bag.mutable.primitive.BooleanHashBag;
import org.eclipse.collections.impl.factory.primitive.BooleanLists;
import org.eclipse.collections.impl.lazy.primitive.LazyBooleanIterableAdapter;
import org.eclipse.collections.impl.lazy.primitive.ReverseBooleanIterable;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.list.mutable.primitive.BooleanArrayList;
import org.eclipse.collections.impl.set.mutable.primitive.BooleanHashSet;
/**
* ImmutableBooleanArrayList is the non-modifiable equivalent of {@link BooleanArrayList}.
* It is backed by a {@link BitSet}.
*
* @since 3.2.
*/
final class ImmutableBooleanArrayList
implements ImmutableBooleanList, Serializable
{
private static final long serialVersionUID = 1L;
private final int size;
private final BitSet items;
private ImmutableBooleanArrayList(boolean[] newElements)
{
if (newElements.length <= 1)
{
throw new IllegalArgumentException("Use BooleanLists.immutable.with() to instantiate an optimized collection");
}
this.size = newElements.length;
this.items = new BitSet(newElements.length);
for (int i = 0; i < newElements.length; i++)
{
if (newElements[i])
{
this.items.set(i);
}
}
}
private ImmutableBooleanArrayList(BitSet newItems, int size)
{
this.size = size;
this.items = newItems;
}
public static ImmutableBooleanArrayList newList(BooleanIterable iterable)
{
return new ImmutableBooleanArrayList(iterable.toArray());
}
public static ImmutableBooleanArrayList newListWith(boolean... elements)
{
return new ImmutableBooleanArrayList(elements);
}
private IndexOutOfBoundsException newIndexOutOfBoundsException(int index)
{
return new IndexOutOfBoundsException("Index: " + index + " Size: " + this.size);
}
@Override
public boolean get(int index)
{
if (index < this.size)
{
return this.items.get(index);
}
throw this.newIndexOutOfBoundsException(index);
}
@Override
public boolean getFirst()
{
return this.items.get(0);
}
@Override
public boolean getLast()
{
return this.items.get(this.size - 1);
}
@Override
public int indexOf(boolean value)
{
for (int i = 0; i < this.size; i++)
{
if (this.items.get(i) == value)
{
return i;
}
}
return -1;
}
@Override
public int lastIndexOf(boolean value)
{
for (int i = this.size - 1; i >= 0; i--)
{
if (this.items.get(i) == value)
{
return i;
}
}
return -1;
}
@Override
public BooleanIterator booleanIterator()
{
return new InternalBooleanIterator();
}
@Override
public void forEach(BooleanProcedure procedure)
{
this.each(procedure);
}
/**
* @since 7.0.
*/
@Override
public void each(BooleanProcedure procedure)
{
for (int i = 0; i < this.size; i++)
{
procedure.value(this.items.get(i));
}
}
@Override
public void forEachWithIndex(BooleanIntProcedure procedure)
{
for (int i = 0; i < this.size; i++)
{
procedure.value(this.items.get(i), i);
}
}
@Override
public int count(BooleanPredicate predicate)
{
int count = 0;
for (int i = 0; i < this.size; i++)
{
if (predicate.accept(this.items.get(i)))
{
count++;
}
}
return count;
}
@Override
public boolean anySatisfy(BooleanPredicate predicate)
{
for (int i = 0; i < this.size; i++)
{
if (predicate.accept(this.items.get(i)))
{
return true;
}
}
return false;
}
@Override
public boolean allSatisfy(BooleanPredicate predicate)
{
for (int i = 0; i < this.size; i++)
{
if (!predicate.accept(this.items.get(i)))
{
return false;
}
}
return true;
}
@Override
public boolean noneSatisfy(BooleanPredicate predicate)
{
for (int i = 0; i < this.size; i++)
{
if (predicate.accept(this.items.get(i)))
{
return false;
}
}
return true;
}
@Override
public ImmutableBooleanList select(BooleanPredicate predicate)
{
MutableBooleanList result = BooleanLists.mutable.empty();
for (int i = 0; i < this.size; i++)
{
boolean item = this.items.get(i);
if (predicate.accept(item))
{
result.add(item);
}
}
return result.toImmutable();
}
@Override
public ImmutableBooleanList reject(BooleanPredicate predicate)
{
MutableBooleanList result = BooleanLists.mutable.empty();
for (int i = 0; i < this.size; i++)
{
boolean item = this.items.get(i);
if (!predicate.accept(item))
{
result.add(item);
}
}
return result.toImmutable();
}
@Override
public boolean detectIfNone(BooleanPredicate predicate, boolean ifNone)
{
for (int i = 0; i < this.size; i++)
{
boolean item = this.items.get(i);
if (predicate.accept(item))
{
return item;
}
}
return ifNone;
}
@Override
public <V> ImmutableList<V> collect(BooleanToObjectFunction<? extends V> function)
{
FastList<V> target = FastList.newList(this.size);
for (int i = 0; i < this.size; i++)
{
target.add(function.valueOf(this.items.get(i)));
}
return target.toImmutable();
}
@Override
public boolean[] toArray()
{
boolean[] newItems = new boolean[this.size];
for (int i = 0; i < this.size; i++)
{
newItems[i] = this.items.get(i);
}
return newItems;
}
@Override
public boolean contains(boolean value)
{
for (int i = 0; i < this.size; i++)
{
if (this.items.get(i) == value)
{
return true;
}
}
return false;
}
@Override
public boolean containsAll(boolean... source)
{
for (boolean value : source)
{
if (!this.contains(value))
{
return false;
}
}
return true;
}
@Override
public boolean containsAll(BooleanIterable source)
{
for (BooleanIterator iterator = source.booleanIterator(); iterator.hasNext(); )
{
if (!this.contains(iterator.next()))
{
return false;
}
}
return true;
}
@Override
public LazyBooleanIterable asReversed()
{
return ReverseBooleanIterable.adapt(this);
}
@Override
public MutableBooleanList toList()
{
return BooleanArrayList.newList(this);
}
@Override
public MutableBooleanSet toSet()
{
return BooleanHashSet.newSet(this);
}
@Override
public MutableBooleanBag toBag()
{
return BooleanHashBag.newBag(this);
}
@Override
public LazyBooleanIterable asLazy()
{
return new LazyBooleanIterableAdapter(this);
}
@Override
public ImmutableBooleanList toImmutable()
{
return this;
}
@Override
public ImmutableBooleanArrayList toReversed()
{
return ImmutableBooleanArrayList.newList(this.asReversed());
}
/**
* @since 6.0
*/
@Override
public ImmutableBooleanList distinct()
{
BooleanArrayList target = new BooleanArrayList();
MutableBooleanSet seenSoFar = new BooleanHashSet();
for (int i = 0; i < this.size; i++)
{
boolean each = this.get(i);
if (seenSoFar.add(each))
{
target.add(each);
}
}
return target.toImmutable();
}
@Override
public ImmutableBooleanList newWith(boolean element)
{
BitSet newItems = (BitSet) this.items.clone();
if (element)
{
newItems.set(this.size);
}
return new ImmutableBooleanArrayList(newItems, this.size + 1);
}
@Override
public ImmutableBooleanList newWithout(boolean element)
{
int index = this.indexOf(element);
if (index != -1)
{
boolean[] newItems = new boolean[this.size - 1];
for (int i = 0; i < index; i++)
{
newItems[i] = this.items.get(i);
}
for (int i = index + 1; i < this.size; i++)
{
newItems[i - 1] = this.items.get(i);
}
return BooleanLists.immutable.with(newItems);
}
return this;
}
@Override
public ImmutableBooleanList newWithAll(BooleanIterable elements)
{
BitSet newItems = (BitSet) this.items.clone();
int index = 0;
for (BooleanIterator booleanIterator = elements.booleanIterator(); booleanIterator.hasNext(); index++)
{
if (booleanIterator.next())
{
newItems.set(this.size + index);
}
}
return new ImmutableBooleanArrayList(newItems, this.size + elements.size());
}
@Override
public ImmutableBooleanList newWithoutAll(BooleanIterable elements)
{
MutableBooleanList list = this.toList();
list.removeAll(elements);
return list.toImmutable();
}
@Override
public int size()
{
return this.size;
}
@Override
public boolean isEmpty()
{
return false;
}
@Override
public boolean notEmpty()
{
return true;
}
@Override
public boolean equals(Object otherList)
{
if (otherList == this)
{
return true;
}
if (!(otherList instanceof BooleanList))
{
return false;
}
BooleanList list = (BooleanList) otherList;
if (this.size != list.size())
{
return false;
}
for (int i = 0; i < this.size; i++)
{
if (this.items.get(i) != list.get(i))
{
return false;
}
}
return true;
}
@Override
public int hashCode()
{
int hashCode = 1;
for (int i = 0; i < this.size; i++)
{
boolean item = this.items.get(i);
hashCode = 31 * hashCode + (item ? 1231 : 1237);
}
return hashCode;
}
@Override
public <T> T injectInto(T injectedValue, ObjectBooleanToObjectFunction<? super T, ? extends T> function)
{
T result = injectedValue;
for (int i = 0; i < this.size; i++)
{
result = function.valueOf(result, this.items.get(i));
}
return result;
}
@Override
public <T> T injectIntoWithIndex(T injectedValue, ObjectBooleanIntToObjectFunction<? super T, ? extends T> function)
{
T result = injectedValue;
for (int i = 0; i < this.size; i++)
{
result = function.valueOf(result, this.items.get(i), i);
}
return result;
}
@Override
public String toString()
{
return this.makeString("[", ", ", "]");
}
@Override
public String makeString()
{
return this.makeString(", ");
}
@Override
public String makeString(String separator)
{
return this.makeString("", separator, "");
}
@Override
public String makeString(String start, String separator, String end)
{
Appendable stringBuilder = new StringBuilder();
this.appendString(stringBuilder, start, separator, end);
return stringBuilder.toString();
}
@Override
public void appendString(Appendable appendable)
{
this.appendString(appendable, ", ");
}
@Override
public void appendString(Appendable appendable, String separator)
{
this.appendString(appendable, "", separator, "");
}
@Override
public void appendString(
Appendable appendable,
String start,
String separator,
String end)
{
try
{
appendable.append(start);
for (int i = 0; i < this.size; i++)
{
if (i > 0)
{
appendable.append(separator);
}
boolean value = this.items.get(i);
appendable.append(String.valueOf(value));
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Override
public ImmutableBooleanList subList(int fromIndex, int toIndex)
{
throw new UnsupportedOperationException("subList not yet implemented!");
}
private Object writeReplace()
{
return new ImmutableBooleanListSerializationProxy(this);
}
private static class ImmutableBooleanListSerializationProxy implements Externalizable
{
private static final long serialVersionUID = 1L;
private ImmutableBooleanList list;
public ImmutableBooleanListSerializationProxy()
{
// Empty constructor for Externalizable class
}
private ImmutableBooleanListSerializationProxy(ImmutableBooleanList list)
{
this.list = list;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException
{
out.writeInt(this.list.size());
for (int i = 0; i < this.list.size(); i++)
{
out.writeBoolean(this.list.get(i));
}
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
int inputSize = in.readInt();
BitSet newItems = new BitSet(inputSize);
for (int i = 0; i < inputSize; i++)
{
newItems.set(i, in.readBoolean());
}
this.list = new ImmutableBooleanArrayList(newItems, inputSize);
}
protected Object readResolve()
{
return this.list;
}
}
private class InternalBooleanIterator implements BooleanIterator
{
/**
* Index of element to be returned by subsequent call to next.
*/
private int currentIndex;
@Override
public boolean hasNext()
{
return this.currentIndex != ImmutableBooleanArrayList.this.size;
}
@Override
public boolean next()
{
if (!this.hasNext())
{
throw new NoSuchElementException();
}
boolean next = ImmutableBooleanArrayList.this.get(this.currentIndex);
this.currentIndex++;
return next;
}
}
}