- java.lang.Object
-
- fr.dufrenoy.util.SynchronizedMultiMap<K,V>
-
- Type Parameters:
K- the type of keys at this levelV- the type of values at this level (may be anotherMultiMap)
- All Implemented Interfaces:
MultiMap<K,V>,Serializable
public class SynchronizedMultiMap<K,V> extends Object implements MultiMap<K,V>, Serializable
A thread-safe implementation ofMultiMap, backed by anUnsynchronizedMultiMapand protected by aReentrantReadWriteLock.Multiple threads may read concurrently. Write operations are exclusive: a write blocks until all ongoing reads have completed, and blocks any new reads until the write is done.
The iterators returned by the views of this map (
keySet(),values(),entrySet()) are fail-safe: they operate on a snapshot of the map taken at the time of the call, under a read lock. Subsequent modifications to the map are not reflected in the snapshot.Atomicity note: each operation is atomic at the current level only. In a recursive multi-level structure, operations on sub-maps are independent — there is no cross-level locking. Callers requiring atomicity across multiple levels must provide their own synchronization.
Memory note: snapshot-based iterators copy the entire entry set at the time of the call. Avoid iterating over very large maps in memory-constrained environments.
Null keys and null values are forbidden. All methods that accept keys or values throw
NullPointerExceptionif a null argument is passed.- See Also:
MultiMap,UnsynchronizedMultiMap, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SynchronizedMultiMap()Creates a new emptySynchronizedMultiMapwith default initial capacity.SynchronizedMultiMap(int initialCapacity)Creates a new emptySynchronizedMultiMapwith the specified initial capacity.SynchronizedMultiMap(MultiMap<? extends K,? extends V> source)Creates a newSynchronizedMultiMapcontaining the same mappings as the specifiedMultiMap.SynchronizedMultiMap(Map<? extends K,? extends V> source)Creates a newSynchronizedMultiMapcontaining the same mappings as the specified map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all mappings from this map.booleancontainsKey(K key)Returnstrueif this map contains a mapping for the specified key.Set<Map.Entry<K,V>>entrySet()Returns aSetview of the mappings contained in this map.booleanequals(Object obj)Vget(K key)Returns the value associated with the specified key, ornullif no mapping exists.Optional<V>getOpt(K key)Returns the value associated with the specified key wrapped in anOptional, or an emptyOptionalif no mapping exists.VgetOrCreate(K key, Supplier<V> factory)Returns the value associated with the specified key.inthashCode()booleanisEmpty()Returnstrueif this map contains no mappings.Set<K>keySet()Returns aSetview of the keys contained in this map.Vput(K key, V value)Associates the specified value with the specified key.Vremove(K key)Removes the mapping for the specified key, if present.intsize()Returns the number of key-value mappings at this level.StringtoString()Collection<V>values()Returns aCollectionview of the values contained in this map.
-
-
-
Constructor Detail
-
SynchronizedMultiMap
public SynchronizedMultiMap()
Creates a new emptySynchronizedMultiMapwith default initial capacity.
-
SynchronizedMultiMap
public SynchronizedMultiMap(int initialCapacity)
Creates a new emptySynchronizedMultiMapwith the specified initial capacity.- Parameters:
initialCapacity- the initial capacity- Throws:
IllegalArgumentException- ifinitialCapacityis negative
-
SynchronizedMultiMap
public SynchronizedMultiMap(Map<? extends K,? extends V> source)
Creates a newSynchronizedMultiMapcontaining the same mappings as the specified map. Null keys and null values in the source map cause aNullPointerException.- Parameters:
source- the map whose mappings are to be copied- Throws:
NullPointerException- ifsourceis null, or if it contains null keys or null values
-
SynchronizedMultiMap
public SynchronizedMultiMap(MultiMap<? extends K,? extends V> source)
Creates a newSynchronizedMultiMapcontaining the same mappings as the specifiedMultiMap.- Parameters:
source- the multi-map whose mappings are to be copied- Throws:
NullPointerException- ifsourceis null
-
-
Method Detail
-
get
public V get(K key)
Description copied from interface:MultiMapReturns the value associated with the specified key, ornullif no mapping exists. This method is designed for chained lookups across multiple levels.
-
getOpt
public Optional<V> getOpt(K key)
Description copied from interface:MultiMapReturns the value associated with the specified key wrapped in anOptional, or an emptyOptionalif no mapping exists.
-
getOrCreate
public V getOrCreate(K key, Supplier<V> factory)
Description copied from interface:MultiMapReturns the value associated with the specified key. If no mapping exists, creates one using the given factory, inserts it, and returns the new value. This method is designed for chained writes across multiple levels.- Specified by:
getOrCreatein interfaceMultiMap<K,V>- Parameters:
key- the key whose associated value is to be returned or createdfactory- the supplier used to create a new value if absent- Returns:
- the existing or newly created value
-
containsKey
public boolean containsKey(K key)
Description copied from interface:MultiMapReturnstrueif this map contains a mapping for the specified key.- Specified by:
containsKeyin interfaceMultiMap<K,V>- Parameters:
key- the key whose presence is to be tested- Returns:
trueif this map contains a mapping forkey
-
put
public V put(K key, V value)
Description copied from interface:MultiMapAssociates the specified value with the specified key. If a mapping already exists for this key, the old value is replaced.
-
remove
public V remove(K key)
Description copied from interface:MultiMapRemoves the mapping for the specified key, if present.
-
clear
public void clear()
Description copied from interface:MultiMapRemoves all mappings from this map.
-
size
public int size()
Description copied from interface:MultiMapReturns the number of key-value mappings at this level.
-
isEmpty
public boolean isEmpty()
Description copied from interface:MultiMapReturnstrueif this map contains no mappings.
-
keySet
public Set<K> keySet()
Description copied from interface:MultiMapReturns aSetview of the keys contained in this map.
-
values
public Collection<V> values()
Description copied from interface:MultiMapReturns aCollectionview of the values contained in this map.
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
Description copied from interface:MultiMapReturns aSetview of the mappings contained in this map. Each element is aMap.Entry<K, V>.
-
-