org.eclipse.collections.impl.map.mutable.SynchronizedMutableMap Maven / Gradle / Ivy
Show all versions of eclipse-collections Show documentation
/*
* Copyright (c) 2022 Goldman Sachs and others.
* 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.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.factory.Maps;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MapIterable;
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.list.fixed.ArrayAdapter;
import org.eclipse.collections.impl.map.AbstractSynchronizedMapIterable;
import org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet;
/**
* 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-Eclipse-Collections 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-Eclipse-Collections 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);
}
@Override
public MutableMap withKeyValue(K key, V value)
{
synchronized (this.lock)
{
this.put(key, value);
return this;
}
}
@Override
public MutableMap withMap(Map extends K, ? extends V> map)
{
synchronized (this.lock)
{
this.putAll(map);
return this;
}
}
@Override
public MutableMap withMapIterable(MapIterable extends K, ? extends V> mapIterable)
{
this.putAllMapIterable(mapIterable);
return this;
}
@Override
public void putAllMapIterable(MapIterable extends K, ? extends V> mapIterable)
{
synchronized (this.lock)
{
mapIterable.forEachKeyValue(this.getDelegate()::put);
}
}
@Override
public MutableMap withAllKeyValueArguments(Pair extends K, ? extends V>... keyValuePairs)
{
return this.withAllKeyValues(ArrayAdapter.adapt(keyValuePairs));
}
@Override
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;
}
}
@Override
public MutableMap withoutKey(K key)
{
synchronized (this.lock)
{
this.remove(key);
return this;
}
}
@Override
public MutableMap withoutAllKeys(Iterable extends K> keys)
{
synchronized (this.lock)
{
for (K key : keys)
{
this.getDelegate().removeKey(key);
}
return this;
}
}
@Override
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();
}
@Override
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);
}
}
@Override
public MutableMap select(Predicate2 super K, ? super V> predicate)
{
synchronized (this.lock)
{
return this.getDelegate().select(predicate);
}
}
@Override
public MutableMap reject(Predicate2 super K, ? super V> predicate)
{
synchronized (this.lock)
{
return this.getDelegate().reject(predicate);
}
}
@Override
public MutableMap collect(Function2 super K, ? super V, Pair> pairFunction)
{
synchronized (this.lock)
{
return this.getDelegate().collect(pairFunction);
}
}
@Override
public MutableMap collectValues(Function2 super K, ? super V, ? extends R> function)
{
synchronized (this.lock)
{
return this.getDelegate().collectValues(function);
}
}
@Override
public MutableMap tap(Procedure super V> procedure)
{
return (MutableMap) super.tap(procedure);
}
@Override
public MutableBag select(Predicate super V> predicate)
{
return (MutableBag) super.select(predicate);
}
@Override
public MutableBag selectWith(Predicate2 super V, ? super P> predicate, P parameter)
{
return (MutableBag) super.selectWith(predicate, parameter);
}
@Override
public MutableBag reject(Predicate super V> predicate)
{
return (MutableBag) super.reject(predicate);
}
@Override
public MutableBag rejectWith(Predicate2 super V, ? super P> predicate, P parameter)
{
return (MutableBag) super.rejectWith(predicate, parameter);
}
@Override
public PartitionMutableBag partition(Predicate super V> predicate)
{
return (PartitionMutableBag) super.partition(predicate);
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead.
*/
@Override
@Deprecated
public MutableSet> zipWithIndex()
{
return (MutableSet>) super.zipWithIndex();
}
@Override
public PartitionMutableBag partitionWith(Predicate2 super V, ? super P> predicate, P parameter)
{
return (PartitionMutableBag) super.partitionWith(predicate, parameter);
}
@Override
public MutableBag selectInstancesOf(Class clazz)
{
return (MutableBag) super.selectInstancesOf(clazz);
}
@Override
public MutableBag collect(Function super V, ? extends A> function)
{
return (MutableBag) super.collect(function);
}
@Override
public MutableBooleanBag collectBoolean(BooleanFunction super V> booleanFunction)
{
return (MutableBooleanBag) super.collectBoolean(booleanFunction);
}
@Override
public MutableByteBag collectByte(ByteFunction super V> byteFunction)
{
return (MutableByteBag) super.collectByte(byteFunction);
}
@Override
public MutableCharBag collectChar(CharFunction super V> charFunction)
{
return (MutableCharBag) super.collectChar(charFunction);
}
@Override
public MutableDoubleBag collectDouble(DoubleFunction super V> doubleFunction)
{
return (MutableDoubleBag) super.collectDouble(doubleFunction);
}
@Override
public MutableFloatBag collectFloat(FloatFunction super V> floatFunction)
{
return (MutableFloatBag) super.collectFloat(floatFunction);
}
@Override
public MutableIntBag collectInt(IntFunction super V> intFunction)
{
return (MutableIntBag) super.collectInt(intFunction);
}
@Override
public MutableLongBag collectLong(LongFunction super V> longFunction)
{
return (MutableLongBag) super.collectLong(longFunction);
}
@Override
public MutableShortBag collectShort(ShortFunction super V> shortFunction)
{
return (MutableShortBag) super.collectShort(shortFunction);
}
@Override
public MutableBag collectWith(Function2 super V, ? super P, ? extends A> function, P parameter)
{
return (MutableBag) super.collectWith(function, parameter);
}
@Override
public MutableBag collectIf(Predicate super V> predicate, Function super V, ? extends A> function)
{
return (MutableBag) super.collectIf(predicate, function);
}
@Override
public MutableBag flatCollect(Function super V, ? extends Iterable> function)
{
return (MutableBag) super.flatCollect(function);
}
@Override
public MutableBagMultimap groupBy(Function super V, ? extends KK> function)
{
return (MutableBagMultimap) super.groupBy(function);
}
@Override
public MutableBagMultimap groupByEach(Function super V, ? extends Iterable> function)
{
return (MutableBagMultimap) super.groupByEach(function);
}
/**
* @since 11.0
*/
@Override
public MutableMap aggregateBy(
Function super V, ? extends KK> groupBy,
Function0 extends VV> zeroValueFactory,
Function2 super VV, ? super V, ? extends VV> nonMutatingAggregator)
{
return (MutableMap) super.aggregateBy(groupBy, zeroValueFactory, nonMutatingAggregator);
}
/**
* @since 11.0
*/
@Override
public MutableMap aggregateBy(
Function super K, ? extends K1> keyFunction,
Function super V, ? extends V1> valueFunction,
Function0 extends V2> zeroValueFactory,
Function2 super V2, ? super V1, ? extends V2> nonMutatingAggregator)
{
return (MutableMap) super.aggregateBy(keyFunction, valueFunction, zeroValueFactory, nonMutatingAggregator);
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead.
*/
@Override
@Deprecated
public MutableBag> zip(Iterable that)
{
return (MutableBag>) super.zip(that);
}
@Override
public MutableMap groupByUniqueKey(Function super V, ? extends VV> function)
{
return (MutableMap) super.groupByUniqueKey(function);
}
@Override
public MutableMap flipUniqueValues()
{
synchronized (this.lock)
{
return this.getDelegate().flipUniqueValues();
}
}
@Override
public MutableSetMultimap flip()
{
synchronized (this.lock)
{
return this.getDelegate().flip();
}
}
@Override
public Set keySet()
{
synchronized (this.lock)
{
return SynchronizedMutableSet.of(this.getDelegate().keySet(), this.lock);
}
}
@Override
public Collection values()
{
synchronized (this.lock)
{
return SynchronizedMutableCollection.of(this.getDelegate().values(), this.lock);
}
}
@Override
public Set> entrySet()
{
synchronized (this.lock)
{
return SynchronizedMutableSet.of(this.getDelegate().entrySet(), this.lock);
}
}
@Override
public MutableMap asUnmodifiable()
{
synchronized (this.lock)
{
return UnmodifiableMutableMap.of(this);
}
}
@Override
public MutableMap asSynchronized()
{
return this;
}
@Override
public ImmutableMap toImmutable()
{
synchronized (this.lock)
{
return Maps.immutable.withAll(this);
}
}
}