2
* Copyright 2011 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.multimap;
19
import java.util.concurrent.atomic.AtomicInteger;
21
import com.gs.collections.api.collection.MutableCollection;
22
import com.gs.collections.api.map.MutableMap;
23
import com.gs.collections.impl.map.mutable.ConcurrentHashMap;
25
public abstract class AbstractSynchronizedPutMultimap<K, V, C extends MutableCollection<V>> extends AbstractMutableMultimap<K, V, C>
27
private final AtomicInteger atomicTotalSize = new AtomicInteger(0);
29
protected AbstractSynchronizedPutMultimap()
33
protected AbstractSynchronizedPutMultimap(MutableMap<K, C> newMap)
39
protected MutableMap<K, C> createMap()
41
return ConcurrentHashMap.newMap();
45
protected MutableMap<K, C> createMapWithKeyCount(int keyCount)
47
return ConcurrentHashMap.newMap(keyCount);
53
return this.atomicTotalSize.get();
57
protected void incrementTotalSize()
59
this.atomicTotalSize.incrementAndGet();
63
protected void decrementTotalSize()
65
this.atomicTotalSize.decrementAndGet();
69
protected void addToTotalSize(int value)
71
this.atomicTotalSize.addAndGet(value);
75
protected void subtractFromTotalSize(int value)
77
this.atomicTotalSize.addAndGet(-value);
81
protected void clearTotalSize()
83
this.atomicTotalSize.set(0);
87
public boolean put(K key, V value)
89
MutableCollection<V> collection = this.getIfAbsentPutCollection(key);
90
synchronized (collection)
92
if (collection.add(value))
94
this.incrementTotalSize();
101
private C getIfAbsentPutCollection(K key)
103
return this.map.getIfAbsentPutWith(key, this.createCollectionBlock(), this);