Package com.globalmentor.collections
Class DecoratorReadWriteLockCollectionMap<K,V,C extends java.util.Collection<V>>
- java.lang.Object
-
- com.globalmentor.collections.ReadWriteLockDecorator
-
- com.globalmentor.collections.DecoratorReadWriteLockMap<K,C>
-
- com.globalmentor.collections.DecoratorReadWriteLockCollectionMap<K,V,C>
-
- Type Parameters:
K
- The type of key used in the map.V
- The type of value stored in the map.C
- The type of collection in which to store values in the map.
- All Implemented Interfaces:
CollectionMap<K,V,C>
,ReadWriteLockCollectionMap<K,V,C>
,ReadWriteLockMap<K,C>
,java.util.concurrent.locks.ReadWriteLock
,java.util.Map<K,C>
public class DecoratorReadWriteLockCollectionMap<K,V,C extends java.util.Collection<V>> extends DecoratorReadWriteLockMap<K,C> implements ReadWriteLockCollectionMap<K,V,C>
A thread-safe collection map decorator that allows many readers but only one writer to access a map at a time. For operations that iterate over live map data, a read or write lock should be acquired before the call to acquire the data and held until the data is consumed.- Author:
- Garret Wilson
-
-
Field Summary
Fields Modifier and Type Field Description protected CollectionMap<K,V,C>
collectionMap
The collection map this class decorates.-
Fields inherited from class com.globalmentor.collections.DecoratorReadWriteLockMap
map
-
-
Constructor Summary
Constructors Constructor Description DecoratorReadWriteLockCollectionMap(CollectionMap<K,V,C> collectionMap)
Collection map constructor with a default reentrant read/write lock.DecoratorReadWriteLockCollectionMap(CollectionMap<K,V,C> collectionMap, java.util.concurrent.locks.ReadWriteLock lock)
Collection map and read/write lock constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addItem(K key, V value)
Adds a value to the collection of values associated with the key.C
createCollection()
Creates a collection in which to store values.C
getCollection(K key)
Retrieves the collection of values associated with the given key.V
getItem(K key)
Retrieves the first value from the collection of values, if any, associated with the key.int
getItemCount(K key)
Retrieves the number of values in the collection, if any, associated with the key.java.lang.Iterable<V>
getItems(K key)
Retrieves iterable access to all items, if any, associated with the given keyboolean
hasItems(K key)
Retrieves whether there are items in a collection associated with the key.boolean
removeItem(K key, V value)
Removes the first occurence of the given value from the collection of values, if any, associated with the key.-
Methods inherited from class com.globalmentor.collections.DecoratorReadWriteLockMap
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Methods inherited from class com.globalmentor.collections.ReadWriteLockDecorator
readLock, writeLock
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
Field Detail
-
collectionMap
protected final CollectionMap<K,V,C extends java.util.Collection<V>> collectionMap
The collection map this class decorates.
-
-
Constructor Detail
-
DecoratorReadWriteLockCollectionMap
public DecoratorReadWriteLockCollectionMap(CollectionMap<K,V,C> collectionMap)
Collection map constructor with a default reentrant read/write lock.- Parameters:
collectionMap
- The collection map this map should decorate.- Throws:
java.lang.NullPointerException
- if the provided collection map isnull
.
-
DecoratorReadWriteLockCollectionMap
public DecoratorReadWriteLockCollectionMap(CollectionMap<K,V,C> collectionMap, java.util.concurrent.locks.ReadWriteLock lock)
Collection map and read/write lock constructor.- Parameters:
collectionMap
- The collection map this map should decorate.lock
- The lock for controlling access to the map.- Throws:
java.lang.NullPointerException
- if the provided map and/or lock isnull
.
-
-
Method Detail
-
getCollection
public C getCollection(K key)
Description copied from interface:CollectionMap
Retrieves the collection of values associated with the given key. If no collection of values is associated with the key, one will be created and added to the map.- Specified by:
getCollection
in interfaceCollectionMap<K,V,C extends java.util.Collection<V>>
- Parameters:
key
- The key in the map.- Returns:
- The collection associated with the given key
- See Also:
CollectionMap.createCollection()
-
createCollection
public C createCollection()
Description copied from interface:CollectionMap
Creates a collection in which to store values.- Specified by:
createCollection
in interfaceCollectionMap<K,V,C extends java.util.Collection<V>>
- Returns:
- The collections with the values stored.
-
hasItems
public boolean hasItems(K key)
Description copied from interface:CollectionMap
Retrieves whether there are items in a collection associated with the key.
-
getItemCount
public int getItemCount(K key)
Description copied from interface:CollectionMap
Retrieves the number of values in the collection, if any, associated with the key.- Specified by:
getItemCount
in interfaceCollectionMap<K,V,C extends java.util.Collection<V>>
- Parameters:
key
- The key in the map.- Returns:
- The number of items associated with the key.
-
addItem
public void addItem(K key, V value)
Description copied from interface:CollectionMap
Adds a value to the collection of values associated with the key. If no collection of values is associated with the key, one will be created and added to the map.
-
getItem
public V getItem(K key)
Description copied from interface:CollectionMap
Retrieves the first value from the collection of values, if any, associated with the key.- Specified by:
getItem
in interfaceCollectionMap<K,V,C extends java.util.Collection<V>>
- Parameters:
key
- The key in the map.- Returns:
- The first value in the collection, or
null
if there is no collection associated with the key or no values in the collection. - See Also:
CollectionMap.getCollection(Object)
-
getItems
public java.lang.Iterable<V> getItems(K key)
Description copied from interface:CollectionMap
Retrieves iterable access to all items, if any, associated with the given key
-
removeItem
public boolean removeItem(K key, V value)
Description copied from interface:CollectionMap
Removes the first occurence of the given value from the collection of values, if any, associated with the key. If all items from the collection are removed, the collection itself is removed from the map.- Specified by:
removeItem
in interfaceCollectionMap<K,V,C extends java.util.Collection<V>>
- Parameters:
key
- The key in the map.value
- The item to be removed from the collection, if present.- Returns:
true
if an item was removed as a result of this call.
-
-