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

com.gs.collections.impl.map.mutable.ConcurrentMutableHashMap 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 2015 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.io.Serializable;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

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.procedure.Procedure;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.map.ConcurrentMutableMap;
import com.gs.collections.api.map.ImmutableMap;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.block.procedure.MapEntryToProcedure2;
import com.gs.collections.impl.factory.Maps;
import com.gs.collections.impl.tuple.ImmutableEntry;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.internal.IterableIterate;

/**
 * A simple concurrent implementation of MutableMap which uses java.util.concurrent.ConcurrentHashMap for its underlying
 * concurrent Map implementation.
 *
 * @see com.gs.collections.impl.map.mutable.ConcurrentHashMap
 * @deprecated since 2.0
 */
@Deprecated
public final class ConcurrentMutableHashMap
        extends AbstractMutableMap
        implements ConcurrentMutableMap, Serializable
{
    private static final long serialVersionUID = 1L;
    private static final String JAVA_SPECIFICATION_VERSION = System.getProperty("java.specification.version");

    private final ConcurrentMap delegate;

    private ConcurrentMutableHashMap()
    {
        this(new ConcurrentHashMap());
    }

    public ConcurrentMutableHashMap(ConcurrentMap delegate)
    {
        this.delegate = delegate;
    }

    public static  ConcurrentMutableHashMap newMap()
    {
        return new ConcurrentMutableHashMap();
    }

    public static  ConcurrentMutableHashMap newMap(int initialCapacity)
    {
        return new ConcurrentMutableHashMap(new ConcurrentHashMap(initialCapacity));
    }

    public static  ConcurrentMutableHashMap newMap(int initialCapacity, float loadFactor, int concurrencyLevel)
    {
        return new ConcurrentMutableHashMap(new ConcurrentHashMap(initialCapacity, loadFactor, concurrencyLevel));
    }

    public static  ConcurrentMutableHashMap newMap(Map map)
    {
        return new ConcurrentMutableHashMap(new ConcurrentHashMap(map));
    }

    @Override
    public ConcurrentMutableHashMap withKeyValue(K key, V value)
    {
        return (ConcurrentMutableHashMap) super.withKeyValue(key, value);
    }

    @Override
    public ConcurrentMutableHashMap withAllKeyValues(Iterable> keyValues)
    {
        return (ConcurrentMutableHashMap) super.withAllKeyValues(keyValues);
    }

    @Override
    public ConcurrentMutableHashMap withAllKeyValueArguments(Pair... keyValues)
    {
        return (ConcurrentMutableHashMap) super.withAllKeyValueArguments(keyValues);
    }

    @Override
    public ConcurrentMutableHashMap withoutKey(K key)
    {
        return (ConcurrentMutableHashMap) super.withoutKey(key);
    }

    @Override
    public ConcurrentMutableHashMap withoutAllKeys(Iterable keys)
    {
        return (ConcurrentMutableHashMap) super.withoutAllKeys(keys);
    }

    @Override
    public String toString()
    {
        return this.delegate.toString();
    }

    @Override
    public MutableMap clone()
    {
        return ConcurrentMutableHashMap.newMap(this.delegate);
    }

    @Override
    public  MutableMap newEmpty(int capacity)
    {
        return ConcurrentMutableHashMap.newMap();
    }

    @Override
    public boolean notEmpty()
    {
        return !this.delegate.isEmpty();
    }

    @Override
    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
        Iterate.forEachWithIndex(this.delegate.values(), objectIntProcedure);
    }

    public int size()
    {
        return this.delegate.size();
    }

    @Override
    public boolean isEmpty()
    {
        return this.delegate.isEmpty();
    }

    @Override
    public Iterator iterator()
    {
        return this.delegate.values().iterator();
    }

    public V remove(Object key)
    {
        return this.delegate.remove(key);
    }

    public Set keySet()
    {
        return this.delegate.keySet();
    }

    public Collection values()
    {
        return this.delegate.values();
    }

    public Set> entrySet()
    {
        if ("1.5".equals(JAVA_SPECIFICATION_VERSION))
        {
            return new SafeEntrySetAdapter(this.delegate.entrySet());
        }
        return this.delegate.entrySet();
    }

    public void clear()
    {
        this.delegate.clear();
    }

    public MutableMap newEmpty()
    {
        return ConcurrentMutableHashMap.newMap();
    }

    @Override
    public void forEachValue(Procedure procedure)
    {
        IterableIterate.forEach(this.delegate.values(), procedure);
    }

    @Override
    public void forEachKey(Procedure procedure)
    {
        IterableIterate.forEach(this.delegate.keySet(), procedure);
    }

    public void forEachKeyValue(Procedure2 procedure)
    {
        IterableIterate.forEach(this.delegate.entrySet(), new MapEntryToProcedure2(procedure));
    }

    public V get(Object key)
    {
        return this.delegate.get(key);
    }

    public V put(K key, V value)
    {
        return this.delegate.put(key, value);
    }

    public void putAll(Map map)
    {
        this.delegate.putAll(map);
    }

    public  MutableMap collectKeysAndValues(
            Iterable iterable,
            Function keyFunction,
            Function valueFunction)
    {
        Iterate.addToMap(iterable, keyFunction, valueFunction, this.delegate);
        return this;
    }

    public V removeKey(K key)
    {
        return this.delegate.remove(key);
    }

    public boolean containsKey(Object key)
    {
        return this.delegate.containsKey(key);
    }

    public boolean containsValue(Object value)
    {
        return this.delegate.containsValue(value);
    }

    @Override
    public V getIfAbsentPut(K key, Function0 function)
    {
        V result = this.delegate.get(key);
        if (result == null)
        {
            V blockValue = function.value();
            V putResult = this.delegate.putIfAbsent(key, blockValue);
            return putResult == null ? blockValue : putResult;
        }
        return result;
    }

    @Override
    public V getIfAbsentPut(K key, V value)
    {
        V result = this.delegate.get(key);
        if (result == null)
        {
            V putResult = this.delegate.putIfAbsent(key, value);
            return putResult == null ? value : putResult;
        }
        return result;
    }

    @Override
    public 

V getIfAbsentPutWith(K key, Function function, P parameter) { V result = this.delegate.get(key); if (result == null) { V functionValue = function.valueOf(parameter); V putResult = this.delegate.putIfAbsent(key, functionValue); return putResult == null ? functionValue : putResult; } return result; } @Override public V getIfAbsent(K key, Function0 function) { V result = this.delegate.get(key); if (result == null) { return function.value(); } return result; } @Override public V getIfAbsentValue(K key, V value) { V result = this.delegate.get(key); if (result == null) { return value; } return result; } @Override public

V getIfAbsentWith( K key, Function function, P parameter) { V result = this.delegate.get(key); if (result == null) { return function.valueOf(parameter); } return result; } public V getIfAbsentPut(K key, Function factory) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".getIfAbsentPut() not implemented yet"); } @Override public A ifPresentApply(K key, Function function) { V result = this.delegate.get(key); return result == null ? null : function.valueOf(result); } @Override public boolean equals(Object o) { return this.delegate.equals(o); } @Override public int hashCode() { return this.delegate.hashCode(); } @Override public

void forEachWith(Procedure2 procedure, P parameter) { Iterate.forEachWith(this.delegate.values(), procedure, parameter); } public V putIfAbsent(K key, V value) { return this.delegate.putIfAbsent(key, value); } public boolean remove(Object key, Object value) { return this.delegate.remove(key, value); } public boolean replace(K key, V oldValue, V newValue) { return this.delegate.replace(key, oldValue, newValue); } public V replace(K key, V value) { return this.delegate.replace(key, value); } private static final class SafeEntrySetAdapter extends AbstractSet> { private final Set> delegate; private SafeEntrySetAdapter(Set> newDelegate) { this.delegate = newDelegate; } @Override public Iterator> iterator() { return new Iterator>() { private final Iterator> entryIterator = SafeEntrySetAdapter.this.delegate.iterator(); public boolean hasNext() { return this.entryIterator.hasNext(); } public Entry next() { Entry superNext = this.entryIterator.next(); return ImmutableEntry.of(superNext.getKey(), superNext.getValue()); } public void remove() { this.entryIterator.remove(); } }; } @Override public int size() { return this.delegate.size(); } } @Override public V updateValue(K key, Function0 factory, Function function) { while (true) { V originalValue = this.delegate.get(key); if (originalValue == null) { V zero = factory.value(); V newValue = function.valueOf(zero); if (this.delegate.putIfAbsent(key, newValue) == null) { return newValue; } } else { V newValue = function.valueOf(originalValue); if (this.delegate.replace(key, originalValue, newValue)) { return newValue; } } } } @Override public

V updateValueWith(K key, Function0 factory, Function2 function, P parameter) { while (true) { V originalValue = this.delegate.get(key); if (originalValue == null) { V zero = factory.value(); V newValue = function.value(zero, parameter); if (this.delegate.putIfAbsent(key, newValue) == null) { return newValue; } } else { V newValue = function.value(originalValue, parameter); if (this.delegate.replace(key, originalValue, newValue)) { return newValue; } } } } @Override public ImmutableMap toImmutable() { return Maps.immutable.ofMap(this); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy