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.MutableCollection;
48
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
49
import com.gs.collections.api.collection.primitive.MutableByteCollection;
50
import com.gs.collections.api.collection.primitive.MutableCharCollection;
51
import com.gs.collections.api.collection.primitive.MutableDoubleCollection;
52
import com.gs.collections.api.collection.primitive.MutableFloatCollection;
53
import com.gs.collections.api.collection.primitive.MutableIntCollection;
54
import com.gs.collections.api.collection.primitive.MutableLongCollection;
55
import com.gs.collections.api.collection.primitive.MutableShortCollection;
56
import com.gs.collections.api.list.MutableList;
57
import com.gs.collections.api.map.MutableMap;
58
import com.gs.collections.api.map.sorted.MutableSortedMap;
59
import com.gs.collections.api.multimap.MutableMultimap;
60
import com.gs.collections.api.partition.PartitionMutableCollection;
61
import com.gs.collections.api.set.MutableSet;
62
import com.gs.collections.api.set.sorted.MutableSortedSet;
63
import com.gs.collections.api.tuple.Pair;
64
import com.gs.collections.api.tuple.Twin;
65
import com.gs.collections.impl.bag.mutable.HashBag;
66
import com.gs.collections.impl.block.factory.Comparators;
67
import com.gs.collections.impl.block.factory.Predicates2;
68
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
69
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
70
import com.gs.collections.impl.factory.Lists;
71
import com.gs.collections.impl.map.mutable.UnifiedMap;
72
import com.gs.collections.impl.map.sorted.mutable.TreeSortedMap;
73
import com.gs.collections.impl.set.mutable.UnifiedSet;
74
import com.gs.collections.impl.set.sorted.mutable.TreeSortedSet;
75
import com.gs.collections.impl.utility.ArrayIterate;
76
import com.gs.collections.impl.utility.Iterate;
77
import com.gs.collections.impl.utility.LazyIterate;
78
import com.gs.collections.impl.utility.internal.IterableIterate;
79
import com.gs.collections.impl.utility.internal.MutableCollectionIterate;
81
public abstract class AbstractCollectionAdapter<T>
82
implements MutableCollection<T>
84
protected abstract Collection<T> getDelegate();
86
protected <E> MutableCollection<E> wrap(Collection<E> collection)
88
return CollectionAdapter.adapt(collection);
91
public boolean notEmpty()
93
return !this.getDelegate().isEmpty();
98
return Iterate.getFirst(this.getDelegate());
103
return Iterate.getLast(this.getDelegate());
106
public void forEach(Procedure<? super T> procedure)
108
Iterate.forEach(this.getDelegate(), procedure);
111
public void forEachWithIndex(ObjectIntProcedure<? super T> objectIntProcedure)
113
Iterate.forEachWithIndex(this.getDelegate(), objectIntProcedure);
116
public void removeIf(Predicate<? super T> predicate)
118
Iterate.removeIf(this.getDelegate(), predicate);
121
public <P> void removeIfWith(Predicate2<? super T, ? super P> predicate, P parameter)
123
Iterate.removeIfWith(this.getDelegate(), predicate, parameter);
126
public T detect(Predicate<? super T> predicate)
128
return Iterate.detect(this.getDelegate(), predicate);
131
public T min(Comparator<? super T> comparator)
133
return Iterate.min(this, comparator);
136
public T max(Comparator<? super T> comparator)
138
return Iterate.max(this, comparator);
143
return Iterate.min(this);
148
return Iterate.max(this);
151
public <V extends Comparable<? super V>> T minBy(Function<? super T, ? extends V> function)
153
return IterableIterate.minBy(this, function);
156
public <V extends Comparable<? super V>> T maxBy(Function<? super T, ? extends V> function)
158
return IterableIterate.maxBy(this, function);
161
public T detectIfNone(Predicate<? super T> predicate, Function0<? extends T> function)
163
T result = this.detect(predicate);
164
return result == null ? function.value() : result;
167
public int count(Predicate<? super T> predicate)
169
return Iterate.count(this.getDelegate(), predicate);
172
public boolean anySatisfy(Predicate<? super T> predicate)
174
return Iterate.anySatisfy(this.getDelegate(), predicate);
177
public boolean allSatisfy(Predicate<? super T> predicate)
179
return Iterate.allSatisfy(this.getDelegate(), predicate);
182
public boolean noneSatisfy(Predicate<? super T> predicate)
184
return Iterate.noneSatisfy(this.getDelegate(), predicate);
187
public <IV> IV injectInto(IV injectedValue, Function2<? super IV, ? super T, ? extends IV> function)
189
return Iterate.injectInto(injectedValue, this.getDelegate(), function);
192
public int injectInto(int injectedValue, IntObjectToIntFunction<? super T> function)
194
return Iterate.injectInto(injectedValue, this.getDelegate(), function);
197
public long injectInto(long injectedValue, LongObjectToLongFunction<? super T> function)
199
return Iterate.injectInto(injectedValue, this.getDelegate(), function);
202
public double injectInto(double injectedValue, DoubleObjectToDoubleFunction<? super T> function)
204
return Iterate.injectInto(injectedValue, this.getDelegate(), function);
207
public float injectInto(float injectedValue, FloatObjectToFloatFunction<? super T> function)
209
return Iterate.injectInto(injectedValue, this.getDelegate(), function);
212
public long sumOfInt(IntFunction<? super T> function)
214
return Iterate.sumOfInt(this.getDelegate(), function);
217
public double sumOfFloat(FloatFunction<? super T> function)
219
return Iterate.sumOfFloat(this.getDelegate(), function);
222
public long sumOfLong(LongFunction<? super T> function)
224
return Iterate.sumOfLong(this.getDelegate(), function);
227
public double sumOfDouble(DoubleFunction<? super T> function)
229
return Iterate.sumOfDouble(this.getDelegate(), function);
232
public MutableCollection<T> select(Predicate<? super T> predicate)
234
return this.wrap(Iterate.select(this.getDelegate(), predicate));
237
public <R extends Collection<T>> R select(Predicate<? super T> predicate, R target)
239
return Iterate.select(this.getDelegate(), predicate, target);
242
public MutableCollection<T> reject(Predicate<? super T> predicate)
244
return this.wrap(Iterate.reject(this.getDelegate(), predicate));
247
public <R extends Collection<T>> R reject(Predicate<? super T> predicate, R target)
249
return Iterate.reject(this.getDelegate(), predicate, target);
252
public <S> MutableCollection<S> selectInstancesOf(Class<S> clazz)
254
return this.wrap(Iterate.selectInstancesOf(this.getDelegate(), clazz));
257
public <V> MutableCollection<V> collect(Function<? super T, ? extends V> function)
259
return this.wrap(Iterate.collect(this.getDelegate(), function));
262
public MutableBooleanCollection collectBoolean(BooleanFunction<? super T> booleanFunction)
264
return Iterate.collectBoolean(this.getDelegate(), booleanFunction);
267
public <R extends MutableBooleanCollection> R collectBoolean(BooleanFunction<? super T> booleanFunction, R target)
269
return Iterate.collectBoolean(this.getDelegate(), booleanFunction, target);
272
public MutableByteCollection collectByte(ByteFunction<? super T> byteFunction)
274
return Iterate.collectByte(this.getDelegate(), byteFunction);
277
public <R extends MutableByteCollection> R collectByte(ByteFunction<? super T> byteFunction, R target)
279
return Iterate.collectByte(this.getDelegate(), byteFunction, target);
282
public MutableCharCollection collectChar(CharFunction<? super T> charFunction)
284
return Iterate.collectChar(this.getDelegate(), charFunction);
287
public <R extends MutableCharCollection> R collectChar(CharFunction<? super T> charFunction, R target)
289
return Iterate.collectChar(this.getDelegate(), charFunction, target);
292
public MutableDoubleCollection collectDouble(DoubleFunction<? super T> doubleFunction)
294
return Iterate.collectDouble(this.getDelegate(), doubleFunction);
297
public <R extends MutableDoubleCollection> R collectDouble(DoubleFunction<? super T> doubleFunction, R target)
299
return Iterate.collectDouble(this.getDelegate(), doubleFunction, target);
302
public MutableFloatCollection collectFloat(FloatFunction<? super T> floatFunction)
304
return Iterate.collectFloat(this.getDelegate(), floatFunction);
307
public <R extends MutableFloatCollection> R collectFloat(FloatFunction<? super T> floatFunction, R target)
309
return Iterate.collectFloat(this.getDelegate(), floatFunction, target);
312
public MutableIntCollection collectInt(IntFunction<? super T> intFunction)
314
return Iterate.collectInt(this.getDelegate(), intFunction);
317
public <R extends MutableIntCollection> R collectInt(IntFunction<? super T> intFunction, R target)
319
return Iterate.collectInt(this.getDelegate(), intFunction, target);
322
public MutableLongCollection collectLong(LongFunction<? super T> longFunction)
324
return Iterate.collectLong(this.getDelegate(), longFunction);
327
public <R extends MutableLongCollection> R collectLong(LongFunction<? super T> longFunction, R target)
329
return Iterate.collectLong(this.getDelegate(), longFunction, target);
332
public MutableShortCollection collectShort(ShortFunction<? super T> shortFunction)
334
return Iterate.collectShort(this.getDelegate(), shortFunction);
337
public <R extends MutableShortCollection> R collectShort(ShortFunction<? super T> shortFunction, R target)
339
return Iterate.collectShort(this.getDelegate(), shortFunction, target);
342
public <V, R extends Collection<V>> R collect(Function<? super T, ? extends V> function, R target)
344
return Iterate.collect(this.getDelegate(), function, target);
347
public <V> MutableCollection<V> flatCollect(Function<? super T, ? extends Iterable<V>> function)
349
return this.wrap(Iterate.flatCollect(this.getDelegate(), function));
352
public <V, R extends Collection<V>> R flatCollect(
353
Function<? super T, ? extends Iterable<V>> function,
356
return Iterate.flatCollect(this.getDelegate(), function, target);
359
public <V> MutableCollection<V> collectIf(
360
Predicate<? super T> predicate,
361
Function<? super T, ? extends V> function)
363
return this.wrap(Iterate.collectIf(this.getDelegate(), predicate, function));
366
public <V, R extends Collection<V>> R collectIf(
367
Predicate<? super T> predicate,
368
Function<? super T, ? extends V> function,
371
return Iterate.collectIf(this.getDelegate(), predicate, function, target);
374
public <P> Twin<MutableList<T>> selectAndRejectWith(
375
Predicate2<? super T, ? super P> predicate,
378
return Iterate.selectAndRejectWith(this.getDelegate(), predicate, parameter);
381
public PartitionMutableCollection<T> partition(Predicate<? super T> predicate)
383
return (PartitionMutableCollection<T>) Iterate.partition(this.getDelegate(), predicate);
386
public <P> PartitionMutableCollection<T> partitionWith(Predicate2<? super T, ? super P> predicate, P parameter)
388
return (PartitionMutableCollection<T>) Iterate.partitionWith(this.getDelegate(), predicate, parameter);
393
return this.getDelegate().size();
396
public boolean isEmpty()
398
return this.getDelegate().isEmpty();
401
public boolean contains(Object o)
403
return this.getDelegate().contains(o);
406
public Iterator<T> iterator()
408
return this.getDelegate().iterator();
411
public Object[] toArray()
413
return this.getDelegate().toArray();
416
public <E> E[] toArray(E[] a)
418
return this.getDelegate().toArray(a);
421
public boolean add(T o)
423
return this.getDelegate().add(o);
426
public boolean remove(Object o)
428
return this.getDelegate().remove(o);
431
public boolean containsAll(Collection<?> collection)
433
return this.getDelegate().containsAll(collection);
436
public boolean containsAllIterable(Iterable<?> source)
438
return Iterate.allSatisfyWith(source, Predicates2.in(), this.getDelegate());
441
public boolean containsAllArguments(Object... elements)
443
return ArrayIterate.allSatisfyWith(elements, Predicates2.in(), this.getDelegate());
446
public boolean addAll(Collection<? extends T> collection)
448
boolean result = false;
449
for (T each : collection)
451
result |= this.add(each);
456
public boolean addAllIterable(Iterable<? extends T> iterable)
458
return Iterate.addAllIterable(iterable, this);
461
public boolean removeAll(Collection<?> collection)
463
int currentSize = this.size();
464
this.removeIfWith(Predicates2.in(), collection);
465
return currentSize != this.size();
468
public boolean removeAllIterable(Iterable<?> iterable)
470
return this.removeAll(CollectionAdapter.wrapSet(iterable));
473
public boolean retainAll(Collection<?> collection)
475
int currentSize = this.size();
476
this.removeIfWith(Predicates2.notIn(), collection);
477
return currentSize != this.size();
480
public boolean retainAllIterable(Iterable<?> iterable)
482
return this.retainAll(CollectionAdapter.wrapSet(iterable));
487
this.getDelegate().clear();
490
public <P> void forEachWith(Procedure2<? super T, ? super P> procedure, P parameter)
492
Iterate.forEachWith(this.getDelegate(), procedure, parameter);
495
public <P> MutableCollection<T> selectWith(
496
Predicate2<? super T, ? super P> predicate,
499
return this.wrap(Iterate.selectWith(this.getDelegate(), predicate, parameter));
502
public <P, R extends Collection<T>> R selectWith(
503
Predicate2<? super T, ? super P> predicate,
507
return Iterate.selectWith(this.getDelegate(), predicate, parameter, targetCollection);
510
public <P> MutableCollection<T> rejectWith(
511
Predicate2<? super T, ? super P> predicate,
514
return this.wrap(Iterate.rejectWith(this.getDelegate(), predicate, parameter));
517
public <P, R extends Collection<T>> R rejectWith(
518
Predicate2<? super T, ? super P> predicate,
522
return Iterate.rejectWith(this.getDelegate(), predicate, parameter, targetCollection);
525
public <P, V> MutableCollection<V> collectWith(
526
Function2<? super T, ? super P, ? extends V> function,
529
return this.wrap(Iterate.collectWith(this.getDelegate(), function, parameter));
532
public <P, A, R extends Collection<A>> R collectWith(
533
Function2<? super T, ? super P, ? extends A> function,
537
return Iterate.collectWith(this.getDelegate(), function, parameter, targetCollection);
540
public <IV, P> IV injectIntoWith(
542
Function3<? super IV, ? super T, ? super P, ? extends IV> function,
545
return Iterate.injectIntoWith(injectValue, this.getDelegate(), function, parameter);
548
public MutableList<T> toList()
550
return Lists.mutable.ofAll(this.getDelegate());
553
public MutableList<T> toSortedList()
555
return this.toList().sortThis();
558
public MutableList<T> toSortedList(Comparator<? super T> comparator)
560
return this.toList().sortThis(comparator);
563
public <V extends Comparable<? super V>> MutableList<T> toSortedListBy(Function<? super T, ? extends V> function)
565
return this.toSortedList(Comparators.byFunction(function));
568
public MutableSortedSet<T> toSortedSet()
570
return TreeSortedSet.newSet(null, this);
573
public MutableSortedSet<T> toSortedSet(Comparator<? super T> comparator)
575
return TreeSortedSet.newSet(comparator, this);
578
public <V extends Comparable<? super V>> MutableSortedSet<T> toSortedSetBy(
579
Function<? super T, ? extends V> function)
581
return this.toSortedSet(Comparators.byFunction(function));
584
public MutableSet<T> toSet()
586
return UnifiedSet.newSet(this.getDelegate());
589
public MutableBag<T> toBag()
591
return HashBag.newBag(this.getDelegate());
594
public <K, V> MutableMap<K, V> toMap(
595
Function<? super T, ? extends K> keyFunction,
596
Function<? super T, ? extends V> valueFunction)
598
UnifiedMap<K, V> map = UnifiedMap.newMap(this.size());
599
map.collectKeysAndValues(this.getDelegate(), keyFunction, valueFunction);
603
public <K, V> MutableSortedMap<K, V> toSortedMap(
604
Function<? super T, ? extends K> keyFunction,
605
Function<? super T, ? extends V> valueFunction)
607
return TreeSortedMap.<K, V>newMap().collectKeysAndValues(this.getDelegate(), keyFunction, valueFunction);
610
public <K, V> MutableSortedMap<K, V> toSortedMap(
611
Comparator<? super K> comparator,
612
Function<? super T, ? extends K> keyFunction,
613
Function<? super T, ? extends V> valueFunction)
615
return TreeSortedMap.<K, V>newMap(comparator).collectKeysAndValues(this.getDelegate(), keyFunction, valueFunction);
618
public LazyIterable<T> asLazy()
620
return LazyIterate.adapt(this);
623
public <P> T detectWith(Predicate2<? super T, ? super P> predicate, P parameter)
625
return Iterate.detectWith(this.getDelegate(), predicate, parameter);
628
public <P> T detectWithIfNone(
629
Predicate2<? super T, ? super P> predicate,
631
Function0<? extends T> function)
633
T result = this.detectWith(predicate, parameter);
634
return result == null ? function.value() : result;
637
public <P> int countWith(Predicate2<? super T, ? super P> predicate, P parameter)
639
return Iterate.countWith(this.getDelegate(), predicate, parameter);
642
public <P> boolean anySatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
644
return Iterate.anySatisfyWith(this.getDelegate(), predicate, parameter);
647
public <P> boolean allSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
649
return Iterate.allSatisfyWith(this.getDelegate(), predicate, parameter);
652
public <P> boolean noneSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
654
return Iterate.noneSatisfyWith(this.getDelegate(), predicate, parameter);
658
public String toString()
660
return this.makeString("[", ", ", "]");
663
public String makeString()
665
return this.makeString(", ");
668
public String makeString(String separator)
670
return this.makeString("", separator, "");
673
public String makeString(String start, String separator, String end)
675
Appendable stringBuilder = new StringBuilder();
676
this.appendString(stringBuilder, start, separator, end);
677
return stringBuilder.toString();
680
public void appendString(Appendable appendable)
682
this.appendString(appendable, ", ");
685
public void appendString(Appendable appendable, String separator)
687
this.appendString(appendable, "", separator, "");
690
public void appendString(Appendable appendable, String start, String separator, String end)
692
IterableIterate.appendString(this, appendable, start, separator, end);
695
public <V> MutableMultimap<V, T> groupBy(
696
Function<? super T, ? extends V> function)
698
return Iterate.groupBy(this.getDelegate(), function);
701
public <V, R extends MutableMultimap<V, T>> R groupBy(
702
Function<? super T, ? extends V> function,
705
return Iterate.groupBy(this.getDelegate(), function, target);
708
public <V> MutableMultimap<V, T> groupByEach(
709
Function<? super T, ? extends Iterable<V>> function)
711
return Iterate.groupByEach(this.getDelegate(), function);
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 <V, R extends MutableMultimap<V, T>> R groupByEach(
720
Function<? super T, ? extends Iterable<V>> function,
723
return Iterate.groupByEach(this.getDelegate(), function, target);
726
public <S> MutableCollection<Pair<T, S>> zip(Iterable<S> that)
728
return this.wrap(Iterate.zip(this.getDelegate(), that));
731
public <S, R extends Collection<Pair<T, S>>> R zip(Iterable<S> that, R target)
733
return Iterate.zip(this.getDelegate(), that, target);
736
public MutableCollection<Pair<T, Integer>> zipWithIndex()
738
return this.wrap(Iterate.zipWithIndex(this.getDelegate()));
741
public <R extends Collection<Pair<T, Integer>>> R zipWithIndex(R target)
743
return Iterate.zipWithIndex(this.getDelegate(), target);
746
public RichIterable<RichIterable<T>> chunk(int size)
748
return MutableCollectionIterate.chunk(this, size);
751
public <K, V> MutableMap<K, V> aggregateInPlaceBy(
752
Function<? super T, ? extends K> groupBy,
753
Function0<? extends V> zeroValueFactory,
754
Procedure2<? super V, ? super T> mutatingAggregator)
756
MutableMap<K, V> map = UnifiedMap.newMap();
757
this.forEach(new MutatingAggregationProcedure<T, K, V>(map, groupBy, zeroValueFactory, mutatingAggregator));
761
public <K, V> MutableMap<K, V> aggregateBy(
762
Function<? super T, ? extends K> groupBy,
763
Function0<? extends V> zeroValueFactory,
764
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
766
MutableMap<K, V> map = UnifiedMap.newMap();
767
this.forEach(new NonMutatingAggregationProcedure<T, K, V>(map, groupBy, zeroValueFactory, nonMutatingAggregator));