2
* Copyright 2014 Goldman Sachs.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.gs.collections.impl.map.mutable;
19
import java.io.Serializable;
20
import java.util.AbstractSet;
21
import java.util.Collection;
22
import java.util.Iterator;
25
import java.util.concurrent.ConcurrentHashMap;
26
import java.util.concurrent.ConcurrentMap;
28
import com.gs.collections.api.block.function.Function;
29
import com.gs.collections.api.block.function.Function0;
30
import com.gs.collections.api.block.function.Function2;
31
import com.gs.collections.api.block.procedure.Procedure;
32
import com.gs.collections.api.block.procedure.Procedure2;
33
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
34
import com.gs.collections.api.map.ConcurrentMutableMap;
35
import com.gs.collections.api.map.ImmutableMap;
36
import com.gs.collections.api.map.MutableMap;
37
import com.gs.collections.api.tuple.Pair;
38
import com.gs.collections.impl.block.procedure.MapEntryToProcedure2;
39
import com.gs.collections.impl.factory.Maps;
40
import com.gs.collections.impl.tuple.ImmutableEntry;
41
import com.gs.collections.impl.utility.Iterate;
42
import com.gs.collections.impl.utility.internal.IterableIterate;
45
* A simple concurrent implementation of MutableMap which uses java.util.concurrent.ConcurrentHashMap for its underlying
46
* concurrent Map implementation.
48
* @see com.gs.collections.impl.map.mutable.ConcurrentHashMap
49
* @deprecated since 2.0
52
public final class ConcurrentMutableHashMap<K, V>
53
extends AbstractMutableMap<K, V>
54
implements ConcurrentMutableMap<K, V>, Serializable
56
private static final long serialVersionUID = 1L;
57
private static final String JAVA_SPECIFICATION_VERSION = System.getProperty("java.specification.version");
59
private final ConcurrentMap<K, V> delegate;
61
private ConcurrentMutableHashMap()
63
this(new ConcurrentHashMap<K, V>());
66
public ConcurrentMutableHashMap(ConcurrentMap<K, V> delegate)
68
this.delegate = delegate;
71
public static <NK, NV> ConcurrentMutableHashMap<NK, NV> newMap()
73
return new ConcurrentMutableHashMap<NK, NV>();
76
public static <NK, NV> ConcurrentMutableHashMap<NK, NV> newMap(int initialCapacity)
78
return new ConcurrentMutableHashMap<NK, NV>(new ConcurrentHashMap<NK, NV>(initialCapacity));
81
public static <NK, NV> ConcurrentMutableHashMap<NK, NV> newMap(int initialCapacity, float loadFactor, int concurrencyLevel)
83
return new ConcurrentMutableHashMap<NK, NV>(new ConcurrentHashMap<NK, NV>(initialCapacity, loadFactor, concurrencyLevel));
86
public static <NK, NV> ConcurrentMutableHashMap<NK, NV> newMap(Map<NK, NV> map)
88
return new ConcurrentMutableHashMap<NK, NV>(new ConcurrentHashMap<NK, NV>(map));
92
public ConcurrentMutableHashMap<K, V> withKeyValue(K key, V value)
94
return (ConcurrentMutableHashMap<K, V>) super.withKeyValue(key, value);
98
public ConcurrentMutableHashMap<K, V> withAllKeyValues(Iterable<? extends Pair<? extends K, ? extends V>> keyValues)
100
return (ConcurrentMutableHashMap<K, V>) super.withAllKeyValues(keyValues);
104
public ConcurrentMutableHashMap<K, V> withAllKeyValueArguments(Pair<? extends K, ? extends V>... keyValues)
106
return (ConcurrentMutableHashMap<K, V>) super.withAllKeyValueArguments(keyValues);
110
public ConcurrentMutableHashMap<K, V> withoutKey(K key)
112
return (ConcurrentMutableHashMap<K, V>) super.withoutKey(key);
116
public ConcurrentMutableHashMap<K, V> withoutAllKeys(Iterable<? extends K> keys)
118
return (ConcurrentMutableHashMap<K, V>) super.withoutAllKeys(keys);
122
public String toString()
124
return this.delegate.toString();
128
public MutableMap<K, V> clone()
130
return ConcurrentMutableHashMap.newMap(this.delegate);
134
public <K, V> MutableMap<K, V> newEmpty(int capacity)
136
return ConcurrentMutableHashMap.newMap();
140
public boolean notEmpty()
142
return !this.delegate.isEmpty();
146
public void forEach(Procedure<? super V> procedure)
148
IterableIterate.forEach(this.delegate.values(), procedure);
152
public void forEachWithIndex(ObjectIntProcedure<? super V> objectIntProcedure)
154
Iterate.forEachWithIndex(this.delegate.values(), objectIntProcedure);
159
return this.delegate.size();
163
public boolean isEmpty()
165
return this.delegate.isEmpty();
169
public Iterator<V> iterator()
171
return this.delegate.values().iterator();
174
public V remove(Object key)
176
return this.delegate.remove(key);
179
public Set<K> keySet()
181
return this.delegate.keySet();
184
public Collection<V> values()
186
return this.delegate.values();
189
public Set<Entry<K, V>> entrySet()
191
if ("1.5".equals(JAVA_SPECIFICATION_VERSION))
193
return new SafeEntrySetAdapter<K, V>(this.delegate.entrySet());
195
return this.delegate.entrySet();
200
this.delegate.clear();
203
public MutableMap<K, V> newEmpty()
205
return ConcurrentMutableHashMap.newMap();
209
public void forEachValue(Procedure<? super V> procedure)
211
IterableIterate.forEach(this.delegate.values(), procedure);
215
public void forEachKey(Procedure<? super K> procedure)
217
IterableIterate.forEach(this.delegate.keySet(), procedure);
220
public void forEachKeyValue(Procedure2<? super K, ? super V> procedure)
222
IterableIterate.forEach(this.delegate.entrySet(), new MapEntryToProcedure2<K, V>(procedure));
225
public V get(Object key)
227
return this.delegate.get(key);
230
public V put(K key, V value)
232
return this.delegate.put(key, value);
235
public void putAll(Map<? extends K, ? extends V> map)
237
this.delegate.putAll(map);
240
public <E> MutableMap<K, V> collectKeysAndValues(
241
Iterable<E> iterable,
242
Function<? super E, ? extends K> keyFunction,
243
Function<? super E, ? extends V> valueFunction)
245
Iterate.addToMap(iterable, keyFunction, valueFunction, this.delegate);
249
public V removeKey(K key)
251
return this.delegate.remove(key);
254
public boolean containsKey(Object key)
256
return this.delegate.containsKey(key);
259
public boolean containsValue(Object value)
261
return this.delegate.containsValue(value);
265
public V getIfAbsentPut(K key, Function0<? extends V> function)
267
V result = this.delegate.get(key);
270
V blockValue = function.value();
271
V putResult = this.delegate.putIfAbsent(key, blockValue);
272
return putResult == null ? blockValue : putResult;
278
public V getIfAbsentPut(K key, V value)
280
V result = this.delegate.get(key);
283
V putResult = this.delegate.putIfAbsent(key, value);
284
return putResult == null ? value : putResult;
290
public <P> V getIfAbsentPutWith(K key, Function<? super P, ? extends V> function, P parameter)
292
V result = this.delegate.get(key);
295
V functionValue = function.valueOf(parameter);
296
V putResult = this.delegate.putIfAbsent(key, functionValue);
297
return putResult == null ? functionValue : putResult;
303
public V getIfAbsent(K key, Function0<? extends V> function)
305
V result = this.delegate.get(key);
308
return function.value();
314
public V getIfAbsentValue(K key, V value)
316
V result = this.delegate.get(key);
325
public <P> V getIfAbsentWith(
327
Function<? super P, ? extends V> function,
330
V result = this.delegate.get(key);
333
return function.valueOf(parameter);
338
public V getIfAbsentPut(K key, Function<? super K, ? extends V> factory)
340
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".getIfAbsentPut() not implemented yet");
344
public <A> A ifPresentApply(K key, Function<? super V, ? extends A> function)
346
V result = this.delegate.get(key);
347
return result == null ? null : function.valueOf(result);
351
public boolean equals(Object o)
353
return this.delegate.equals(o);
357
public int hashCode()
359
return this.delegate.hashCode();
363
public <P> void forEachWith(Procedure2<? super V, ? super P> procedure, P parameter)
365
Iterate.forEachWith(this.delegate.values(), procedure, parameter);
368
public V putIfAbsent(K key, V value)
370
return this.delegate.putIfAbsent(key, value);
373
public boolean remove(Object key, Object value)
375
return this.delegate.remove(key, value);
378
public boolean replace(K key, V oldValue, V newValue)
380
return this.delegate.replace(key, oldValue, newValue);
383
public V replace(K key, V value)
385
return this.delegate.replace(key, value);
388
private static final class SafeEntrySetAdapter<K, V>
389
extends AbstractSet<Entry<K, V>>
391
private final Set<Entry<K, V>> delegate;
393
private SafeEntrySetAdapter(Set<Entry<K, V>> newDelegate)
395
this.delegate = newDelegate;
399
public Iterator<Entry<K, V>> iterator()
401
return new Iterator<Entry<K, V>>()
403
private final Iterator<Entry<K, V>> entryIterator = SafeEntrySetAdapter.this.delegate.iterator();
405
public boolean hasNext()
407
return this.entryIterator.hasNext();
410
public Entry<K, V> next()
412
Entry<K, V> superNext = this.entryIterator.next();
413
return ImmutableEntry.of(superNext.getKey(), superNext.getValue());
418
this.entryIterator.remove();
426
return this.delegate.size();
431
public V updateValue(K key, Function0<? extends V> factory, Function<? super V, ? extends V> function)
435
V originalValue = this.delegate.get(key);
436
if (originalValue == null)
438
V zero = factory.value();
439
V newValue = function.valueOf(zero);
440
if (this.delegate.putIfAbsent(key, newValue) == null)
447
V newValue = function.valueOf(originalValue);
448
if (this.delegate.replace(key, originalValue, newValue))
457
public <P> V updateValueWith(K key, Function0<? extends V> factory, Function2<? super V, ? super P, ? extends V> function, P parameter)
461
V originalValue = this.delegate.get(key);
462
if (originalValue == null)
464
V zero = factory.value();
465
V newValue = function.value(zero, parameter);
466
if (this.delegate.putIfAbsent(key, newValue) == null)
473
V newValue = function.value(originalValue, parameter);
474
if (this.delegate.replace(key, originalValue, newValue))
483
public ImmutableMap<K, V> toImmutable()
485
return Maps.immutable.ofMap(this);