/* * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gs.collections.impl.map.sorted.mutable; import java.io.Serializable; import java.util.Comparator; import java.util.SortedMap; import com.gs.collections.api.LazyIterable; import com.gs.collections.api.RichIterable; import com.gs.collections.api.block.function.Function; import com.gs.collections.api.block.function.Function0; import com.gs.collections.api.block.function.Function2; import com.gs.collections.api.block.function.primitive.BooleanFunction; import com.gs.collections.api.block.function.primitive.ByteFunction; import com.gs.collections.api.block.function.primitive.CharFunction; import com.gs.collections.api.block.function.primitive.DoubleFunction; import com.gs.collections.api.block.function.primitive.FloatFunction; import com.gs.collections.api.block.function.primitive.IntFunction; import com.gs.collections.api.block.function.primitive.LongFunction; import com.gs.collections.api.block.function.primitive.ShortFunction; import com.gs.collections.api.block.predicate.Predicate; import com.gs.collections.api.block.predicate.Predicate2; import com.gs.collections.api.block.procedure.Procedure; import com.gs.collections.api.block.procedure.Procedure2; import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure; import com.gs.collections.api.collection.MutableCollection; import com.gs.collections.api.list.MutableList; import com.gs.collections.api.list.primitive.MutableBooleanList; import com.gs.collections.api.list.primitive.MutableByteList; import com.gs.collections.api.list.primitive.MutableCharList; import com.gs.collections.api.list.primitive.MutableDoubleList; import com.gs.collections.api.list.primitive.MutableFloatList; import com.gs.collections.api.list.primitive.MutableIntList; import com.gs.collections.api.list.primitive.MutableLongList; import com.gs.collections.api.list.primitive.MutableShortList; import com.gs.collections.api.map.MutableMap; import com.gs.collections.api.map.MutableMapIterable; import com.gs.collections.api.map.sorted.ImmutableSortedMap; import com.gs.collections.api.map.sorted.MutableSortedMap; import com.gs.collections.api.multimap.list.MutableListMultimap; import com.gs.collections.api.multimap.sortedset.MutableSortedSetMultimap; import com.gs.collections.api.ordered.OrderedIterable; import com.gs.collections.api.partition.list.PartitionMutableList; import com.gs.collections.api.set.MutableSet; import com.gs.collections.api.stack.MutableStack; import com.gs.collections.api.tuple.Pair; import com.gs.collections.impl.collection.mutable.SynchronizedMutableCollection; import com.gs.collections.impl.factory.SortedMaps; import com.gs.collections.impl.list.fixed.ArrayAdapter; import com.gs.collections.impl.map.AbstractSynchronizedMapIterable; import com.gs.collections.impl.map.mutable.SynchronizedMapSerializationProxy; import com.gs.collections.impl.set.mutable.SynchronizedMutableSet; import com.gs.collections.impl.utility.LazyIterate; /** * A synchronized view of a SortedMap. * * @see #SynchronizedSortedMap(MutableSortedMap) */ public class SynchronizedSortedMap<K, V> extends AbstractSynchronizedMapIterable<K, V> implements MutableSortedMap<K, V>, Serializable { private static final long serialVersionUID = 2L; public SynchronizedSortedMap(MutableSortedMap<K, V> newMap) { super(newMap); } public SynchronizedSortedMap(MutableSortedMap<K, V> newMap, Object lock) { super(newMap, lock); } /** * This method will take a MutableSortedMap and wrap it directly in a SynchronizedSortedMap. It will * take any other non-GS-SortedMap and first adapt it will a SortedMapAdapter, and then return a * SynchronizedSortedMap that wraps the adapter. */ public static <K, V, M extends SortedMap<K, V>> SynchronizedSortedMap<K, V> of(M map) { return new SynchronizedSortedMap<K, V>(SortedMapAdapter.adapt(map)); } public static <K, V, M extends SortedMap<K, V>> SynchronizedSortedMap<K, V> of(M map, Object lock) { return new SynchronizedSortedMap<K, V>(SortedMapAdapter.adapt(map), lock); } @Override protected MutableSortedMap<K, V> getDelegate() { return (MutableSortedMap<K, V>) super.getDelegate(); } public Comparator<? super K> comparator() { synchronized (this.lock) { return this.getDelegate().comparator(); } } public MutableSortedMap<K, V> withKeyValue(K key, V value) { synchronized (this.lock) { this.put(key, value); return this; } } /** * @deprecated in 6.0 Use {@link #withAllKeyValueArguments(Pair[])} instead. Inlineable. */ @Deprecated public MutableSortedMap<K, V> with(Pair<K, V>... pairs) { return this.withAllKeyValueArguments(pairs); } public MutableSortedMap<K, V> withAllKeyValueArguments(Pair<? extends K, ? extends V>... keyValuePairs) { return this.withAllKeyValues(ArrayAdapter.adapt(keyValuePairs)); } public MutableSortedMap<K, V> withAllKeyValues(Iterable<? extends Pair<? extends K, ? extends V>> keyValues) { synchronized (this.lock) { for (Pair<? extends K, ? extends V> keyValue : keyValues) { this.getDelegate().put(keyValue.getOne(), keyValue.getTwo()); } return this; } } public MutableSortedMap<K, V> withoutKey(K key) { this.remove(key); return this; } public MutableSortedMap<K, V> withoutAllKeys(Iterable<? extends K> keys) { synchronized (this.lock) { for (K key : keys) { this.getDelegate().removeKey(key); } return this; } } public MutableSortedMap<K, V> newEmpty() { synchronized (this.lock) { return this.getDelegate().newEmpty(); } } @Override public MutableSortedMap<K, V> clone() { synchronized (this.lock) { return SynchronizedSortedMap.of(this.getDelegate().clone()); } } protected Object writeReplace() { return new SynchronizedMapSerializationProxy<K, V>(this.getDelegate()); } public <E> MutableSortedMap<K, V> collectKeysAndValues( Iterable<E> iterable, Function<? super E, ? extends K> keyFunction, Function<? super E, ? extends V> function) { synchronized (this.lock) { return this.getDelegate().collectKeysAndValues(iterable, keyFunction, function); } } public K firstKey() { synchronized (this.getLock()) { return this.getDelegate().firstKey(); } } public K lastKey() { synchronized (this.getLock()) { return this.getDelegate().lastKey(); } } public MutableSortedMap<K, V> tap(Procedure<? super V> procedure) { synchronized (this.lock) { this.forEach(procedure); return this; } } public MutableList<V> select(Predicate<? super V> predicate) { synchronized (this.lock) { return this.getDelegate().select(predicate); } } public <P> MutableList<V> selectWith(Predicate2<? super V, ? super P> predicate, P parameter) { synchronized (this.lock) { return this.getDelegate().selectWith(predicate, parameter); } } public MutableList<V> reject(Predicate<? super V> predicate) { synchronized (this.lock) { return this.getDelegate().reject(predicate); } } public <P> MutableList<V> rejectWith(Predicate2<? super V, ? super P> predicate, P parameter) { synchronized (this.lock) { return this.getDelegate().rejectWith(predicate, parameter); } } public PartitionMutableList<V> partition(Predicate<? super V> predicate) { synchronized (this.lock) { return this.getDelegate().partition(predicate); } } public MutableList<Pair<V, Integer>> zipWithIndex() { synchronized (this.lock) { return this.getDelegate().zipWithIndex(); } } public <P> PartitionMutableList<V> partitionWith(Predicate2<? super V, ? super P> predicate, P parameter) { synchronized (this.lock) { return this.getDelegate().partitionWith(predicate, parameter); } } public <S> MutableList<S> selectInstancesOf(Class<S> clazz) { synchronized (this.lock) { return this.getDelegate().selectInstancesOf(clazz); } } public <R> MutableList<R> collect(Function<? super V, ? extends R> function) { synchronized (this.lock) { return this.getDelegate().collect(function); } } public MutableBooleanList collectBoolean(BooleanFunction<? super V> booleanFunction) { synchronized (this.lock) { return this.getDelegate().collectBoolean(booleanFunction); } } public MutableByteList collectByte(ByteFunction<? super V> byteFunction) { synchronized (this.lock) { return this.getDelegate().collectByte(byteFunction); } } public MutableCharList collectChar(CharFunction<? super V> charFunction) { synchronized (this.lock) { return this.getDelegate().collectChar(charFunction); } } public MutableDoubleList collectDouble(DoubleFunction<? super V> doubleFunction) { synchronized (this.lock) { return this.getDelegate().collectDouble(doubleFunction); } } public MutableFloatList collectFloat(FloatFunction<? super V> floatFunction) { synchronized (this.lock) { return this.getDelegate().collectFloat(floatFunction); } } public MutableIntList collectInt(IntFunction<? super V> intFunction) { synchronized (this.lock) { return this.getDelegate().collectInt(intFunction); } } public MutableLongList collectLong(LongFunction<? super V> longFunction) { synchronized (this.lock) { return this.getDelegate().collectLong(longFunction); } } public MutableShortList collectShort(ShortFunction<? super V> shortFunction) { synchronized (this.lock) { return this.getDelegate().collectShort(shortFunction); } } public <P, VV> MutableList<VV> collectWith(Function2<? super V, ? super P, ? extends VV> function, P parameter) { synchronized (this.lock) { return this.getDelegate().collectWith(function, parameter); } } public <R> MutableList<R> collectIf( Predicate<? super V> predicate, Function<? super V, ? extends R> function) { synchronized (this.lock) { return this.getDelegate().collectIf(predicate, function); } } public <R> MutableList<R> flatCollect(Function<? super V, ? extends Iterable<R>> function) { synchronized (this.lock) { return this.getDelegate().flatCollect(function); } } @Override public String makeString() { synchronized (this.lock) { return this.getDelegate().makeString(); } } @Override public String makeString(String separator) { synchronized (this.lock) { return this.getDelegate().makeString(separator); } } @Override public String makeString(String start, String separator, String end) { synchronized (this.lock) { return this.getDelegate().makeString(start, separator, end); } } public <KK> MutableListMultimap<KK, V> groupBy(Function<? super V, ? extends KK> function) { synchronized (this.lock) { return this.getDelegate().groupBy(function); } } public <KK> MutableListMultimap<KK, V> groupByEach(Function<? super V, ? extends Iterable<KK>> function) { synchronized (this.lock) { return this.getDelegate().groupByEach(function); } } public <S> MutableList<Pair<V, S>> zip(Iterable<S> that) { synchronized (this.lock) { return this.getDelegate().zip(that); } } public <K2, V2> MutableMap<K2, V2> aggregateInPlaceBy( Function<? super V, ? extends K2> groupBy, Function0<? extends V2> zeroValueFactory, Procedure2<? super V2, ? super V> mutatingAggregator) { synchronized (this.getLock()) { return this.getDelegate().aggregateInPlaceBy(groupBy, zeroValueFactory, mutatingAggregator); } } public <K2, V2> MutableMap<K2, V2> aggregateBy( Function<? super V, ? extends K2> groupBy, Function0<? extends V2> zeroValueFactory, Function2<? super V2, ? super V, ? extends V2> nonMutatingAggregator) { synchronized (this.getLock()) { return this.getDelegate().aggregateBy(groupBy, zeroValueFactory, nonMutatingAggregator); } } public MutableMapIterable<V, K> flipUniqueValues() { synchronized (this.lock) { return this.getDelegate().flipUniqueValues(); } } public MutableSortedSetMultimap<V, K> flip() { synchronized (this.lock) { return this.getDelegate().flip(); } } public MutableSortedMap<K, V> select(Predicate2<? super K, ? super V> predicate) { synchronized (this.lock) { return this.getDelegate().select(predicate); } } public MutableSortedMap<K, V> reject(Predicate2<? super K, ? super V> predicate) { synchronized (this.lock) { return this.getDelegate().reject(predicate); } } public <K2, V2> MutableMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function) { synchronized (this.lock) { return this.getDelegate().collect(function); } } public <R> MutableSortedMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function) { synchronized (this.lock) { return this.getDelegate().collectValues(function); } } public RichIterable<K> keysView() { return LazyIterate.adapt(this.keySet()); } public RichIterable<V> valuesView() { return LazyIterate.adapt(this.values()); } public MutableSortedMap<K, V> asUnmodifiable() { synchronized (this.lock) { return UnmodifiableTreeMap.of(this); } } public MutableSortedMap<K, V> asSynchronized() { return this; } public ImmutableSortedMap<K, V> toImmutable() { synchronized (this.lock) { return SortedMaps.immutable.withSortedMap(this); } } public MutableSet<K> keySet() { synchronized (this.lock) { return SynchronizedMutableSet.of(this.getDelegate().keySet(), this.lock); } } public MutableCollection<V> values() { synchronized (this.lock) { return SynchronizedMutableCollection.of(this.getDelegate().values(), this.lock); } } public MutableSet<Entry<K, V>> entrySet() { synchronized (this.lock) { return SynchronizedMutableSet.of(this.getDelegate().entrySet(), this.lock); } } public MutableSortedMap<K, V> headMap(K toKey) { synchronized (this.lock) { return SynchronizedSortedMap.of(this.getDelegate().headMap(toKey), this.lock); } } public MutableSortedMap<K, V> tailMap(K fromKey) { synchronized (this.lock) { return SynchronizedSortedMap.of(this.getDelegate().tailMap(fromKey), this.lock); } } public MutableSortedMap<K, V> subMap(K fromKey, K toKey) { synchronized (this.lock) { return SynchronizedSortedMap.of(this.getDelegate().subMap(fromKey, toKey), this.lock); } } public void reverseForEach(Procedure<? super V> procedure) { synchronized (this.lock) { this.getDelegate().reverseForEach(procedure); } } public LazyIterable<V> asReversed() { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".asReversed() not implemented yet"); } public int detectLastIndex(Predicate<? super V> predicate) { synchronized (this.lock) { return this.getDelegate().detectLastIndex(predicate); } } public int indexOf(Object object) { synchronized (this.lock) { return this.getDelegate().indexOf(object); } } public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super V, ? super S> predicate) { synchronized (this.lock) { return this.getDelegate().corresponds(other, predicate); } } public void forEach(int startIndex, int endIndex, Procedure<? super V> procedure) { synchronized (this.lock) { this.getDelegate().forEach(startIndex, endIndex, procedure); } } public void forEachWithIndex(int fromIndex, int toIndex, ObjectIntProcedure<? super V> objectIntProcedure) { synchronized (this.lock) { this.getDelegate().forEachWithIndex(fromIndex, toIndex, objectIntProcedure); } } public MutableStack<V> toStack() { synchronized (this.lock) { return this.getDelegate().toStack(); } } public int detectIndex(Predicate<? super V> predicate) { synchronized (this.lock) { return this.getDelegate().detectIndex(predicate); } } public MutableSortedMap<K, V> toReversed() { synchronized (this.lock) { return this.getDelegate().toReversed(); } } public MutableSortedMap<K, V> take(int count) { synchronized (this.lock) { return this.getDelegate().take(count); } } public MutableSortedMap<K, V> takeWhile(Predicate<? super V> predicate) { synchronized (this.lock) { return this.getDelegate().takeWhile(predicate); } } public MutableSortedMap<K, V> drop(int count) { synchronized (this.lock) { return this.getDelegate().drop(count); } } public MutableSortedMap<K, V> dropWhile(Predicate<? super V> predicate) { synchronized (this.lock) { return this.getDelegate().dropWhile(predicate); } } public PartitionMutableList<V> partitionWhile(Predicate<? super V> predicate) { synchronized (this.lock) { return this.getDelegate().partitionWhile(predicate); } } public MutableList<V> distinct() { synchronized (this.lock) { return this.getDelegate().distinct(); } } }