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 Details

    • storage

      protected final Map<K,Collection<V>> 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

      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 Details

    • 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

      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

      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.
      Specified by:
      get in interface Map<K,V>
      Parameters:
      key -
      Returns:
      The associated values, or an empty collection if the key was not present.
    • 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.
      Specified by:
      remove in interface Map<K,V>
      Parameters:
      key -
      value -
      Returns:
      Whether the key was present and the associated collection of values was changed.
    • removeAll

      public boolean removeAll(Object key)
      Removes all values associated with the key, leaving the key itself in place, by calling Collection.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.
      Specified by:
      remove in interface Map<K,V>
      Parameters:
      key -
      Returns:
      The values associated with the key, or null if no mapping was present.
    • clear

      public void clear()
      Removes all mappings.
      Specified by:
      clear in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<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)
      Specified by:
      put in interface Map<K,V>
    • 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()
      Specified by:
      entrySet in interface Map<K,V>
      Returns:
      An immutable KeySetView of this Multimap.
      Since:
      1.6
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
      Returns:
      The number of keys.
      Since:
      1.6
    • 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)
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object
      Since:
      1.6
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
      Since:
      1.6
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
      Since:
      1.6
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
      Since:
      1.6
    • putAll

      public void putAll(Map<? extends K,? extends Collection<V>> m)
      Specified by:
      putAll in interface Map<K,V>
      Since:
      1.6
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
      Since:
      1.6
    • values

      public Collection<Collection<V>> values()
      Specified by:
      values in interface Map<K,V>
      Since:
      1.6