All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.collections.impl.map.mutable.SynchronizedMutableMap Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * 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 predicate)
    {
        synchronized (this.lock)
        {
            return this.getMutableMap().select(predicate);
        }
    }

    public  MutableMap collectValues(Function2 function)
    {
        synchronized (this.lock)
        {
            return this.getMutableMap().collectValues(function);
        }
    }

    public  MutableMap collect(Function2> pairFunction)
    {
        synchronized (this.lock)
        {
            return this.getMutableMap().collect(pairFunction);
        }
    }

    public MutableMap reject(Predicate2 predicate)
    {
        synchronized (this.lock)
        {
            return this.getMutableMap().reject(predicate);
        }
    }

    public MutableCollection select(Predicate predicate)
    {
        synchronized (this.lock)
        {
            return this.getMutableMap().select(predicate);
        }
    }

    public MutableCollection reject(Predicate predicate)
    {
        synchronized (this.lock)
        {
            return this.getMutableMap().reject(predicate);
        }
    }

    public PartitionMutableCollection partition(Predicate predicate)
    {
        synchronized (this.lock)
        {
            return this.getMutableMap().partition(predicate);
        }
    }

    public 

PartitionMutableCollection partitionWith(Predicate2 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> function) { synchronized (this.lock) { return this.getMutableMap().flatCollect(function); } } public MutableCollection collectIf(Predicate predicate, Function function) { synchronized (this.lock) { return this.getMutableMap().collectIf(predicate, function); } } public MutableCollection collect(Function function) { synchronized (this.lock) { return this.getMutableMap().collect(function); } } public MutableBooleanCollection collectBoolean(BooleanFunction booleanFunction) { synchronized (this.lock) { return this.getMutableMap().collectBoolean(booleanFunction); } } public MutableByteCollection collectByte(ByteFunction byteFunction) { synchronized (this.lock) { return this.getMutableMap().collectByte(byteFunction); } } public MutableCharCollection collectChar(CharFunction charFunction) { synchronized (this.lock) { return this.getMutableMap().collectChar(charFunction); } } public MutableDoubleCollection collectDouble(DoubleFunction doubleFunction) { synchronized (this.lock) { return this.getMutableMap().collectDouble(doubleFunction); } } public MutableFloatCollection collectFloat(FloatFunction floatFunction) { synchronized (this.lock) { return this.getMutableMap().collectFloat(floatFunction); } } public MutableIntCollection collectInt(IntFunction intFunction) { synchronized (this.lock) { return this.getMutableMap().collectInt(intFunction); } } public MutableLongCollection collectLong(LongFunction longFunction) { synchronized (this.lock) { return this.getMutableMap().collectLong(longFunction); } } public MutableShortCollection collectShort(ShortFunction shortFunction) { synchronized (this.lock) { return this.getMutableMap().collectShort(shortFunction); } } public MutableMultimap groupBy(Function function) { synchronized (this.lock) { return this.getMutableMap().groupBy(function); } } public MutableMultimap groupByEach(Function> function) { synchronized (this.lock) { return this.getMutableMap().groupByEach(function); } } public MutableMap groupByUniqueKey(Function 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 keyFunction, Function 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 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 function) { synchronized (this.lock) { return this.getMutableMap().getIfAbsentPutWithKey(key, function); } } public

V getIfAbsentPutWith(K key, Function 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 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... keyValuePairs) { return this.withAllKeyValues(ArrayAdapter.adapt(keyValuePairs)); } public MutableMap withAllKeyValues(Iterable> keyValues) { synchronized (this.lock) { for (Pair 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 keys) { synchronized (this.lock) { for (K key : keys) { this.getMutableMap().removeKey(key); } return this; } } public MutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { synchronized (this.lock) { return this.getMutableMap().aggregateInPlaceBy(groupBy, zeroValueFactory, mutatingAggregator); } } public MutableMap aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { synchronized (this.lock) { return this.getMutableMap().aggregateBy(groupBy, zeroValueFactory, nonMutatingAggregator); } } public V updateValue(K key, Function0 factory, Function function) { synchronized (this.lock) { return this.getMutableMap().updateValue(key, factory, function); } } public

V updateValueWith( K key, Function0 factory, Function2 function, P parameter) { synchronized (this.lock) { return this.getMutableMap().updateValueWith(key, factory, function, parameter); } } }