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

org.eclipse.collections.impl.map.mutable.SynchronizedMutableMap Maven / Gradle / Ivy

Go to download

Builds the commons-text. Requires eclipse-collections-api be built first and be excluded from any other poms requiring it.

There is a newer version: 11.1.0-r13
Show newest version
/*
 * 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 map)
    {
        synchronized (this.lock)
        {
            this.putAll(map);
            return this;
        }
    }

    @Override
    public MutableMap withMapIterable(MapIterable mapIterable)
    {
        this.putAllMapIterable(mapIterable);
        return this;
    }

    @Override
    public void putAllMapIterable(MapIterable mapIterable)
    {
        synchronized (this.lock)
        {
            mapIterable.forEachKeyValue(this.getDelegate()::put);
        }
    }

    @Override
    public MutableMap withAllKeyValueArguments(Pair... keyValuePairs)
    {
        return this.withAllKeyValues(ArrayAdapter.adapt(keyValuePairs));
    }

    @Override
    public MutableMap withAllKeyValues(Iterable> keyValues)
    {
        synchronized (this.lock)
        {
            for (Pair 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 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 keyFunction,
            Function function)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().collectKeysAndValues(iterable, keyFunction, function);
        }
    }

    @Override
    public MutableMap select(Predicate2 predicate)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().select(predicate);
        }
    }

    @Override
    public MutableMap reject(Predicate2 predicate)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().reject(predicate);
        }
    }

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

    @Override
    public  MutableMap collectValues(Function2 function)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().collectValues(function);
        }
    }

    @Override
    public MutableMap tap(Procedure procedure)
    {
        return (MutableMap) super.tap(procedure);
    }

    @Override
    public MutableBag select(Predicate predicate)
    {
        return (MutableBag) super.select(predicate);
    }

    @Override
    public 

MutableBag selectWith(Predicate2 predicate, P parameter) { return (MutableBag) super.selectWith(predicate, parameter); } @Override public MutableBag reject(Predicate predicate) { return (MutableBag) super.reject(predicate); } @Override public

MutableBag rejectWith(Predicate2 predicate, P parameter) { return (MutableBag) super.rejectWith(predicate, parameter); } @Override public PartitionMutableBag partition(Predicate 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 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 function) { return (MutableBag) super.collect(function); } @Override public MutableBooleanBag collectBoolean(BooleanFunction booleanFunction) { return (MutableBooleanBag) super.collectBoolean(booleanFunction); } @Override public MutableByteBag collectByte(ByteFunction byteFunction) { return (MutableByteBag) super.collectByte(byteFunction); } @Override public MutableCharBag collectChar(CharFunction charFunction) { return (MutableCharBag) super.collectChar(charFunction); } @Override public MutableDoubleBag collectDouble(DoubleFunction doubleFunction) { return (MutableDoubleBag) super.collectDouble(doubleFunction); } @Override public MutableFloatBag collectFloat(FloatFunction floatFunction) { return (MutableFloatBag) super.collectFloat(floatFunction); } @Override public MutableIntBag collectInt(IntFunction intFunction) { return (MutableIntBag) super.collectInt(intFunction); } @Override public MutableLongBag collectLong(LongFunction longFunction) { return (MutableLongBag) super.collectLong(longFunction); } @Override public MutableShortBag collectShort(ShortFunction shortFunction) { return (MutableShortBag) super.collectShort(shortFunction); } @Override public MutableBag collectWith(Function2 function, P parameter) { return (MutableBag) super.collectWith(function, parameter); } @Override public MutableBag collectIf(Predicate predicate, Function function) { return (MutableBag) super.collectIf(predicate, function); } @Override public MutableBag flatCollect(Function> function) { return (MutableBag) super.flatCollect(function); } @Override public MutableBagMultimap groupBy(Function function) { return (MutableBagMultimap) super.groupBy(function); } @Override public MutableBagMultimap groupByEach(Function> function) { return (MutableBagMultimap) super.groupByEach(function); } /** * @since 11.0 */ @Override public MutableMap aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { return (MutableMap) super.aggregateBy(groupBy, zeroValueFactory, nonMutatingAggregator); } /** * @since 11.0 */ @Override public MutableMap aggregateBy( Function keyFunction, Function valueFunction, Function0 zeroValueFactory, Function2 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 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); } } }