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>>
public class Multimap<K,V> extends Object implements Map<K,Collection<V>>
A Map which allows each to be associated with multiple values.- Since:
- 1.6 implements Map interface, additional utility methods
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
isConcurrent
protected boolean
isReadOptimized
protected Map<K,Collection<V>>
storage
-
Constructor Summary
Constructors Constructor Description Multimap()
Creates a non-concurrent Multimap.Multimap(boolean concurrent)
Multimap(boolean concurrent, boolean readOnly)
Multimap(boolean concurrent, boolean readOnly, Map<K,? extends Collection<V>> other)
Multimap(Multimap<K,V> other)
Copy constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all mappings.boolean
containsKey(Object key)
boolean
containsValue(Object value)
Set<Map.Entry<K,Collection<V>>>
entrySet()
boolean
equals(Object obj)
Collection<V>
get(Object key)
Returns all values associated with the key.Collection<V>
getMutable(Object key)
Returns all values associated with the key.int
hashCode()
boolean
hasKey(K key)
boolean
isEmpty()
boolean
isThreadSafe()
Set<K>
keySet()
protected Collection<V>
newCollection()
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 Map<K,Collection<V>>
newMapDelegate()
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.Collection<V>
put(K key, Collection<V> values)
Collection<V>
put(K key, Collection<V> values, boolean wrap)
Associates the key with the given values, overwriting the entry if the key is present.boolean
put(K key, V value)
Adds the given value to the associations for the key.void
putAll(Map<? extends K,? extends Collection<V>> m)
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.Collection<V>
remove(Object key)
Deletes the specified key from this map.boolean
remove(Object key, Object value)
Removes the specified value associated with the given key.boolean
removeAll(Object key)
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
size(K key)
Returns the number of values associated with the key.Stream<V>
stream(Object key)
Streams the values for the given key.Stream<V>
streamAll()
String
toString()
Collection<Collection<V>>
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 Detail
-
storage
protected final Map<K,Collection<V>> storage
-
isConcurrent
protected final boolean isConcurrent
-
isReadOptimized
protected final boolean isReadOptimized
-
-
Constructor Detail
-
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
public Multimap(boolean concurrent, boolean readOnly, Map<K,? extends Collection<V>> other)
- 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
public Multimap(Multimap<K,V> other)
Copy constructor.- Parameters:
other
- The Multimap to copy from.- Throws:
NullPointerException
- If the Multimap is null.
-
-
Method Detail
-
newMapDelegate
protected final Map<K,Collection<V>> newMapDelegate()
Convenience method for creating a new underlying Multimap.- Parameters:
initial
- The elements to add to the multimap.- Since:
- 1.6
- See Also:
newMapDelegate(Map)
-
newMapDelegate
protected Map<K,Collection<V>> newMapDelegate(Map<? extends K,? extends Collection<V>> initial)
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
protected final Collection<V> 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(Collection)
-
newCollection
protected Collection<V> newCollection(Collection<? extends V> values)
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
public Collection<V> get(Object key)
Returns all values associated with the key. The returned collection is immutable.
-
getMutable
public Collection<V> getMutable(Object key)
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
public boolean put(K key, V value)
Adds the given value to the associations for the key.- Parameters:
key
-value
-- Returns:
- Whether a mapping existed for the key.
-
putIfPresent
public boolean putIfPresent(Object key, V value)
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
public 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.- 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
public boolean remove(Object key, Object value)
Removes the specified value associated with the given key.
-
removeAll
public boolean removeAll(Object key)
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
public boolean removeAll(Object key, Collection<V> values)
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
public Collection<V> remove(Object key)
Deletes the specified key from this map.
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKey
in interfaceMap<K,V>
- Parameters:
key
-- Returns:
- Whether the given key has one or more values associated with it.
-
hasKey
public boolean hasKey(K key)
- Parameters:
key
-- Returns:
- Whether the key is present in they keySet.
- Since:
- 1.6
-
putAll
public final boolean putAll(K key, Collection<V> values)
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
public boolean putAll(K key, Collection<V> values, boolean wrap)
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
public boolean replaceValues(K key, Collection<V> values)
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
public Collection<V> put(K key, Collection<V> values, boolean wrap)
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
public Collection<V> put(K key, Collection<V> values)
-
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
public Stream<V> stream(Object key)
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
public Stream<V> streamAll()
- Returns:
- A flattened Stream of all values in this Multimap.
- Since:
- 1.6
-
entrySet
public Set<Map.Entry<K,Collection<V>>> entrySet()
-
size
public int size()
-
size
public int size(K key)
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
public boolean equals(Object obj)
-
hashCode
public int hashCode()
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap<K,V>
- Since:
- 1.6
-
putAll
public void putAll(Map<? extends K,? extends Collection<V>> m)
-
values
public Collection<Collection<V>> values()
-
-