2
* Copyright 2014 Goldman Sachs.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.gs.collections.impl.collection.mutable;
19
import java.util.Collection;
20
import java.util.Comparator;
21
import java.util.Iterator;
23
import com.gs.collections.api.LazyIterable;
24
import com.gs.collections.api.RichIterable;
25
import com.gs.collections.api.bag.MutableBag;
26
import com.gs.collections.api.block.function.Function;
27
import com.gs.collections.api.block.function.Function0;
28
import com.gs.collections.api.block.function.Function2;
29
import com.gs.collections.api.block.function.Function3;
30
import com.gs.collections.api.block.function.primitive.BooleanFunction;
31
import com.gs.collections.api.block.function.primitive.ByteFunction;
32
import com.gs.collections.api.block.function.primitive.CharFunction;
33
import com.gs.collections.api.block.function.primitive.DoubleFunction;
34
import com.gs.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
35
import com.gs.collections.api.block.function.primitive.FloatFunction;
36
import com.gs.collections.api.block.function.primitive.FloatObjectToFloatFunction;
37
import com.gs.collections.api.block.function.primitive.IntFunction;
38
import com.gs.collections.api.block.function.primitive.IntObjectToIntFunction;
39
import com.gs.collections.api.block.function.primitive.LongFunction;
40
import com.gs.collections.api.block.function.primitive.LongObjectToLongFunction;
41
import com.gs.collections.api.block.function.primitive.ShortFunction;
42
import com.gs.collections.api.block.predicate.Predicate;
43
import com.gs.collections.api.block.predicate.Predicate2;
44
import com.gs.collections.api.block.procedure.Procedure;
45
import com.gs.collections.api.block.procedure.Procedure2;
46
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
47
import com.gs.collections.api.collection.ImmutableCollection;
48
import com.gs.collections.api.collection.MutableCollection;
49
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
50
import com.gs.collections.api.collection.primitive.MutableByteCollection;
51
import com.gs.collections.api.collection.primitive.MutableCharCollection;
52
import com.gs.collections.api.collection.primitive.MutableDoubleCollection;
53
import com.gs.collections.api.collection.primitive.MutableFloatCollection;
54
import com.gs.collections.api.collection.primitive.MutableIntCollection;
55
import com.gs.collections.api.collection.primitive.MutableLongCollection;
56
import com.gs.collections.api.collection.primitive.MutableShortCollection;
57
import com.gs.collections.api.list.MutableList;
58
import com.gs.collections.api.map.MutableMap;
59
import com.gs.collections.api.map.sorted.MutableSortedMap;
60
import com.gs.collections.api.multimap.MutableMultimap;
61
import com.gs.collections.api.partition.PartitionMutableCollection;
62
import com.gs.collections.api.set.MutableSet;
63
import com.gs.collections.api.set.sorted.MutableSortedSet;
64
import com.gs.collections.api.tuple.Pair;
65
import com.gs.collections.api.tuple.Twin;
66
import com.gs.collections.impl.UnmodifiableIteratorAdapter;
67
import com.gs.collections.impl.block.factory.Comparators;
68
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
69
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
70
import com.gs.collections.impl.map.mutable.UnifiedMap;
71
import com.gs.collections.impl.utility.LazyIterate;
73
public class AbstractUnmodifiableMutableCollection<T> implements MutableCollection<T>
75
private final MutableCollection<? extends T> collection;
77
protected AbstractUnmodifiableMutableCollection(MutableCollection<? extends T> mutableCollection)
79
if (mutableCollection == null)
81
throw new NullPointerException("cannot create a UnmodifiableMutableCollection for null");
83
this.collection = mutableCollection;
88
return this.collection.size();
91
public boolean isEmpty()
93
return this.collection.isEmpty();
96
public boolean contains(Object o)
98
return this.collection.contains(o);
101
public Iterator<T> iterator()
103
return new UnmodifiableIteratorAdapter<T>(this.collection.iterator());
106
public Object[] toArray()
108
return this.collection.toArray();
111
public <S> S[] toArray(S[] a)
113
return this.collection.toArray(a);
116
public boolean add(T o)
118
throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName());
121
public boolean remove(Object o)
123
throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName());
126
public boolean containsAll(Collection<?> c)
128
return this.collection.containsAll(c);
131
public boolean addAll(Collection<? extends T> c)
133
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
136
public boolean retainAll(Collection<?> c)
138
throw new UnsupportedOperationException("Cannot call retainAll() on " + this.getClass().getSimpleName());
141
public boolean removeAll(Collection<?> c)
143
throw new UnsupportedOperationException("Cannot call removeAll() on " + this.getClass().getSimpleName());
148
throw new UnsupportedOperationException("Cannot call clear() on " + this.getClass().getSimpleName());
151
protected MutableCollection<T> getMutableCollection()
153
return (MutableCollection<T>) this.collection;
156
public boolean addAllIterable(Iterable<? extends T> iterable)
158
throw new UnsupportedOperationException("Cannot call addAllIterable() on " + this.getClass().getSimpleName());
161
public boolean removeAllIterable(Iterable<?> iterable)
163
throw new UnsupportedOperationException("Cannot call removeAllIterable() on " + this.getClass().getSimpleName());
166
public boolean retainAllIterable(Iterable<?> iterable)
168
throw new UnsupportedOperationException("Cannot call retainAllIterable() on " + this.getClass().getSimpleName());
171
public MutableCollection<T> asUnmodifiable()
176
public MutableCollection<T> asSynchronized()
178
return SynchronizedMutableCollection.of(this);
181
public ImmutableCollection<T> toImmutable()
183
return this.getMutableCollection().toImmutable();
186
public LazyIterable<T> asLazy()
188
return LazyIterate.adapt(this);
191
public void forEach(Procedure<? super T> procedure)
193
this.getMutableCollection().forEach(procedure);
196
public void forEachWithIndex(ObjectIntProcedure<? super T> objectIntProcedure)
198
this.getMutableCollection().forEachWithIndex(objectIntProcedure);
201
public <P> void forEachWith(Procedure2<? super T, ? super P> procedure, P parameter)
203
this.getMutableCollection().forEachWith(procedure, parameter);
206
public boolean containsAllIterable(Iterable<?> source)
208
return this.getMutableCollection().containsAllIterable(source);
211
public boolean containsAllArguments(Object... elements)
213
return this.getMutableCollection().containsAllArguments(elements);
216
public boolean notEmpty()
218
return this.getMutableCollection().notEmpty();
221
public MutableCollection<T> newEmpty()
223
return this.getMutableCollection().newEmpty();
228
return this.getMutableCollection().getFirst();
233
return this.getMutableCollection().getLast();
236
public MutableCollection<T> select(Predicate<? super T> predicate)
238
return this.getMutableCollection().select(predicate);
241
public <R extends Collection<T>> R select(Predicate<? super T> predicate, R target)
243
return this.getMutableCollection().select(predicate, target);
246
public <P> MutableCollection<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter)
248
return this.getMutableCollection().selectWith(predicate, parameter);
251
public <P, R extends Collection<T>> R selectWith(
252
Predicate2<? super T, ? super P> predicate,
256
return this.getMutableCollection().selectWith(predicate, parameter, targetCollection);
259
public MutableCollection<T> reject(Predicate<? super T> predicate)
261
return this.getMutableCollection().reject(predicate);
264
public <R extends Collection<T>> R reject(Predicate<? super T> predicate, R target)
266
return this.getMutableCollection().reject(predicate, target);
269
public <P> MutableCollection<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter)
271
return this.getMutableCollection().rejectWith(predicate, parameter);
274
public <P, R extends Collection<T>> R rejectWith(
275
Predicate2<? super T, ? super P> predicate,
279
return this.getMutableCollection().rejectWith(predicate, parameter, targetCollection);
282
public <P> Twin<MutableList<T>> selectAndRejectWith(
283
Predicate2<? super T, ? super P> predicate,
286
return this.getMutableCollection().selectAndRejectWith(predicate, parameter);
289
public PartitionMutableCollection<T> partition(Predicate<? super T> predicate)
291
return this.getMutableCollection().partition(predicate);
294
public <P> PartitionMutableCollection<T> partitionWith(Predicate2<? super T, ? super P> predicate, P parameter)
296
return this.getMutableCollection().partitionWith(predicate, parameter);
299
public <S> MutableCollection<S> selectInstancesOf(Class<S> clazz)
301
return this.getMutableCollection().selectInstancesOf(clazz);
304
public void removeIf(Predicate<? super T> predicate)
306
throw new UnsupportedOperationException("Cannot call removeIf() on " + this.getClass().getSimpleName());
309
public <P> void removeIfWith(Predicate2<? super T, ? super P> predicate, P parameter)
311
throw new UnsupportedOperationException("Cannot call removeIfWith() on " + this.getClass().getSimpleName());
314
public <V> MutableCollection<V> collect(Function<? super T, ? extends V> function)
316
return this.getMutableCollection().collect(function);
319
public MutableBooleanCollection collectBoolean(BooleanFunction<? super T> booleanFunction)
321
return this.getMutableCollection().collectBoolean(booleanFunction);
324
public <R extends MutableBooleanCollection> R collectBoolean(BooleanFunction<? super T> booleanFunction, R target)
326
return this.getMutableCollection().collectBoolean(booleanFunction, target);
329
public MutableByteCollection collectByte(ByteFunction<? super T> byteFunction)
331
return this.getMutableCollection().collectByte(byteFunction);
334
public <R extends MutableByteCollection> R collectByte(ByteFunction<? super T> byteFunction, R target)
336
return this.getMutableCollection().collectByte(byteFunction, target);
339
public MutableCharCollection collectChar(CharFunction<? super T> charFunction)
341
return this.getMutableCollection().collectChar(charFunction);
344
public <R extends MutableCharCollection> R collectChar(CharFunction<? super T> charFunction, R target)
346
return this.getMutableCollection().collectChar(charFunction, target);
349
public MutableDoubleCollection collectDouble(DoubleFunction<? super T> doubleFunction)
351
return this.getMutableCollection().collectDouble(doubleFunction);
354
public <R extends MutableDoubleCollection> R collectDouble(DoubleFunction<? super T> doubleFunction, R target)
356
return this.getMutableCollection().collectDouble(doubleFunction, target);
359
public MutableFloatCollection collectFloat(FloatFunction<? super T> floatFunction)
361
return this.getMutableCollection().collectFloat(floatFunction);
364
public <R extends MutableFloatCollection> R collectFloat(FloatFunction<? super T> floatFunction, R target)
366
return this.getMutableCollection().collectFloat(floatFunction, target);
369
public MutableIntCollection collectInt(IntFunction<? super T> intFunction)
371
return this.getMutableCollection().collectInt(intFunction);
374
public <R extends MutableIntCollection> R collectInt(IntFunction<? super T> intFunction, R target)
376
return this.getMutableCollection().collectInt(intFunction, target);
379
public MutableLongCollection collectLong(LongFunction<? super T> longFunction)
381
return this.getMutableCollection().collectLong(longFunction);
384
public <R extends MutableLongCollection> R collectLong(LongFunction<? super T> longFunction, R target)
386
return this.getMutableCollection().collectLong(longFunction, target);
389
public MutableShortCollection collectShort(ShortFunction<? super T> shortFunction)
391
return this.getMutableCollection().collectShort(shortFunction);
394
public <R extends MutableShortCollection> R collectShort(ShortFunction<? super T> shortFunction, R target)
396
return this.getMutableCollection().collectShort(shortFunction, target);
399
public <V, R extends Collection<V>> R collect(Function<? super T, ? extends V> function, R target)
401
return this.getMutableCollection().collect(function, target);
404
public <V> MutableCollection<V> flatCollect(Function<? super T, ? extends Iterable<V>> function)
406
return this.getMutableCollection().flatCollect(function);
409
public <V, R extends Collection<V>> R flatCollect(Function<? super T, ? extends Iterable<V>> function, R target)
411
return this.getMutableCollection().flatCollect(function, target);
414
public <P, A> MutableCollection<A> collectWith(Function2<? super T, ? super P, ? extends A> function, P parameter)
416
return this.getMutableCollection().collectWith(function, parameter);
419
public <P, A, R extends Collection<A>> R collectWith(
420
Function2<? super T, ? super P, ? extends A> function,
424
return this.getMutableCollection().collectWith(function, parameter, targetCollection);
427
public <V> MutableCollection<V> collectIf(
428
Predicate<? super T> predicate,
429
Function<? super T, ? extends V> function)
431
return this.getMutableCollection().collectIf(predicate, function);
434
public <V, R extends Collection<V>> R collectIf(
435
Predicate<? super T> predicate,
436
Function<? super T, ? extends V> function,
439
return this.getMutableCollection().collectIf(predicate, function, target);
442
public T detect(Predicate<? super T> predicate)
444
return this.getMutableCollection().detect(predicate);
447
public T min(Comparator<? super T> comparator)
449
return this.getMutableCollection().min(comparator);
452
public T max(Comparator<? super T> comparator)
454
return this.getMutableCollection().max(comparator);
459
return this.getMutableCollection().min();
464
return this.getMutableCollection().max();
467
public <V extends Comparable<? super V>> T minBy(Function<? super T, ? extends V> function)
469
return this.getMutableCollection().minBy(function);
472
public <V extends Comparable<? super V>> T maxBy(Function<? super T, ? extends V> function)
474
return this.getMutableCollection().maxBy(function);
477
public T detectIfNone(Predicate<? super T> predicate, Function0<? extends T> function)
479
return this.getMutableCollection().detectIfNone(predicate, function);
482
public <P> T detectWith(Predicate2<? super T, ? super P> predicate, P parameter)
484
return this.getMutableCollection().detectWith(predicate, parameter);
487
public <P> T detectWithIfNone(
488
Predicate2<? super T, ? super P> predicate,
490
Function0<? extends T> function)
492
return this.getMutableCollection().detectWithIfNone(predicate, parameter, function);
495
public int count(Predicate<? super T> predicate)
497
return this.getMutableCollection().count(predicate);
500
public <P> int countWith(Predicate2<? super T, ? super P> predicate, P parameter)
502
return this.getMutableCollection().countWith(predicate, parameter);
505
public boolean anySatisfy(Predicate<? super T> predicate)
507
return this.getMutableCollection().anySatisfy(predicate);
510
public <P> boolean anySatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
512
return this.getMutableCollection().anySatisfyWith(predicate, parameter);
515
public boolean allSatisfy(Predicate<? super T> predicate)
517
return this.getMutableCollection().allSatisfy(predicate);
520
public <P> boolean allSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
522
return this.getMutableCollection().allSatisfyWith(predicate, parameter);
525
public boolean noneSatisfy(Predicate<? super T> predicate)
527
return this.getMutableCollection().noneSatisfy(predicate);
530
public <P> boolean noneSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
532
return this.getMutableCollection().noneSatisfyWith(predicate, parameter);
535
public <IV> IV injectInto(IV injectedValue, Function2<? super IV, ? super T, ? extends IV> function)
537
return this.getMutableCollection().injectInto(injectedValue, function);
540
public int injectInto(int injectedValue, IntObjectToIntFunction<? super T> function)
542
return this.getMutableCollection().injectInto(injectedValue, function);
545
public long injectInto(long injectedValue, LongObjectToLongFunction<? super T> function)
547
return this.getMutableCollection().injectInto(injectedValue, function);
550
public double injectInto(double injectedValue, DoubleObjectToDoubleFunction<? super T> function)
552
return this.getMutableCollection().injectInto(injectedValue, function);
555
public float injectInto(float injectedValue, FloatObjectToFloatFunction<? super T> function)
557
return this.getMutableCollection().injectInto(injectedValue, function);
560
public long sumOfInt(IntFunction<? super T> function)
562
return this.getMutableCollection().sumOfInt(function);
565
public double sumOfFloat(FloatFunction<? super T> function)
567
return this.getMutableCollection().sumOfFloat(function);
570
public long sumOfLong(LongFunction<? super T> function)
572
return this.getMutableCollection().sumOfLong(function);
575
public double sumOfDouble(DoubleFunction<? super T> function)
577
return this.getMutableCollection().sumOfDouble(function);
580
public <IV, P> IV injectIntoWith(
582
Function3<? super IV, ? super T, ? super P, ? extends IV> function,
585
return this.getMutableCollection().injectIntoWith(injectValue, function, parameter);
588
public MutableList<T> toList()
590
return this.getMutableCollection().toList();
593
public MutableList<T> toSortedList()
595
return this.getMutableCollection().toSortedList();
598
public MutableList<T> toSortedList(Comparator<? super T> comparator)
600
return this.getMutableCollection().toSortedList(comparator);
603
public <V extends Comparable<? super V>> MutableList<T> toSortedListBy(Function<? super T, ? extends V> function)
605
return this.getMutableCollection().toSortedList(Comparators.byFunction(function));
608
public MutableSortedSet<T> toSortedSet()
610
return this.getMutableCollection().toSortedSet();
613
public MutableSortedSet<T> toSortedSet(Comparator<? super T> comparator)
615
return this.getMutableCollection().toSortedSet(comparator);
618
public <V extends Comparable<? super V>> MutableSortedSet<T> toSortedSetBy(Function<? super T, ? extends V> function)
620
return this.getMutableCollection().toSortedSetBy(function);
623
public MutableSet<T> toSet()
625
return this.getMutableCollection().toSet();
628
public MutableBag<T> toBag()
630
return this.getMutableCollection().toBag();
633
public <NK, NV> MutableMap<NK, NV> toMap(
634
Function<? super T, ? extends NK> keyFunction,
635
Function<? super T, ? extends NV> valueFunction)
637
return this.getMutableCollection().toMap(keyFunction, valueFunction);
640
public <NK, NV> MutableSortedMap<NK, NV> toSortedMap(
641
Function<? super T, ? extends NK> keyFunction,
642
Function<? super T, ? extends NV> valueFunction)
644
return this.getMutableCollection().toSortedMap(keyFunction, valueFunction);
647
public <NK, NV> MutableSortedMap<NK, NV> toSortedMap(Comparator<? super NK> comparator,
648
Function<? super T, ? extends NK> keyFunction,
649
Function<? super T, ? extends NV> valueFunction)
651
return this.getMutableCollection().toSortedMap(comparator, keyFunction, valueFunction);
655
public String toString()
657
return this.getMutableCollection().toString();
660
public String makeString()
662
return this.getMutableCollection().makeString();
665
public String makeString(String separator)
667
return this.getMutableCollection().makeString(separator);
670
public String makeString(String start, String separator, String end)
672
return this.getMutableCollection().makeString(start, separator, end);
675
public void appendString(Appendable appendable)
677
this.getMutableCollection().appendString(appendable);
680
public void appendString(Appendable appendable, String separator)
682
this.getMutableCollection().appendString(appendable, separator);
685
public void appendString(Appendable appendable, String start, String separator, String end)
687
this.getMutableCollection().appendString(appendable, start, separator, end);
690
public <V> MutableMultimap<V, T> groupBy(Function<? super T, ? extends V> function)
692
return this.getMutableCollection().groupBy(function);
695
public <V, R extends MutableMultimap<V, T>> R groupBy(
696
Function<? super T, ? extends V> function,
699
return this.getMutableCollection().groupBy(function, target);
702
public <V> MutableMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function)
704
return this.getMutableCollection().groupByEach(function);
707
public <V, R extends MutableMultimap<V, T>> R groupByEach(
708
Function<? super T, ? extends Iterable<V>> function,
711
return this.getMutableCollection().groupByEach(function, target);
714
public <V> MutableMap<V, T> groupByUniqueKey(Function<? super T, ? extends V> function)
716
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".groupByUniqueKey() not implemented yet");
719
public <S> MutableCollection<Pair<T, S>> zip(Iterable<S> that)
721
return this.getMutableCollection().zip(that);
724
public <S, R extends Collection<Pair<T, S>>> R zip(Iterable<S> that, R target)
726
return this.getMutableCollection().zip(that, target);
729
public MutableCollection<Pair<T, Integer>> zipWithIndex()
731
return this.getMutableCollection().zipWithIndex();
734
public <R extends Collection<Pair<T, Integer>>> R zipWithIndex(R target)
736
return this.getMutableCollection().zipWithIndex(target);
739
public RichIterable<RichIterable<T>> chunk(int size)
741
return this.getMutableCollection().chunk(size);
744
public MutableCollection<T> with(T element)
746
throw new UnsupportedOperationException("Cannot call with() on " + this.getClass().getSimpleName());
749
public MutableCollection<T> without(T element)
751
throw new UnsupportedOperationException("Cannot call without() on " + this.getClass().getSimpleName());
754
public MutableCollection<T> withAll(Iterable<? extends T> elements)
756
throw new UnsupportedOperationException("Cannot call withAll() on " + this.getClass().getSimpleName());
759
public MutableCollection<T> withoutAll(Iterable<? extends T> elements)
761
throw new UnsupportedOperationException("Cannot call withoutAll() on " + this.getClass().getSimpleName());
764
public <K, V> MutableMap<K, V> aggregateInPlaceBy(
765
Function<? super T, ? extends K> groupBy,
766
Function0<? extends V> zeroValueFactory,
767
Procedure2<? super V, ? super T> mutatingAggregator)
769
MutableMap<K, V> map = UnifiedMap.newMap();
770
this.forEach(new MutatingAggregationProcedure<T, K, V>(map, groupBy, zeroValueFactory, mutatingAggregator));
774
public <K, V> MutableMap<K, V> aggregateBy(
775
Function<? super T, ? extends K> groupBy,
776
Function0<? extends V> zeroValueFactory,
777
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
779
MutableMap<K, V> map = UnifiedMap.newMap();
780
this.forEach(new NonMutatingAggregationProcedure<T, K, V>(map, groupBy, zeroValueFactory, nonMutatingAggregator));