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.map;
19
import java.io.Serializable;
20
import java.util.Collection;
21
import java.util.Comparator;
22
import java.util.Iterator;
24
import com.gs.collections.api.LazyIterable;
25
import com.gs.collections.api.RichIterable;
26
import com.gs.collections.api.bag.MutableBag;
27
import com.gs.collections.api.block.function.Function;
28
import com.gs.collections.api.block.function.Function0;
29
import com.gs.collections.api.block.function.Function2;
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.primitive.MutableBooleanCollection;
48
import com.gs.collections.api.collection.primitive.MutableByteCollection;
49
import com.gs.collections.api.collection.primitive.MutableCharCollection;
50
import com.gs.collections.api.collection.primitive.MutableDoubleCollection;
51
import com.gs.collections.api.collection.primitive.MutableFloatCollection;
52
import com.gs.collections.api.collection.primitive.MutableIntCollection;
53
import com.gs.collections.api.collection.primitive.MutableLongCollection;
54
import com.gs.collections.api.collection.primitive.MutableShortCollection;
55
import com.gs.collections.api.list.MutableList;
56
import com.gs.collections.api.map.MapIterable;
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.set.MutableSet;
61
import com.gs.collections.api.set.sorted.MutableSortedSet;
62
import com.gs.collections.api.tuple.Pair;
65
* A synchronized view of a map.
67
public abstract class SynchronizedMapIterable<K, V>
68
implements MapIterable<K, V>, Serializable
70
private static final long serialVersionUID = 1L;
72
protected final Object lock;
73
private final MapIterable<K, V> mapIterable;
75
protected SynchronizedMapIterable(MapIterable<K, V> newMap)
80
protected SynchronizedMapIterable(MapIterable<K, V> newMap, Object newLock)
84
throw new IllegalArgumentException("Cannot create a SynchronizedMapIterable on a null map");
86
this.mapIterable = newMap;
87
this.lock = newLock == null ? this : newLock;
90
protected MapIterable<K, V> getMap()
92
return this.mapIterable;
95
public V get(Object key)
97
synchronized (this.lock)
99
return this.mapIterable.get(key);
103
public boolean containsKey(Object key)
105
synchronized (this.lock)
107
return this.mapIterable.containsKey(key);
111
public boolean containsValue(Object value)
113
synchronized (this.lock)
115
return this.mapIterable.containsValue(value);
119
public void forEachValue(Procedure<? super V> procedure)
121
synchronized (this.lock)
123
this.mapIterable.forEachValue(procedure);
127
public void forEachKey(Procedure<? super K> procedure)
129
synchronized (this.lock)
131
this.mapIterable.forEachKey(procedure);
135
public void forEachKeyValue(Procedure2<? super K, ? super V> procedure2)
137
synchronized (this.lock)
139
this.mapIterable.forEachKeyValue(procedure2);
143
public V getIfAbsent(K key, Function0<? extends V> function)
145
synchronized (this.lock)
147
return this.mapIterable.getIfAbsent(key, function);
151
public V getIfAbsentValue(K key, V value)
153
synchronized (this.lock)
155
return this.mapIterable.getIfAbsentValue(key, value);
159
public <P> V getIfAbsentWith(K key, Function<? super P, ? extends V> function, P parameter)
161
synchronized (this.lock)
163
return this.mapIterable.getIfAbsentWith(key, function, parameter);
167
public <A> A ifPresentApply(K key, Function<? super V, ? extends A> function)
169
synchronized (this.lock)
171
return this.mapIterable.ifPresentApply(key, function);
175
public Pair<K, V> detect(Predicate2<? super K, ? super V> predicate)
177
synchronized (this.lock)
179
return this.mapIterable.detect(predicate);
185
synchronized (this.lock)
187
return this.mapIterable.size();
191
public boolean isEmpty()
193
synchronized (this.lock)
195
return this.mapIterable.isEmpty();
199
public boolean notEmpty()
201
synchronized (this.lock)
203
return this.mapIterable.notEmpty();
209
synchronized (this.lock)
211
return this.mapIterable.getFirst();
217
synchronized (this.lock)
219
return this.mapIterable.getLast();
223
public boolean contains(Object object)
225
synchronized (this.lock)
227
return this.mapIterable.contains(object);
231
public boolean containsAllIterable(Iterable<?> source)
233
synchronized (this.lock)
235
return this.mapIterable.containsAllIterable(source);
239
public boolean containsAll(Collection<?> source)
241
synchronized (this.lock)
243
return this.mapIterable.containsAll(source);
247
public boolean containsAllArguments(Object... elements)
249
synchronized (this.lock)
251
return this.mapIterable.containsAllArguments(elements);
255
public <R extends Collection<V>> R select(Predicate<? super V> predicate, R target)
257
synchronized (this.lock)
259
return this.mapIterable.select(predicate, target);
263
public <P> RichIterable<V> selectWith(Predicate2<? super V, ? super P> predicate, P parameter)
265
synchronized (this.lock)
267
return this.mapIterable.selectWith(predicate, parameter);
271
public <P, R extends Collection<V>> R selectWith(Predicate2<? super V, ? super P> predicate, P parameter, R targetCollection)
273
synchronized (this.lock)
275
return this.mapIterable.selectWith(predicate, parameter, targetCollection);
279
public <R extends Collection<V>> R reject(Predicate<? super V> predicate, R target)
281
synchronized (this.lock)
283
return this.mapIterable.reject(predicate, target);
287
public <P> RichIterable<V> rejectWith(Predicate2<? super V, ? super P> predicate, P parameter)
289
synchronized (this.lock)
291
return this.mapIterable.rejectWith(predicate, parameter);
295
public <P, R extends Collection<V>> R rejectWith(Predicate2<? super V, ? super P> predicate, P parameter, R targetCollection)
297
synchronized (this.lock)
299
return this.mapIterable.rejectWith(predicate, parameter, targetCollection);
303
public V detect(Predicate<? super V> predicate)
305
synchronized (this.lock)
307
return this.mapIterable.detect(predicate);
311
public <P> V detectWith(Predicate2<? super V, ? super P> predicate, P parameter)
313
synchronized (this.lock)
315
return this.mapIterable.detectWith(predicate, parameter);
319
public V detectIfNone(Predicate<? super V> predicate, Function0<? extends V> function)
321
synchronized (this.lock)
323
return this.mapIterable.detectIfNone(predicate, function);
327
public <P> V detectWithIfNone(Predicate2<? super V, ? super P> predicate, P parameter, Function0<? extends V> function)
329
synchronized (this.lock)
331
return this.mapIterable.detectWithIfNone(predicate, parameter, function);
335
public int count(Predicate<? super V> predicate)
337
synchronized (this.lock)
339
return this.mapIterable.count(predicate);
343
public <P> int countWith(Predicate2<? super V, ? super P> predicate, P parameter)
345
synchronized (this.lock)
347
return this.mapIterable.countWith(predicate, parameter);
351
public boolean anySatisfy(Predicate<? super V> predicate)
353
synchronized (this.lock)
355
return this.mapIterable.anySatisfy(predicate);
359
public boolean allSatisfy(Predicate<? super V> predicate)
361
synchronized (this.lock)
363
return this.mapIterable.allSatisfy(predicate);
367
public boolean noneSatisfy(Predicate<? super V> predicate)
369
synchronized (this.lock)
371
return this.mapIterable.noneSatisfy(predicate);
375
public <P> boolean anySatisfyWith(Predicate2<? super V, ? super P> predicate, P parameter)
377
synchronized (this.lock)
379
return this.mapIterable.anySatisfyWith(predicate, parameter);
383
public <P> boolean allSatisfyWith(Predicate2<? super V, ? super P> predicate, P parameter)
385
synchronized (this.lock)
387
return this.mapIterable.allSatisfyWith(predicate, parameter);
391
public <P> boolean noneSatisfyWith(Predicate2<? super V, ? super P> predicate, P parameter)
393
synchronized (this.lock)
395
return this.mapIterable.noneSatisfyWith(predicate, parameter);
399
public <IV> IV injectInto(IV injectedValue, Function2<? super IV, ? super V, ? extends IV> function)
401
synchronized (this.lock)
403
return this.mapIterable.injectInto(injectedValue, function);
407
public int injectInto(int injectedValue, IntObjectToIntFunction<? super V> function)
409
synchronized (this.lock)
411
return this.mapIterable.injectInto(injectedValue, function);
415
public long injectInto(long injectedValue, LongObjectToLongFunction<? super V> function)
417
synchronized (this.lock)
419
return this.mapIterable.injectInto(injectedValue, function);
423
public double injectInto(double injectedValue, DoubleObjectToDoubleFunction<? super V> function)
425
synchronized (this.lock)
427
return this.mapIterable.injectInto(injectedValue, function);
431
public float injectInto(float injectedValue, FloatObjectToFloatFunction<? super V> function)
433
synchronized (this.lock)
435
return this.mapIterable.injectInto(injectedValue, function);
439
public long sumOfInt(IntFunction<? super V> function)
441
synchronized (this.lock)
443
return this.mapIterable.sumOfInt(function);
447
public double sumOfFloat(FloatFunction<? super V> function)
449
synchronized (this.lock)
451
return this.mapIterable.sumOfFloat(function);
455
public long sumOfLong(LongFunction<? super V> function)
457
synchronized (this.lock)
459
return this.mapIterable.sumOfLong(function);
463
public double sumOfDouble(DoubleFunction<? super V> function)
465
synchronized (this.lock)
467
return this.mapIterable.sumOfDouble(function);
471
public MutableList<V> toList()
473
synchronized (this.lock)
475
return this.mapIterable.toList();
479
public MutableList<V> toSortedList()
481
synchronized (this.lock)
483
return this.mapIterable.toSortedList();
487
public MutableList<V> toSortedList(Comparator<? super V> comparator)
489
synchronized (this.lock)
491
return this.mapIterable.toSortedList(comparator);
495
public MutableSortedSet<V> toSortedSet()
497
synchronized (this.lock)
499
return this.mapIterable.toSortedSet();
503
public MutableSortedSet<V> toSortedSet(Comparator<? super V> comparator)
505
synchronized (this.lock)
507
return this.mapIterable.toSortedSet(comparator);
511
public MutableSet<V> toSet()
513
synchronized (this.lock)
515
return this.mapIterable.toSet();
519
public MutableBag<V> toBag()
521
synchronized (this.lock)
523
return this.mapIterable.toBag();
527
public <NK, NV> MutableMap<NK, NV> toMap(
528
Function<? super V, ? extends NK> keyFunction,
529
Function<? super V, ? extends NV> valueFunction)
531
synchronized (this.lock)
533
return this.mapIterable.toMap(keyFunction, valueFunction);
537
public <NK, NV> MutableSortedMap<NK, NV> toSortedMap(
538
Function<? super V, ? extends NK> keyFunction,
539
Function<? super V, ? extends NV> valueFunction)
541
synchronized (this.lock)
543
return this.mapIterable.toSortedMap(keyFunction, valueFunction);
547
public <NK, NV> MutableSortedMap<NK, NV> toSortedMap(
548
Comparator<? super NK> comparator,
549
Function<? super V, ? extends NK> keyFunction,
550
Function<? super V, ? extends NV> valueFunction)
552
synchronized (this.lock)
554
return this.mapIterable.toSortedMap(comparator, keyFunction, valueFunction);
558
public LazyIterable<V> asLazy()
560
synchronized (this.lock)
562
return this.mapIterable.asLazy();
566
public Object[] toArray()
568
synchronized (this.lock)
570
return this.mapIterable.toArray();
574
public <T> T[] toArray(T[] a)
576
synchronized (this.lock)
578
return this.mapIterable.toArray(a);
582
public V min(Comparator<? super V> comparator)
584
synchronized (this.lock)
586
return this.mapIterable.min(comparator);
590
public V max(Comparator<? super V> comparator)
592
synchronized (this.lock)
594
return this.mapIterable.max(comparator);
600
synchronized (this.lock)
602
return this.mapIterable.min();
608
synchronized (this.lock)
610
return this.mapIterable.max();
614
public String makeString()
616
synchronized (this.lock)
618
return this.mapIterable.makeString();
622
public String makeString(String separator)
624
synchronized (this.lock)
626
return this.mapIterable.makeString(separator);
630
public String makeString(String start, String separator, String end)
632
synchronized (this.lock)
634
return this.mapIterable.makeString(start, separator, end);
638
public void appendString(Appendable appendable)
640
synchronized (this.lock)
642
this.mapIterable.appendString(appendable);
646
public void appendString(Appendable appendable, String separator)
648
synchronized (this.lock)
650
this.mapIterable.appendString(appendable, separator);
654
public void appendString(Appendable appendable, String start, String separator, String end)
656
synchronized (this.lock)
658
this.mapIterable.appendString(appendable, start, separator, end);
662
public <S, R extends Collection<Pair<V, S>>> R zip(Iterable<S> that, R target)
664
synchronized (this.lock)
666
return this.mapIterable.zip(that, target);
670
public <R extends Collection<Pair<V, Integer>>> R zipWithIndex(R target)
672
synchronized (this.lock)
674
return this.mapIterable.zipWithIndex(target);
678
public RichIterable<RichIterable<V>> chunk(int size)
680
synchronized (this.lock)
682
return this.mapIterable.chunk(size);
686
public <KK, R extends MutableMultimap<KK, V>> R groupBy(Function<? super V, ? extends KK> function, R target)
688
synchronized (this.lock)
690
return this.mapIterable.groupBy(function, target);
694
public <KK, R extends MutableMultimap<KK, V>> R groupByEach(Function<? super V, ? extends Iterable<KK>> function, R target)
696
synchronized (this.lock)
698
return this.mapIterable.groupByEach(function, target);
702
public <A extends Comparable<? super A>> V minBy(Function<? super V, ? extends A> function)
704
synchronized (this.lock)
706
return this.mapIterable.minBy(function);
710
public <A extends Comparable<? super A>> V maxBy(Function<? super V, ? extends A> function)
712
synchronized (this.lock)
714
return this.mapIterable.maxBy(function);
718
public <A extends Comparable<? super A>> MutableSortedSet<V> toSortedSetBy(Function<? super V, ? extends A> function)
720
synchronized (this.lock)
722
return this.mapIterable.toSortedSetBy(function);
726
public <A extends Comparable<? super A>> MutableList<V> toSortedListBy(Function<? super V, ? extends A> function)
728
synchronized (this.lock)
730
return this.mapIterable.toSortedListBy(function);
734
public <A, R extends Collection<A>> R flatCollect(Function<? super V, ? extends Iterable<A>> function, R target)
736
synchronized (this.lock)
738
return this.mapIterable.flatCollect(function, target);
742
public <P, A> RichIterable<A> collectWith(
743
Function2<? super V, ? super P, ? extends A> function,
746
synchronized (this.lock)
748
return this.mapIterable.collectWith(function, parameter);
752
public <A, R extends Collection<A>> R collectIf(
753
Predicate<? super V> predicate,
754
Function<? super V, ? extends A> function,
757
synchronized (this.lock)
759
return this.mapIterable.collectIf(predicate, function, target);
763
public <P, A, R extends Collection<A>> R collectWith(
764
Function2<? super V, ? super P, ? extends A> function,
768
synchronized (this.lock)
770
return this.mapIterable.collectWith(function, parameter, targetCollection);
774
public <A, R extends Collection<A>> R collect(
775
Function<? super V, ? extends A> function,
778
synchronized (this.lock)
780
return this.mapIterable.collect(function, target);
784
public <R extends MutableBooleanCollection> R collectBoolean(BooleanFunction<? super V> booleanFunction, R target)
786
synchronized (this.lock)
788
return this.mapIterable.collectBoolean(booleanFunction, target);
792
public <R extends MutableByteCollection> R collectByte(ByteFunction<? super V> byteFunction, R target)
794
synchronized (this.lock)
796
return this.mapIterable.collectByte(byteFunction, target);
800
public <R extends MutableCharCollection> R collectChar(CharFunction<? super V> charFunction, R target)
802
synchronized (this.lock)
804
return this.mapIterable.collectChar(charFunction, target);
808
public <R extends MutableDoubleCollection> R collectDouble(DoubleFunction<? super V> doubleFunction, R target)
810
synchronized (this.lock)
812
return this.mapIterable.collectDouble(doubleFunction, target);
816
public <R extends MutableFloatCollection> R collectFloat(FloatFunction<? super V> floatFunction, R target)
818
synchronized (this.lock)
820
return this.mapIterable.collectFloat(floatFunction, target);
824
public <R extends MutableIntCollection> R collectInt(IntFunction<? super V> intFunction, R target)
826
synchronized (this.lock)
828
return this.mapIterable.collectInt(intFunction, target);
832
public <R extends MutableLongCollection> R collectLong(LongFunction<? super V> longFunction, R target)
834
synchronized (this.lock)
836
return this.mapIterable.collectLong(longFunction, target);
840
public <R extends MutableShortCollection> R collectShort(ShortFunction<? super V> shortFunction, R target)
842
synchronized (this.lock)
844
return this.mapIterable.collectShort(shortFunction, target);
848
public void forEach(Procedure<? super V> procedure)
850
synchronized (this.lock)
852
this.mapIterable.forEach(procedure);
856
public void forEachWithIndex(ObjectIntProcedure<? super V> objectIntProcedure)
858
synchronized (this.lock)
860
this.mapIterable.forEachWithIndex(objectIntProcedure);
864
public <P> void forEachWith(Procedure2<? super V, ? super P> procedure2, P parameter)
866
synchronized (this.lock)
868
this.mapIterable.forEachWith(procedure2, parameter);
872
public Iterator<V> iterator()
874
return this.mapIterable.iterator();