-
Notifications
You must be signed in to change notification settings - Fork 587
/
ImmutableEmptyBag.java
786 lines (676 loc) · 20.4 KB
/
ImmutableEmptyBag.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
* 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.bag.immutable;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.MutableBagIterable;
import org.eclipse.collections.api.bag.primitive.ImmutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.ImmutableByteBag;
import org.eclipse.collections.api.bag.primitive.ImmutableCharBag;
import org.eclipse.collections.api.bag.primitive.ImmutableDoubleBag;
import org.eclipse.collections.api.bag.primitive.ImmutableFloatBag;
import org.eclipse.collections.api.bag.primitive.ImmutableIntBag;
import org.eclipse.collections.api.bag.primitive.ImmutableLongBag;
import org.eclipse.collections.api.bag.primitive.ImmutableShortBag;
import org.eclipse.collections.api.bag.sorted.MutableSortedBag;
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.ObjectIntToObjectFunction;
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.predicate.primitive.IntPredicate;
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.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.multimap.bag.ImmutableBagMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.bag.PartitionImmutableBag;
import org.eclipse.collections.api.set.ImmutableSet;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.EmptyIterator;
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.bag.sorted.mutable.TreeBag;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Maps;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.factory.primitive.BooleanBags;
import org.eclipse.collections.impl.factory.primitive.ByteBags;
import org.eclipse.collections.impl.factory.primitive.CharBags;
import org.eclipse.collections.impl.factory.primitive.DoubleBags;
import org.eclipse.collections.impl.factory.primitive.FloatBags;
import org.eclipse.collections.impl.factory.primitive.IntBags;
import org.eclipse.collections.impl.factory.primitive.LongBags;
import org.eclipse.collections.impl.factory.primitive.ShortBags;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap;
import org.eclipse.collections.impl.multimap.bag.HashBagMultimap;
import org.eclipse.collections.impl.partition.bag.PartitionHashBag;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.LazyIterate;
/**
* This is a zero element {@link ImmutableBag} which is created by calling the Bags.immutable.empty().
*
* @since 1.0
*/
final class ImmutableEmptyBag<T>
extends AbstractImmutableBag<T>
implements Serializable
{
static final ImmutableBag<?> INSTANCE = new ImmutableEmptyBag<>();
private static final long serialVersionUID = 1L;
private static final LazyIterable<?> LAZY_ITERABLE = LazyIterate.adapt(INSTANCE);
private static final Object[] TO_ARRAY = new Object[0];
private static final PartitionImmutableBag<Object> IMMUTABLE_EMPTY_PARTITION = new PartitionHashBag<>().toImmutable();
@Override
public boolean equals(Object obj)
{
if (obj == this)
{
return true;
}
return obj instanceof Bag && ((Bag<?>) obj).isEmpty();
}
@Override
public int sizeDistinct()
{
return 0;
}
@Override
public int occurrencesOf(Object item)
{
return 0;
}
@Override
public void forEachWithOccurrences(ObjectIntProcedure<? super T> objectIntProcedure)
{
}
@Override
public <V> ImmutableBag<V> collectWithOccurrences(ObjectIntToObjectFunction<? super T, ? extends V> function)
{
return Bags.immutable.empty();
}
/**
* @since 9.1.
*/
@Override
public <V, R extends Collection<V>> R collectWithOccurrences(ObjectIntToObjectFunction<? super T, ? extends V> function, R target)
{
return target;
}
@Override
public MutableMap<T, Integer> toMapOfItemToCount()
{
return Maps.mutable.empty();
}
@Override
public int hashCode()
{
return 0;
}
@Override
public int size()
{
return 0;
}
@Override
public boolean isEmpty()
{
return true;
}
@Override
public boolean notEmpty()
{
return false;
}
@Override
public boolean contains(Object object)
{
return false;
}
@Override
public boolean containsAllIterable(Iterable<?> source)
{
return Iterate.isEmpty(source);
}
@Override
public boolean containsAllArguments(Object... elements)
{
return ArrayIterate.isEmpty(elements);
}
@Override
public ImmutableBag<T> tap(Procedure<? super T> procedure)
{
return this;
}
@Override
public void each(Procedure<? super T> procedure)
{
}
@Override
public void forEachWithIndex(ObjectIntProcedure<? super T> objectIntProcedure)
{
}
@Override
public <P> void forEachWith(Procedure2<? super T, ? super P> procedure, P parameter)
{
}
@Override
public T getFirst()
{
return null;
}
@Override
public T getLast()
{
return null;
}
@Override
public T getOnly()
{
throw new IllegalStateException("Size must be 1 but was " + this.size());
}
@Override
public Iterator<T> iterator()
{
return EmptyIterator.getInstance();
}
@Override
public ImmutableBag<T> newWith(T element)
{
return Bags.immutable.with(element);
}
@Override
public ImmutableBag<T> newWithout(T element)
{
return this;
}
@Override
public ImmutableBag<T> newWithAll(Iterable<? extends T> elements)
{
MutableBag<T> bag = HashBag.newBag(elements);
return bag.toImmutable();
}
@Override
public ImmutableBag<T> selectByOccurrences(IntPredicate predicate)
{
return this;
}
@Override
public ImmutableBag<T> select(Predicate<? super T> predicate)
{
return this;
}
@Override
public <P> ImmutableBag<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return this;
}
@Override
public ImmutableBag<T> reject(Predicate<? super T> predicate)
{
return this;
}
@Override
public <P> ImmutableBag<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return this;
}
@Override
public PartitionImmutableBag<T> partition(Predicate<? super T> predicate)
{
return (PartitionImmutableBag<T>) IMMUTABLE_EMPTY_PARTITION;
}
@Override
public <P> PartitionImmutableBag<T> partitionWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return (PartitionImmutableBag<T>) IMMUTABLE_EMPTY_PARTITION;
}
@Override
public <S> ImmutableBag<S> selectInstancesOf(Class<S> clazz)
{
return (ImmutableBag<S>) INSTANCE;
}
@Override
public <V> ImmutableBag<V> collect(Function<? super T, ? extends V> function)
{
return (ImmutableBag<V>) INSTANCE;
}
@Override
public ImmutableBooleanBag collectBoolean(BooleanFunction<? super T> booleanFunction)
{
return BooleanBags.immutable.empty();
}
@Override
public ImmutableByteBag collectByte(ByteFunction<? super T> byteFunction)
{
return ByteBags.immutable.empty();
}
@Override
public ImmutableCharBag collectChar(CharFunction<? super T> charFunction)
{
return CharBags.immutable.empty();
}
@Override
public ImmutableDoubleBag collectDouble(DoubleFunction<? super T> doubleFunction)
{
return DoubleBags.immutable.empty();
}
@Override
public ImmutableFloatBag collectFloat(FloatFunction<? super T> floatFunction)
{
return FloatBags.immutable.empty();
}
@Override
public ImmutableIntBag collectInt(IntFunction<? super T> intFunction)
{
return IntBags.immutable.empty();
}
@Override
public ImmutableLongBag collectLong(LongFunction<? super T> longFunction)
{
return LongBags.immutable.empty();
}
@Override
public ImmutableShortBag collectShort(ShortFunction<? super T> shortFunction)
{
return ShortBags.immutable.empty();
}
@Override
public <P, V> ImmutableBag<V> collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter)
{
return (ImmutableBag<V>) INSTANCE;
}
@Override
public <V> ImmutableBag<V> collectIf(
Predicate<? super T> predicate,
Function<? super T, ? extends V> function)
{
return (ImmutableBag<V>) INSTANCE;
}
@Override
public <V> ImmutableBag<V> flatCollect(Function<? super T, ? extends Iterable<V>> function)
{
return (ImmutableBag<V>) INSTANCE;
}
/**
* @since 9.0
*/
@Override
public <V> ImmutableBag<V> countBy(Function<? super T, ? extends V> function)
{
return Bags.immutable.empty();
}
/**
* @since 9.0
*/
@Override
public <V, R extends MutableBagIterable<V>> R countBy(Function<? super T, ? extends V> function, R target)
{
return target;
}
/**
* @since 9.0
*/
@Override
public <V, P> ImmutableBag<V> countByWith(Function2<? super T, ? super P, ? extends V> function, P parameter)
{
return Bags.immutable.empty();
}
/**
* @since 9.0
*/
@Override
public <V, P, R extends MutableBagIterable<V>> R countByWith(Function2<? super T, ? super P, ? extends V> function, P parameter, R target)
{
return target;
}
/**
* @since 10.0.0
*/
@Override
public <V> ImmutableBag<V> countByEach(Function<? super T, ? extends Iterable<V>> function)
{
return Bags.immutable.empty();
}
/**
* @since 10.0.0
*/
@Override
public <V, R extends MutableBagIterable<V>> R countByEach(Function<? super T, ? extends Iterable<V>> function, R target)
{
return target;
}
@Override
public <V> ImmutableBagMultimap<V, T> groupBy(Function<? super T, ? extends V> function)
{
// TODO: Create a Singleton ImmutableEmptyBagMultimap for efficiency
return HashBagMultimap.<V, T>newMultimap().toImmutable();
}
@Override
public <V> ImmutableBagMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function)
{
// TODO: Create a Singleton ImmutableEmptyBagMultimap for efficiency
return HashBagMultimap.<V, T>newMultimap().toImmutable();
}
@Override
public <V> ImmutableMap<V, T> groupByUniqueKey(Function<? super T, ? extends V> function)
{
return Maps.immutable.empty();
}
@Override
public T detect(Predicate<? super T> predicate)
{
return null;
}
@Override
public <P> T detectWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return null;
}
@Override
public Optional<T> detectOptional(Predicate<? super T> predicate)
{
return Optional.empty();
}
@Override
public <P> Optional<T> detectWithOptional(Predicate2<? super T, ? super P> predicate, P parameter)
{
return Optional.empty();
}
@Override
public T detectIfNone(Predicate<? super T> predicate, Function0<? extends T> function)
{
return function.value();
}
@Override
public <P> T detectWithIfNone(Predicate2<? super T, ? super P> predicate, P parameter, Function0<? extends T> function)
{
return function.value();
}
@Override
public int count(Predicate<? super T> predicate)
{
return 0;
}
@Override
public <P> int countWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return 0;
}
@Override
public boolean anySatisfy(Predicate<? super T> predicate)
{
return false;
}
@Override
public boolean allSatisfy(Predicate<? super T> predicate)
{
return true;
}
@Override
public boolean noneSatisfy(Predicate<? super T> predicate)
{
return true;
}
@Override
public <P> boolean anySatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return false;
}
@Override
public <P> boolean allSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return true;
}
@Override
public <P> boolean noneSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return true;
}
@Override
public <IV> IV injectInto(IV injectedValue, Function2<? super IV, ? super T, ? extends IV> function)
{
return injectedValue;
}
@Override
public MutableList<T> toList()
{
return Lists.mutable.empty();
}
@Override
public MutableList<T> toSortedList()
{
return Lists.mutable.empty();
}
@Override
public MutableList<T> toSortedList(Comparator<? super T> comparator)
{
return Lists.mutable.empty();
}
@Override
public <V extends Comparable<? super V>> MutableList<T> toSortedListBy(Function<? super T, ? extends V> function)
{
return Lists.mutable.empty();
}
@Override
public MutableSet<T> toSet()
{
return Sets.mutable.empty();
}
@Override
public MutableBag<T> toBag()
{
return Bags.mutable.empty();
}
@Override
public MutableSortedBag<T> toSortedBag()
{
return TreeBag.newBag();
}
@Override
public MutableSortedBag<T> toSortedBag(Comparator<? super T> comparator)
{
return TreeBag.newBag(comparator);
}
@Override
public <V extends Comparable<? super V>> MutableSortedBag<T> toSortedBagBy(Function<? super T, ? extends V> function)
{
return TreeBag.newBag(Comparators.byFunction(function));
}
@Override
public <NK, NV> MutableMap<NK, NV> toMap(
Function<? super T, ? extends NK> keyFunction,
Function<? super T, ? extends NV> valueFunction)
{
return UnifiedMap.newMap();
}
@Override
public <NK, NV, R extends Map<NK, NV>> R toMap(
Function<? super T, ? extends NK> keyFunction,
Function<? super T, ? extends NV> valueFunction,
R target)
{
return target;
}
@Override
public <NK, NV> MutableSortedMap<NK, NV> toSortedMap(
Function<? super T, ? extends NK> keyFunction,
Function<? super T, ? extends NV> valueFunction)
{
return TreeSortedMap.newMap();
}
@Override
public <NK, NV> MutableSortedMap<NK, NV> toSortedMap(
Comparator<? super NK> comparator,
Function<? super T, ? extends NK> keyFunction,
Function<? super T, ? extends NV> valueFunction)
{
return TreeSortedMap.newMap(comparator);
}
@Override
public <KK extends Comparable<? super KK>, K, V> MutableSortedMap<K, V> toSortedMapBy(Function<? super K, KK> sortBy, Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction)
{
return TreeSortedMap.newMap(Comparators.byFunction(sortBy));
}
@Override
public LazyIterable<T> asLazy()
{
return (LazyIterable<T>) LAZY_ITERABLE;
}
@Override
public Object[] toArray()
{
return TO_ARRAY;
}
@Override
public <T> T[] toArray(T[] a)
{
if (a.length > 0)
{
a[0] = null;
}
return a;
}
@Override
public T min(Comparator<? super T> comparator)
{
throw new NoSuchElementException();
}
@Override
public T max(Comparator<? super T> comparator)
{
throw new NoSuchElementException();
}
@Override
public T min()
{
throw new NoSuchElementException();
}
@Override
public T max()
{
throw new NoSuchElementException();
}
@Override
public <V extends Comparable<? super V>> T minBy(Function<? super T, ? extends V> function)
{
throw new NoSuchElementException();
}
@Override
public <V extends Comparable<? super V>> T maxBy(Function<? super T, ? extends V> function)
{
throw new NoSuchElementException();
}
@Override
public String toString()
{
return "[]";
}
@Override
public String makeString()
{
return "";
}
@Override
public String makeString(String separator)
{
return "";
}
@Override
public String makeString(String start, String separator, String end)
{
return start + end;
}
@Override
public void appendString(Appendable appendable)
{
}
@Override
public void appendString(Appendable appendable, String separator)
{
}
@Override
public void appendString(Appendable appendable, String start, String separator, String end)
{
try
{
appendable.append(start);
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead.
*/
@Override
@Deprecated
public <S> ImmutableBag<Pair<T, S>> zip(Iterable<S> that)
{
return Bags.immutable.empty();
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead.
*/
@Override
@Deprecated
public ImmutableSet<Pair<T, Integer>> zipWithIndex()
{
return Sets.immutable.empty();
}
@Override
public RichIterable<RichIterable<T>> chunk(int size)
{
if (size <= 0)
{
throw new IllegalArgumentException("Size for groups must be positive but was: " + size);
}
return Bags.immutable.empty();
}
private Object writeReplace()
{
return new ImmutableBagSerializationProxy<>(this);
}
@Override
public ImmutableSet<T> selectUnique()
{
return Sets.immutable.empty();
}
}