Package org.eclipse.epsilon.common.util
Class Multimap<K,V>
java.lang.Object
org.eclipse.epsilon.common.util.Multimap<K,V>
- Type Parameters:
K
- The key type.V
- The type of values.
- All Implemented Interfaces:
Map<K,
Collection<V>>
A Map which allows each to be associated with multiple values.
- Since:
- 1.6 implements Map interface, additional utility methods
-
Nested Class Summary
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final boolean
protected final boolean
protected final Map<K,
Collection<V>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all mappings.boolean
containsKey
(Object key) boolean
containsValue
(Object value) Set<Map.Entry<K,
Collection<V>>> entrySet()
boolean
Returns all values associated with the key.getMutable
(Object key) Returns all values associated with the key.int
hashCode()
boolean
boolean
isEmpty()
boolean
keySet()
protected final Collection<V>
Convenience method for creating new values for entries.protected Collection<V>
newCollection
(Collection<? extends V> values) Convenience method for creating new values for entries.protected final Map<K,
Collection<V>> Convenience method for creating a new underlying Multimap.protected Map<K,
Collection<V>> newMapDelegate
(Map<? extends K, ? extends Collection<V>> initial) Convenience method for creating a new underlying Multimap.put
(K key, Collection<V> values) put
(K key, Collection<V> values, boolean wrap) Associates the key with the given values, overwriting the entry if the key is present.boolean
Adds the given value to the associations for the key.void
putAll
(Map<? extends K, ? extends Collection<V>> m) final boolean
putAll
(K key, Collection<V> values) Associates all of the specified values for the given key.boolean
putAll
(K key, Collection<V> values, boolean wrap) Associates all of the specified values for the given key.protected boolean
putAll
(K key, Collection<V> values, boolean create, boolean replace, boolean wrap) Replaces the entry in this Multimap with the specified values.boolean
putAllIfPresent
(K key, Collection<V> values) Adds all of the specified values to the collection associated with the key if a mapping for the key exists in this Multimap.boolean
putIfPresent
(Object key, V value) Associates the value with the key if the key already exists.Deletes the specified key from this map.boolean
Removes the specified value associated with the given key.boolean
Removes all values associated with the key, leaving the key itself in place, by callingCollection.clear()
if the mapping is present.boolean
removeAll
(Object key, Collection<V> values) Removes all of the specified values associated with the key.boolean
replaceValues
(K key, Collection<V> values) Replaces the values associated with the key with the values from the specified collection.int
size()
int
Returns the number of values associated with the key.Streams the values for the given key.toString()
values()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, replace, replace, replaceAll
-
Field Details
-
storage
-
isConcurrent
protected final boolean isConcurrent -
isReadOptimized
protected final boolean isReadOptimized
-
-
Constructor Details
-
Multimap
public Multimap()Creates a non-concurrent Multimap. -
Multimap
public Multimap(boolean concurrent) - Parameters:
concurrent
- Whether this implementation should be thread-safe.- Since:
- 1.6
-
Multimap
public Multimap(boolean concurrent, boolean readOnly) - Parameters:
concurrent
- Whether this implementation should be thread-safe.readOnly
- Whether this Multimap (i.e. both the underlying Map itself and collections associated with keys) will be immutable or rarely written to.- Since:
- 1.6
-
Multimap
- Parameters:
concurrent
- Whether this implementation should be thread-safe.readOnly
- Whether this Multimap (i.e. both the underlying Map itself and collections associated with keys) will be immutable or rarely written to.other
- The Multimap to copy from.- Since:
- 1.6
-
Multimap
Copy constructor.- Parameters:
other
- The Multimap to copy from.- Throws:
NullPointerException
- If the Multimap is null.
-
-
Method Details
-
newMapDelegate
Convenience method for creating a new underlying Multimap.- Parameters:
initial
- The elements to add to the multimap.- Since:
- 1.6
- See Also:
-
newMapDelegate
Convenience method for creating a new underlying Multimap.- Parameters:
initial
- The elements to add to the multimap.- Returns:
- A suitable Map based on the thread-safety of this Multimap.
-
newCollection
Convenience method for creating new values for entries.- Returns:
- A suitable collection based on the thread-safety of this Multimap.
- Since:
- 1.6
- See Also:
-
newCollection
Convenience method for creating new values for entries.- Parameters:
values
- The items to add to the returned collection.- Returns:
- A suitable collection based on the thread-safety of this Multimap.
- Since:
- 1.6
-
get
Returns all values associated with the key. The returned collection is immutable. -
getMutable
Returns all values associated with the key. The returned collection is the collection used internally to associate the key with its values.- Parameters:
key
-- Returns:
- A collection of the associated values, or
null
if no mapping was present. - Since:
- 1.6
-
put
Adds the given value to the associations for the key.- Parameters:
key
-value
-- Returns:
- Whether a mapping existed for the key.
-
putIfPresent
Associates the value with the key if the key already exists. Note that this operation will succeed even if the key has no values associated with it; only if the key is present.- Parameters:
key
-value
-- Returns:
- Whether the key was present and value was added.
- Since:
- 1.6
-
putAllIfPresent
Adds all of the specified values to the collection associated with the key if a mapping for the key exists in this Multimap.- Parameters:
key
-values
- The additional values to associate with the key.- Returns:
- Whether the key was present and the values were added.
- Since:
- 1.6
-
remove
Removes the specified value associated with the given key. -
removeAll
Removes all values associated with the key, leaving the key itself in place, by callingCollection.clear()
if the mapping is present.- Parameters:
key
-- Returns:
- Whether the key was present and the collection changed as a result.
- Since:
- 1.6
-
removeAll
Removes all of the specified values associated with the key. Note that if the collection contains all values associated with the key, the key itself will not be removed.- Parameters:
key
-values
- The values to remove.- Returns:
- Whether the key was present and the associated collection of values was changed as a result of this call.
- Since:
- 1.6
-
remove
Deletes the specified key from this map. -
clear
public void clear()Removes all mappings. -
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V> - Parameters:
key
-- Returns:
- Whether the given key has one or more values associated with it.
-
hasKey
- Parameters:
key
-- Returns:
- Whether the key is present in they keySet.
- Since:
- 1.6
-
putAll
Associates all of the specified values for the given key. Previously associated values will not be removed.- Parameters:
key
-values
- The additional values to associate with the key.- Returns:
- Whether the key was present.
- Since:
- 1.6 Values are wrapped into the appropriate collection, and existing values are appended to rather than replaced.
-
putAll
Associates all of the specified values for the given key. Previously associated values will not be removed.- Parameters:
key
-values
- The additional values to associate with the key.wrap
- Whether to wrap the provided values into a new collection.- Returns:
- Whether the key was present.
- Since:
- 1.6
-
replaceValues
Replaces the values associated with the key with the values from the specified collection. The Collection associated with the key itself will not be replaced; only its values will be. Note that unlike putAll, a new entry will not be created if a mapping for the key doesn't exist.- Parameters:
key
-values
- The collection of values to copy from.- Returns:
- Whether the collection was changed as a result of
- Since:
- 1.6
-
put
Associates the key with the given values, overwriting the entry if the key is present. Note that the associated collection will not be the same object as the parameter.- Parameters:
key
-values
- The values to copy from.wrap
- Whether to wrap the provided values into a new collection.- Returns:
- The previously associated values, or
null
if no mapping was present. - Since:
- 1.6
-
put
-
putAll
protected boolean putAll(K key, Collection<V> values, boolean create, boolean replace, boolean wrap) Replaces the entry in this Multimap with the specified values.- Parameters:
key
-values
- The values to associate with the key.create
- Creates a new entry with the values if one doesn't exist.replace
- Whether to append or replace existing values.wrap
- Whether to wrap the values into a known data structure.- Returns:
- Whether the operation had an effect on this Multimap.
- Since:
- 1.6
-
isThreadSafe
public boolean isThreadSafe()- Returns:
- Since:
- 1.6
-
stream
Streams the values for the given key.- Parameters:
key
-- Returns:
- The stream of values for the key, or an empty stream if the key is not present.
- Since:
- 1.6
-
streamAll
- Returns:
- A flattened Stream of all values in this Multimap.
- Since:
- 1.6
-
entrySet
-
size
public int size() -
size
Returns the number of values associated with the key.- Parameters:
key
-- Returns:
- The number of associated values, or
-1
if the key was not present. - Since:
- 1.6
-
equals
-
hashCode
public int hashCode() -
toString
-
isEmpty
public boolean isEmpty() -
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V> - Since:
- 1.6
-
putAll
-
keySet
-
values
-