- java.lang.Object
-
- fr.dufrenoy.util.SynchronizedSymmetricMap<K,V>
-
- Type Parameters:
K- the type of keysV- the type of values
- All Implemented Interfaces:
SymmetricMap<K,V>,Serializable,Map<K,V>
public class SynchronizedSymmetricMap<K,V> extends Object implements SymmetricMap<K,V>, Serializable
A thread-safe implementation ofSymmetricMap, backed by anUnsynchronizedSymmetricMapand 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.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.
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SynchronizedSymmetricMap()Creates a newSynchronizedSymmetricMapwith the default initial capacity (16) and load factor (0.75).SynchronizedSymmetricMap(int initialCapacity)Creates a newSynchronizedSymmetricMapwith the given initial capacity and the default load factor (0.75).SynchronizedSymmetricMap(int initialCapacity, float loadFactor)Creates a newSynchronizedSymmetricMapwith the given initial capacity and load factor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Vcompute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)VcomputeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)booleancontainsKey(Object key)booleancontainsValue(Object value)Set<Map.Entry<K,V>>entrySet()Returns aSetview of the entries contained in this map.booleanequals(Object o)Vget(Object key)Optional<K>getKey(Object value)Returns the key associated with the given value, or empty if not found.inthashCode()SynchronizedSymmetricMap<V,K>inverse()Returns an independent copy of this map with keys and values swapped, as aSynchronizedSymmetricMap.booleanisEmpty()Set<K>keySet()Returns aSetview of the keys contained in this map.Vmerge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)Vput(K key, V value)voidputAll(Map<? extends K,? extends V> m)Vremove(Object key)Optional<K>removeByValue(Object value)Removes the entry associated with the given value, if present.Vreplace(K key, V value)booleanreplace(K key, V oldValue, V newValue)voidreplaceAll(BiFunction<? super K,? super V,? extends V> function)Replaces each value with the result of the given function applied to its key and current value.voidsafePut(K key, V value)Associates the given key with the given value, throwing an exception if the key or value already exists in this map.intsize()StringtoString()Set<V>values()Returns aSetview of the values contained in this map.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
computeIfAbsent, forEach, getOrDefault, putIfAbsent, remove
-
-
-
-
Constructor Detail
-
SynchronizedSymmetricMap
public SynchronizedSymmetricMap()
Creates a newSynchronizedSymmetricMapwith the default initial capacity (16) and load factor (0.75).
-
SynchronizedSymmetricMap
public SynchronizedSymmetricMap(int initialCapacity)
Creates a newSynchronizedSymmetricMapwith the given initial capacity and the default load factor (0.75).- Parameters:
initialCapacity- the initial capacity; must be at least 1- Throws:
IllegalArgumentException- ifinitialCapacityis less than 1
-
SynchronizedSymmetricMap
public SynchronizedSymmetricMap(int initialCapacity, float loadFactor)Creates a newSynchronizedSymmetricMapwith the given initial capacity and load factor.- Parameters:
initialCapacity- the initial capacity; must be at least 1loadFactor- the load factor; must be positive- Throws:
IllegalArgumentException- ifinitialCapacityis less than 1 orloadFactoris not positive
-
-
Method Detail
-
getKey
public Optional<K> getKey(Object value)
Description copied from interface:SymmetricMapReturns the key associated with the given value, or empty if not found.- Specified by:
getKeyin interfaceSymmetricMap<K,V>- Parameters:
value- the value to look up- Returns:
- an
Optionalcontaining the key associated withvalue, or empty if not found
-
safePut
public void safePut(K key, V value)
Description copied from interface:SymmetricMapAssociates the given key with the given value, throwing an exception if the key or value already exists in this map.Use
Map.put(Object, Object)for a permissive insertion that silently removes conflicting entries.- Specified by:
safePutin interfaceSymmetricMap<K,V>- Parameters:
key- the keyvalue- the value
-
removeByValue
public Optional<K> removeByValue(Object value)
Description copied from interface:SymmetricMapRemoves the entry associated with the given value, if present. Symmetric toMap.remove(Object)which removes by key.- Specified by:
removeByValuein interfaceSymmetricMap<K,V>- Parameters:
value- the value whose entry is to be removed- Returns:
- an
Optionalcontaining the key that was associated withvalue, or empty if not found
-
inverse
public SynchronizedSymmetricMap<V,K> inverse()
Returns an independent copy of this map with keys and values swapped, as aSynchronizedSymmetricMap. The copy is taken under a read lock to guarantee a consistent snapshot.- Specified by:
inversein interfaceSymmetricMap<K,V>- Returns:
- a new
SynchronizedSymmetricMap<V, K>with all entries inverted
-
values
public Set<V> values()
Returns aSetview of the values contained in this map.This method narrows the return type of
Map.values()fromCollectiontoSet, which is valid since values in a bijective map are unique by definition.The returned set is a live view of the map: changes to the map are reflected in the set, and vice versa.
The returned set is a snapshot-based view: its iterator operates on a copy of the map taken under a read lock at the time
Set.iterator()is called.Memory note: each call to
Set.iterator()copies the entire entry set.
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValuein interfaceMap<K,V>
-
replaceAll
public void replaceAll(BiFunction<? super K,? super V,? extends V> function)
Replaces each value with the result of the given function applied to its key and current value. This operation is fully atomic — the write lock is held for its entire duration.Maintains bijectivity: if the function produces a duplicate value, the conflicting entry is silently removed.
- Specified by:
replaceAllin interfaceMap<K,V>- Parameters:
function- the remapping function
-
merge
public V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
-
computeIfPresent
public V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
- Specified by:
computeIfPresentin interfaceMap<K,V>
-
keySet
public Set<K> keySet()
Returns aSetview of the keys contained in this map.The returned set is a snapshot-based view: its iterator operates on a copy of the map taken under a read lock at the time
Set.iterator()is called.Memory note: each call to
Set.iterator()copies the entire entry set.
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
Returns aSetview of the entries contained in this map.The returned set is a snapshot-based view: its iterator operates on a copy of the map taken under a read lock at the time
Set.iterator()is called.Memory note: each call to
Set.iterator()copies the entire entry set.
-
equals
public boolean equals(Object o)
-
hashCode
public int hashCode()
-
-