com.gs.collections.impl.map.mutable.SynchronizedMutableMap Maven / Gradle / Ivy
Show all versions of gs-collections Show documentation
/*
* Copyright 2014 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.mutable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
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.Procedure2;
import com.gs.collections.api.collection.MutableCollection;
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
import com.gs.collections.api.collection.primitive.MutableByteCollection;
import com.gs.collections.api.collection.primitive.MutableCharCollection;
import com.gs.collections.api.collection.primitive.MutableDoubleCollection;
import com.gs.collections.api.collection.primitive.MutableFloatCollection;
import com.gs.collections.api.collection.primitive.MutableIntCollection;
import com.gs.collections.api.collection.primitive.MutableLongCollection;
import com.gs.collections.api.collection.primitive.MutableShortCollection;
import com.gs.collections.api.map.ImmutableMap;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.multimap.set.MutableSetMultimap;
import com.gs.collections.api.partition.PartitionMutableCollection;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.collection.mutable.SynchronizedMutableCollection;
import com.gs.collections.impl.factory.Maps;
import com.gs.collections.impl.list.fixed.ArrayAdapter;
import com.gs.collections.impl.map.SynchronizedMapIterable;
import com.gs.collections.impl.set.mutable.SynchronizedMutableSet;
import com.gs.collections.impl.tuple.AbstractImmutableEntry;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.LazyIterate;
/**
* A synchronized view of a {@link MutableMap}. It is imperative that the user manually synchronize on the collection when iterating over it using the
* standard JDK iterator or JDK 5 for loop, as per {@link Collections#synchronizedCollection(Collection)}.
*
* @see MutableMap#asSynchronized()
*/
public class SynchronizedMutableMap
extends SynchronizedMapIterable implements MutableMap
{
private static final long serialVersionUID = 1L;
protected SynchronizedMutableMap(MutableMap newMap)
{
super(newMap);
}
protected SynchronizedMutableMap(MutableMap newMap, Object newLock)
{
super(newMap, newLock);
}
/**
* This method will take a MutableMap and wrap it directly in a SynchronizedMutableMap. It will
* take any other non-GS-map and first adapt it will a MapAdapter, and then return a
* SynchronizedMutableMap that wraps the adapter.
*/
public static > SynchronizedMutableMap of(M map)
{
if (map == null)
{
throw new IllegalArgumentException("cannot create a SynchronizedMutableMap for null");
}
return new SynchronizedMutableMap(MapAdapter.adapt(map));
}
public MutableSetMultimap flip()
{
synchronized (this.lock)
{
return this.getMutableMap().flip();
}
}
public MutableMap getMutableMap()
{
return (MutableMap) this.getMap();
}
public MutableMap select(Predicate2 super K, ? super V> predicate)
{
synchronized (this.lock)
{
return this.getMutableMap().select(predicate);
}
}
public MutableMap collectValues(Function2 super K, ? super V, ? extends R> function)
{
synchronized (this.lock)
{
return this.getMutableMap().collectValues(function);
}
}
public MutableMap collect(Function2 super K, ? super V, Pair> pairFunction)
{
synchronized (this.lock)
{
return this.getMutableMap().collect(pairFunction);
}
}
public MutableMap reject(Predicate2 super K, ? super V> predicate)
{
synchronized (this.lock)
{
return this.getMutableMap().reject(predicate);
}
}
public MutableCollection select(Predicate super V> predicate)
{
synchronized (this.lock)
{
return this.getMutableMap().select(predicate);
}
}
public MutableCollection reject(Predicate super V> predicate)
{
synchronized (this.lock)
{
return this.getMutableMap().reject(predicate);
}
}
public PartitionMutableCollection partition(Predicate super V> predicate)
{
synchronized (this.lock)
{
return this.getMutableMap().partition(predicate);
}
}
public PartitionMutableCollection partitionWith(Predicate2 super V, ? super P> predicate, P parameter)
{
synchronized (this.lock)
{
return this.getMutableMap().partitionWith(predicate, parameter);
}
}
public MutableCollection selectInstancesOf(Class clazz)
{
synchronized (this.lock)
{
return this.getMutableMap().selectInstancesOf(clazz);
}
}
public MutableCollection> zip(Iterable that)
{
synchronized (this.lock)
{
return this.getMutableMap().zip(that);
}
}
public MutableCollection> zipWithIndex()
{
synchronized (this.lock)
{
return this.getMutableMap().zipWithIndex();
}
}
public MutableCollection flatCollect(Function super V, ? extends Iterable> function)
{
synchronized (this.lock)
{
return this.getMutableMap().flatCollect(function);
}
}
public MutableCollection collectIf(Predicate super V> predicate, Function super V, ? extends A> function)
{
synchronized (this.lock)
{
return this.getMutableMap().collectIf(predicate, function);
}
}
public MutableCollection collect(Function super V, ? extends A> function)
{
synchronized (this.lock)
{
return this.getMutableMap().collect(function);
}
}
public MutableBooleanCollection collectBoolean(BooleanFunction super V> booleanFunction)
{
synchronized (this.lock)
{
return this.getMutableMap().collectBoolean(booleanFunction);
}
}
public MutableByteCollection collectByte(ByteFunction super V> byteFunction)
{
synchronized (this.lock)
{
return this.getMutableMap().collectByte(byteFunction);
}
}
public MutableCharCollection collectChar(CharFunction super V> charFunction)
{
synchronized (this.lock)
{
return this.getMutableMap().collectChar(charFunction);
}
}
public MutableDoubleCollection collectDouble(DoubleFunction super V> doubleFunction)
{
synchronized (this.lock)
{
return this.getMutableMap().collectDouble(doubleFunction);
}
}
public MutableFloatCollection collectFloat(FloatFunction super V> floatFunction)
{
synchronized (this.lock)
{
return this.getMutableMap().collectFloat(floatFunction);
}
}
public MutableIntCollection collectInt(IntFunction super V> intFunction)
{
synchronized (this.lock)
{
return this.getMutableMap().collectInt(intFunction);
}
}
public MutableLongCollection collectLong(LongFunction super V> longFunction)
{
synchronized (this.lock)
{
return this.getMutableMap().collectLong(longFunction);
}
}
public MutableShortCollection collectShort(ShortFunction super V> shortFunction)
{
synchronized (this.lock)
{
return this.getMutableMap().collectShort(shortFunction);
}
}
public MutableMultimap groupBy(Function super V, ? extends KK> function)
{
synchronized (this.lock)
{
return this.getMutableMap().groupBy(function);
}
}
public MutableMultimap groupByEach(Function super V, ? extends Iterable> function)
{
synchronized (this.lock)
{
return this.getMutableMap().groupByEach(function);
}
}
public MutableMap groupByUniqueKey(Function super V, ? extends VV> function)
{
synchronized (this.lock)
{
return this.getMutableMap().groupByUniqueKey(function);
}
}
public MutableMap newEmpty()
{
synchronized (this.lock)
{
return this.getMutableMap().newEmpty();
}
}
public MutableMap collectKeysAndValues(
Iterable iterable,
Function super E, ? extends K> keyFunction,
Function super E, ? extends V> function)
{
synchronized (this.lock)
{
return this.getMutableMap().collectKeysAndValues(iterable, keyFunction, function);
}
}
public V removeKey(K key)
{
synchronized (this.lock)
{
return this.getMutableMap().removeKey(key);
}
}
public V getIfAbsentPut(K key, Function0 extends V> function)
{
synchronized (this.lock)
{
return this.getMutableMap().getIfAbsentPut(key, function);
}
}
public V getIfAbsentPut(K key, V value)
{
synchronized (this.lock)
{
return this.getMutableMap().getIfAbsentPut(key, value);
}
}
public V getIfAbsentPutWithKey(K key, Function super K, ? extends V> function)
{
synchronized (this.lock)
{
return this.getMutableMap().getIfAbsentPutWithKey(key, function);
}
}
public V getIfAbsentPutWith(K key, Function super P, ? extends V> function, P parameter)
{
synchronized (this.lock)
{
return this.getMutableMap().getIfAbsentPutWith(key, function, parameter);
}
}
@Override
public MutableMap clone()
{
synchronized (this.lock)
{
return of(this.getMutableMap().clone());
}
}
@Override
public boolean equals(Object o)
{
synchronized (this.lock)
{
return this.getMutableMap().equals(o);
}
}
@Override
public int hashCode()
{
synchronized (this.lock)
{
return this.getMutableMap().hashCode();
}
}
@Override
public String toString()
{
synchronized (this.lock)
{
return this.getMutableMap().toString();
}
}
public MutableMap asUnmodifiable()
{
synchronized (this.lock)
{
return UnmodifiableMutableMap.of(this);
}
}
public ImmutableMap toImmutable()
{
synchronized (this.lock)
{
return Maps.immutable.ofAll(this);
}
}
public MutableMap asSynchronized()
{
return this;
}
public RichIterable> keyValuesView()
{
synchronized (this.lock)
{
Set> entries = this.getMutableMap().entrySet();
Iterable> pairs = Iterate.collect(entries, AbstractImmutableEntry.getPairFunction());
return LazyIterate.adapt(pairs);
}
}
public MutableMap flipUniqueValues()
{
synchronized (this.lock)
{
return this.getMutableMap().flipUniqueValues();
}
}
@Override
public RichIterable keysView()
{
return LazyIterate.adapt(this.keySet());
}
@Override
public RichIterable valuesView()
{
return LazyIterate.adapt(this.values());
}
public V put(K key, V value)
{
synchronized (this.lock)
{
return this.getMutableMap().put(key, value);
}
}
public V remove(Object key)
{
synchronized (this.lock)
{
return this.getMutableMap().remove(key);
}
}
public void putAll(Map extends K, ? extends V> map)
{
synchronized (this.lock)
{
this.getMutableMap().putAll(map);
}
}
public void clear()
{
synchronized (this.lock)
{
this.getMutableMap().clear();
}
}
public Set keySet()
{
synchronized (this.lock)
{
return SynchronizedMutableSet.of(this.getMutableMap().keySet(), this.lock);
}
}
public Collection values()
{
synchronized (this.lock)
{
return SynchronizedMutableCollection.of(this.getMutableMap().values(), this.lock);
}
}
public Set> entrySet()
{
synchronized (this.lock)
{
return SynchronizedMutableSet.of(this.getMutableMap().entrySet(), this.lock);
}
}
public V add(Pair keyValuePair)
{
synchronized (this.lock)
{
return this.put(keyValuePair.getOne(), keyValuePair.getTwo());
}
}
public MutableMap withKeyValue(K key, V value)
{
synchronized (this.lock)
{
this.put(key, value);
return this;
}
}
public MutableMap withAllKeyValueArguments(Pair extends K, ? extends V>... keyValuePairs)
{
return this.withAllKeyValues(ArrayAdapter.adapt(keyValuePairs));
}
public MutableMap withAllKeyValues(Iterable extends Pair extends K, ? extends V>> keyValues)
{
synchronized (this.lock)
{
for (Pair extends K, ? extends V> keyValue : keyValues)
{
this.getMutableMap().put(keyValue.getOne(), keyValue.getTwo());
}
return this;
}
}
public MutableMap withoutKey(K key)
{
this.remove(key);
return this;
}
public MutableMap withoutAllKeys(Iterable extends K> keys)
{
synchronized (this.lock)
{
for (K key : keys)
{
this.getMutableMap().removeKey(key);
}
return this;
}
}
public MutableMap aggregateInPlaceBy(
Function super V, ? extends K2> groupBy,
Function0 extends V2> zeroValueFactory,
Procedure2 super V2, ? super V> mutatingAggregator)
{
synchronized (this.lock)
{
return this.getMutableMap().aggregateInPlaceBy(groupBy, zeroValueFactory, mutatingAggregator);
}
}
public MutableMap aggregateBy(
Function super V, ? extends K2> groupBy,
Function0 extends V2> zeroValueFactory,
Function2 super V2, ? super V, ? extends V2> nonMutatingAggregator)
{
synchronized (this.lock)
{
return this.getMutableMap().aggregateBy(groupBy, zeroValueFactory, nonMutatingAggregator);
}
}
public V updateValue(K key, Function0 extends V> factory, Function super V, ? extends V> function)
{
synchronized (this.lock)
{
return this.getMutableMap().updateValue(key, factory, function);
}
}
public V updateValueWith(
K key,
Function0 extends V> factory,
Function2 super V, ? super P, ? extends V> function,
P parameter)
{
synchronized (this.lock)
{
return this.getMutableMap().updateValueWith(key, factory, function, parameter);
}
}
}