Class CompositeMap<K,V>
- Type Parameters:
K- the type of the keys in this mapV- the type of the values in this map
- All Implemented Interfaces:
Serializable,Map<K,,V> Get<K,,V> IterableGet<K,,V> IterableMap<K,,V> Put<K,V>
Changes made to this map will actually be made on the decorated map. Add and remove operations require the use of a pluggable strategy. If no strategy is provided then add and remove are unsupported.
Note that CompositeMap is not synchronized and is not thread-safe.
If you wish to use this map from multiple threads concurrently, you must use
appropriate synchronization. The simplest approach is to wrap this map
using Collections.synchronizedMap(Map). This class may throw
exceptions when accessed by concurrent threads without synchronization.
- Since:
- 3.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThis interface allows definition for all of the indeterminate mutators in a CompositeMap, as well as providing a hook for callbacks on key collisions. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new, empty, CompositeMap.CompositeMap(Map<K, V>... composite) Create a new CompositeMap which composites all of the Map instances in the argument.CompositeMap(Map<K, V>[] composite, CompositeMap.MapMutator<K, V> mutator) Create a new CompositeMap which composites all of the Map instances in the argument.Create a new CompositeMap with two composited Map instances.CompositeMap(Map<K, V> one, Map<K, V> two, CompositeMap.MapMutator<K, V> mutator) Create a new CompositeMap with two composited Map instances. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddComposited(Map<K, V> map) Add an additional Map to the composite.voidclear()Callsclear()on all composited Maps.booleancontainsKey(Object key) Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(Object value) Returnstrueif this map maps one or more keys to the specified value.entrySet()Returns a set view of the mappings contained in this map.booleanChecks if this Map equals another as per the Map specification.Gets the value to which this map maps the specified key.inthashCode()Gets a hash code for the Map as per the Map specification.booleanisEmpty()Returnstrueif this map contains no key-value mappings.keySet()Returns a set view of the keys contained in this map.Associates the specified value with the specified key in this map (optional operation).voidCopies all of the mappings from the specified map to this map (optional operation).Removes the mapping for this key from this map if it is present (optional operation).removeComposited(Map<K, V> map) Remove a Map from the composite.voidsetMutator(CompositeMap.MapMutator<K, V> mutator) Specify the MapMutator to be used by mutation operations.intsize()Returns the number of key-value mappings in this map.values()Returns a collection view of the values contained in this map.Methods inherited from class org.apache.commons.collections4.map.AbstractIterableMap
mapIteratorMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
CompositeMap
public CompositeMap()Create a new, empty, CompositeMap. -
CompositeMap
Create a new CompositeMap which composites all of the Map instances in the argument. It copies the argument array, it does not use it directly.- Parameters:
composite- the Maps to be composited- Throws:
IllegalArgumentException- if there is a key collision
-
CompositeMap
Create a new CompositeMap with two composited Map instances.- Parameters:
one- the first Map to be compositedtwo- the second Map to be composited- Throws:
IllegalArgumentException- if there is a key collision
-
CompositeMap
Create a new CompositeMap with two composited Map instances.- Parameters:
one- the first Map to be compositedtwo- the second Map to be compositedmutator- MapMutator to be used for mutation operations
-
CompositeMap
Create a new CompositeMap which composites all of the Map instances in the argument. It copies the argument array, it does not use it directly.- Parameters:
composite- Maps to be compositedmutator- MapMutator to be used for mutation operations
-
-
Method Details
-
addComposited
Add an additional Map to the composite.- Parameters:
map- the Map to be added to the composite- Throws:
IllegalArgumentException- if there is a key collision and there is no MapMutator set to handle it.
-
clear
-
containsKey
Returnstrueif this map contains a mapping for the specified key. More formally, returnstrueif and only if this map contains at a mapping for a keyksuch that(key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)- Specified by:
containsKeyin interfaceGet<K,V> - Specified by:
containsKeyin interfaceMap<K,V> - Parameters:
key- key whose presence in this map is to be tested.- Returns:
trueif this map contains a mapping for the specified key.- Throws:
ClassCastException- if the key is of an inappropriate type for this map (optional).NullPointerException- if the key isnulland this map does not permitnullkeys (optional).- See Also:
-
containsValue
Returnstrueif this map maps one or more keys to the specified value. More formally, returnstrueif and only if this map contains at least one mapping to a valuevsuch that(value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of theMapinterface.- Specified by:
containsValuein interfaceGet<K,V> - Specified by:
containsValuein interfaceMap<K,V> - Parameters:
value- value whose presence in this map is to be tested.- Returns:
trueif this map maps one or more keys to the specified value.- Throws:
ClassCastException- if the value is of an inappropriate type for this map (optional).NullPointerException- if the value isnulland this map does not permitnullvalues (optional).- See Also:
-
entrySet
Returns a set view of the mappings contained in this map. Each element in the returned set is aMap.Entry. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via theIterator.remove,Set.remove,removeAll,retainAllandclearoperations. It does not support theaddoraddAlloperations.This implementation returns a
CompositeSetwhich composites the entry sets from all of the composited maps. -
equals
-
get
Gets the value to which this map maps the specified key. Returnsnullif the map contains no mapping for this key. A return value ofnulldoes not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key tonull. ThecontainsKeyoperation may be used to distinguish these two cases.More formally, if this map contains a mapping from a key
kto a valuevsuch that(key==null ? k==null : key.equals(k)), then this method returnsv; otherwise it returnsnull. (There can be at most one such mapping.)- Specified by:
getin interfaceGet<K,V> - Specified by:
getin interfaceMap<K,V> - Parameters:
key- key whose associated value is to be returned.- Returns:
- the value to which this map maps the specified key, or
nullif the map contains no mapping for this key. - Throws:
ClassCastException- if the key is of an inappropriate type for this map (optional).NullPointerException- key isnulland this map does not permitnullkeys (optional).- See Also:
-
hashCode
-
isEmpty
-
keySet
Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via theIterator.remove,Set.remove,removeAllretainAll, andclearoperations. It does not support the add oraddAlloperations.This implementation returns a
CompositeSetwhich composites the key sets from all of the composited maps. -
put
Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced by the specified value. (A mapmis said to contain a mapping for a keykif and only ifm.containsKey(k)would returntrue.))- Specified by:
putin interfaceMap<K,V> - Specified by:
putin interfacePut<K,V> - Parameters:
key- key with which the specified value is to be associated.value- value to be associated with the specified key.- Returns:
- previous value associated with specified key, or
nullif there was no mapping for key. Anullreturn can also indicate that the map previously associatednullwith the specified key, if the implementation supportsnullvalues. - Throws:
UnsupportedOperationException- if no MapMutator has been specifiedClassCastException- if the class of the specified key or value prevents it from being stored in this map.IllegalArgumentException- if some aspect of this key or value prevents it from being stored in this map.NullPointerException- this map does not permitnullkeys or values, and the specified key or value isnull.- See Also:
-
putAll
Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of callingput(k, v)on this map once for each mapping from keykto valuevin the specified map. The behavior of this operation is unspecified if the specified map is modified while the operation is in progress.- Specified by:
putAllin interfaceMap<K,V> - Specified by:
putAllin interfacePut<K,V> - Parameters:
map- Mappings to be stored in this map.- Throws:
UnsupportedOperationException- if theputAllmethod is not supported by this map.ClassCastException- if the class of a key or value in the specified map prevents it from being stored in this map.IllegalArgumentException- some aspect of a key or value in the specified map prevents it from being stored in this map.NullPointerException- the specified map isnull, or if this map does not permitnullkeys or values, and the specified map containsnullkeys or values.- See Also:
-
remove
Removes the mapping for this key from this map if it is present (optional operation). More formally, if this map contains a mapping from keykto valuevsuch that(key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)Returns the value to which the map previously associated the key, or
nullif the map contained no mapping for this key. (Anullreturn can also indicate that the map previously associatednullwith the specified key if the implementation supportsnullvalues.) The map will not contain a mapping for the specified key once the call returns.- Specified by:
removein interfaceGet<K,V> - Specified by:
removein interfaceMap<K,V> - Parameters:
key- key whose mapping is to be removed from the map.- Returns:
- previous value associated with specified key, or
nullif there was no mapping for key. - Throws:
ClassCastException- if the key is of an inappropriate type for the composited map (optional).NullPointerException- if the key isnulland the composited map does not permitnullkeys (optional).UnsupportedOperationException- if theremovemethod is not supported by the composited map containing the key- See Also:
-
removeComposited
-
setMutator
Specify the MapMutator to be used by mutation operations.- Parameters:
mutator- the MapMutator to be used for mutation delegation
-
size
Returns the number of key-value mappings in this map. If the map contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE. -
values
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via theIterator.remove,Collection.remove,removeAll,retainAllandclearoperations. It does not support the add oraddAlloperations.
-