org.eclipse.collections.impl.map.mutable.SynchronizedMutableMap Maven / Gradle / Ivy
/*
* Copyright (c) 2015 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.map.mutable;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.MutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.MutableByteBag;
import org.eclipse.collections.api.bag.primitive.MutableCharBag;
import org.eclipse.collections.api.bag.primitive.MutableDoubleBag;
import org.eclipse.collections.api.bag.primitive.MutableFloatBag;
import org.eclipse.collections.api.bag.primitive.MutableIntBag;
import org.eclipse.collections.api.bag.primitive.MutableLongBag;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.multimap.bag.MutableBagMultimap;
import org.eclipse.collections.api.multimap.set.MutableSetMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.bag.PartitionMutableBag;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.collection.mutable.SynchronizedMutableCollection;
import org.eclipse.collections.impl.factory.Maps;
import org.eclipse.collections.impl.list.fixed.ArrayAdapter;
import org.eclipse.collections.impl.map.AbstractSynchronizedMapIterable;
import org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet;
import org.eclipse.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 AbstractSynchronizedMapIterable implements MutableMap, Serializable
{
private static final long serialVersionUID = 2L;
public SynchronizedMutableMap(MutableMap newMap)
{
super(newMap);
}
public 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));
}
/**
* 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. Additionally, a developer specifies which lock to use
* with the collection.
*/
public static > SynchronizedMutableMap of(M map, Object lock)
{
if (map == null)
{
throw new IllegalArgumentException("cannot create a SynchronizedMutableMap for null");
}
return new SynchronizedMutableMap(MapAdapter.adapt(map), lock);
}
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.getDelegate().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.getDelegate().removeKey(key);
}
return this;
}
}
public MutableMap newEmpty()
{
synchronized (this.lock)
{
return this.getDelegate().newEmpty();
}
}
@Override
public MutableMap clone()
{
synchronized (this.lock)
{
return SynchronizedMutableMap.of(this.getDelegate().clone());
}
}
protected Object writeReplace()
{
return new SynchronizedMapSerializationProxy(this.getDelegate());
}
@Override
protected MutableMap getDelegate()
{
return (MutableMap) super.getDelegate();
}
public MutableMap collectKeysAndValues(
Iterable iterable,
Function super E, ? extends K> keyFunction,
Function super E, ? extends V> function)
{
synchronized (this.lock)
{
return this.getDelegate().collectKeysAndValues(iterable, keyFunction, function);
}
}
public MutableMap select(Predicate2 super K, ? super V> predicate)
{
synchronized (this.lock)
{
return this.getDelegate().select(predicate);
}
}
public MutableMap reject(Predicate2 super K, ? super V> predicate)
{
synchronized (this.lock)
{
return this.getDelegate().reject(predicate);
}
}
public MutableMap collect(Function2 super K, ? super V, Pair> pairFunction)
{
synchronized (this.lock)
{
return this.getDelegate().collect(pairFunction);
}
}
public MutableMap collectValues(Function2 super K, ? super V, ? extends R> function)
{
synchronized (this.lock)
{
return this.getDelegate().collectValues(function);
}
}
public MutableMap tap(Procedure super V> procedure)
{
synchronized (this.lock)
{
this.forEach(procedure);
return this;
}
}
public MutableBag select(Predicate super V> predicate)
{
synchronized (this.lock)
{
return this.getDelegate().select(predicate);
}
}
public MutableBag selectWith(Predicate2 super V, ? super P> predicate, P parameter)
{
synchronized (this.lock)
{
return this.getDelegate().selectWith(predicate, parameter);
}
}
public MutableBag reject(Predicate super V> predicate)
{
synchronized (this.lock)
{
return this.getDelegate().reject(predicate);
}
}
public MutableBag rejectWith(Predicate2 super V, ? super P> predicate, P parameter)
{
synchronized (this.lock)
{
return this.getDelegate().rejectWith(predicate, parameter);
}
}
public PartitionMutableBag partition(Predicate super V> predicate)
{
synchronized (this.lock)
{
return this.getDelegate().partition(predicate);
}
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead.
*/
@Deprecated
public MutableSet> zipWithIndex()
{
synchronized (this.lock)
{
return this.getDelegate().zipWithIndex();
}
}
public PartitionMutableBag partitionWith(Predicate2 super V, ? super P> predicate, P parameter)
{
synchronized (this.lock)
{
return this.getDelegate().partitionWith(predicate, parameter);
}
}
public MutableBag selectInstancesOf(Class clazz)
{
synchronized (this.lock)
{
return this.getDelegate().selectInstancesOf(clazz);
}
}
public MutableBag collect(Function super V, ? extends A> function)
{
synchronized (this.lock)
{
return this.getDelegate().collect(function);
}
}
public MutableBooleanBag collectBoolean(BooleanFunction super V> booleanFunction)
{
synchronized (this.lock)
{
return this.getDelegate().collectBoolean(booleanFunction);
}
}
public MutableByteBag collectByte(ByteFunction super V> byteFunction)
{
synchronized (this.lock)
{
return this.getDelegate().collectByte(byteFunction);
}
}
public MutableCharBag collectChar(CharFunction super V> charFunction)
{
synchronized (this.lock)
{
return this.getDelegate().collectChar(charFunction);
}
}
public MutableDoubleBag collectDouble(DoubleFunction super V> doubleFunction)
{
synchronized (this.lock)
{
return this.getDelegate().collectDouble(doubleFunction);
}
}
public MutableFloatBag collectFloat(FloatFunction super V> floatFunction)
{
synchronized (this.lock)
{
return this.getDelegate().collectFloat(floatFunction);
}
}
public MutableIntBag collectInt(IntFunction super V> intFunction)
{
synchronized (this.lock)
{
return this.getDelegate().collectInt(intFunction);
}
}
public MutableLongBag collectLong(LongFunction super V> longFunction)
{
synchronized (this.lock)
{
return this.getDelegate().collectLong(longFunction);
}
}
public MutableShortBag collectShort(ShortFunction super V> shortFunction)
{
synchronized (this.lock)
{
return this.getDelegate().collectShort(shortFunction);
}
}
public MutableBag collectWith(Function2 super V, ? super P, ? extends A> function, P parameter)
{
synchronized (this.lock)
{
return this.getDelegate().collectWith(function, parameter);
}
}
public MutableBag collectIf(Predicate super V> predicate, Function super V, ? extends A> function)
{
synchronized (this.lock)
{
return this.getDelegate().collectIf(predicate, function);
}
}
public MutableBag flatCollect(Function super V, ? extends Iterable> function)
{
synchronized (this.lock)
{
return this.getDelegate().flatCollect(function);
}
}
public MutableBagMultimap groupBy(Function super V, ? extends KK> function)
{
synchronized (this.lock)
{
return this.getDelegate().groupBy(function);
}
}
public MutableBagMultimap groupByEach(Function super V, ? extends Iterable> function)
{
synchronized (this.lock)
{
return this.getDelegate().groupByEach(function);
}
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead.
*/
@Deprecated
public MutableBag> zip(Iterable that)
{
synchronized (this.lock)
{
return this.getDelegate().zip(that);
}
}
public MutableMap aggregateInPlaceBy(
Function super V, ? extends K2> groupBy,
Function0 extends V2> zeroValueFactory,
Procedure2 super V2, ? super V> mutatingAggregator)
{
synchronized (this.lock)
{
return this.getDelegate().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.getDelegate().aggregateBy(groupBy, zeroValueFactory, nonMutatingAggregator);
}
}
public MutableMap flipUniqueValues()
{
synchronized (this.lock)
{
return this.getDelegate().flipUniqueValues();
}
}
public MutableSetMultimap flip()
{
synchronized (this.lock)
{
return this.getDelegate().flip();
}
}
public Set keySet()
{
synchronized (this.lock)
{
return SynchronizedMutableSet.of(this.getDelegate().keySet(), this.lock);
}
}
public Collection values()
{
synchronized (this.lock)
{
return SynchronizedMutableCollection.of(this.getDelegate().values(), this.lock);
}
}
public Set> entrySet()
{
synchronized (this.lock)
{
return SynchronizedMutableSet.of(this.getDelegate().entrySet(), this.lock);
}
}
public RichIterable keysView()
{
return LazyIterate.adapt(this.keySet());
}
public RichIterable valuesView()
{
return LazyIterate.adapt(this.values());
}
public MutableMap asUnmodifiable()
{
synchronized (this.lock)
{
return UnmodifiableMutableMap.of(this);
}
}
public MutableMap asSynchronized()
{
return this;
}
public ImmutableMap toImmutable()
{
synchronized (this.lock)
{
return Maps.immutable.withAll(this);
}
}
}