Class MultiCollection<K,​V>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<K,​java.util.Collection<V>>
    Direct Known Subclasses:
    TwoWayMultiCollection

    public class MultiCollection<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​java.util.Collection<V>>, java.io.Serializable, java.lang.Cloneable
    An MultiCollection is a map with the ability to store more than one element per key (an collection).
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Map<K,​java.util.Collection<V>> map
      The map.
      protected java.lang.Class<?> type
      The collection type.
    • Constructor Summary

      Constructors 
      Constructor Description
      MultiCollection()
      Create a new multi collection.
      MultiCollection​(java.util.Map<K,​java.util.Collection<V>> map, java.lang.Class<?> type)
      Create a new multi collection.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Collection<V> add​(K key, java.util.Collection<V> value)
      Associates the specified value with the specified key in this map (optional operation).
      java.util.Collection<V> add​(K key, V value)
      Associates the specified value with the specified key in this map (optional operation).
      void addAll​(java.util.Map<? extends K,​? extends V> t)
      Add all elements of another map.
      void clear()
      Removes all mappings from this map.
      java.lang.Object clone()
      Clone a multi collection.
      boolean containsKey​(java.lang.Object key)
      Returns
      boolean containsValue​(java.lang.Object value)
      Returns
      java.util.Collection<V> createCollection​(K key)
      Create a collection instance.
      java.util.Set<java.util.Map.Entry<K,​java.util.Collection<V>>> entrySet()
      Returns a set view of the mappings contained in this map.
      boolean equals​(java.lang.Object o)
      Compares the specified object with this map for equality.
      java.util.Collection<V> get​(java.lang.Object key)
      Returns the collection to which this map maps the specified key.
      java.util.Collection<V> getCollection​(java.lang.Object key)
      Get the values associated to a key as collection.
      java.lang.Object[] getKeys()
      Get the keys as array.
      java.lang.Object[] getKeys​(java.lang.Class<?> type)
      Get the keys as array.
      V getObject​(java.lang.Object key)
      Get first object for a key (unwraps from collection).
      java.lang.Object[] getObjects()
      Get the values as array.
      java.lang.Object[] getObjects​(java.lang.Class<?> type)
      Get the values as array.
      int hashCode()
      Returns the hash code value for this map.
      boolean isEmpty()
      Returns
      java.util.Set<K> keySet()
      Returns a set view of the keys contained in this map.
      java.util.Collection<V> put​(K key, java.util.Collection<V> value)
      Associates the specified value with the specified key in this map (optional operation).
      void putAll​(java.util.Map<? extends K,​? extends java.util.Collection<V>> t)
      Copies all of the mappings from the specified map to this map (optional operation).
      java.util.Collection<V> remove​(java.lang.Object key)
      Removes the mapping for this key from this map if it is present.
      void removeObject​(java.lang.Object key, java.lang.Object value)
      Remove a special object from the collection of a defined key.
      int size()
      Returns the number of elements added to this map.
      java.lang.String toString()
      Create a string representation of this map.
      java.util.Collection<java.util.Collection<V>> values()
      Unsupported Operation.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • map

        protected java.util.Map<K,​java.util.Collection<V>> map
        The map.
      • type

        protected java.lang.Class<?> type
        The collection type.
    • Constructor Detail

      • MultiCollection

        public MultiCollection()
        Create a new multi collection.
      • MultiCollection

        public MultiCollection​(java.util.Map<K,​java.util.Collection<V>> map,
                               java.lang.Class<?> type)
        Create a new multi collection.
        Parameters:
        map - The undelying map.
        type - The collection type to use (requires public empty contstructor and has to implement java.util.Collection).
    • Method Detail

      • clone

        public java.lang.Object clone()
                               throws java.lang.CloneNotSupportedException
        Clone a multi collection.
        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException
      • size

        public int size()
        Returns the number of elements added to this map.
        Specified by:
        size in interface java.util.Map<K,​V>
      • isEmpty

        public boolean isEmpty()
        Returns
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Returns:
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Returns
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Parameters:
        key - key whose presence in this map is to be tested.
        Returns:
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - if the key is
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Returns
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Parameters:
        value - value whose presence in this map is to be tested.
        Returns:
        Throws:
        java.lang.ClassCastException - if the value is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - if the value is
      • get

        public java.util.Collection<V> get​(java.lang.Object key)
        Returns the collection to which this map maps the specified key. Returns
        Specified by:
        get in interface java.util.Map<K,​V>
        Parameters:
        key - key whose associated collection is to be returned.
        Returns:
        the collection to which this map maps the specified key, or
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - key is
        See Also:
        containsKey(Object)
      • getObject

        public V getObject​(java.lang.Object key)
        Get first object for a key (unwraps from collection).
        Parameters:
        key - The key.
        Returns:
        The (first) value or null.
      • put

        public java.util.Collection<V> put​(K key,
                                           java.util.Collection<V> value)
        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 map m.containsKey(k) would return
        Specified by:
        put in interface java.util.Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated.
        value - value to be associated with the specified key.
        Returns:
        The collection associated to the key.
        Throws:
        java.lang.UnsupportedOperationException - if the
        java.lang.ClassCastException - if the class of the specified key or value prevents it from being stored in this map.
        java.lang.IllegalArgumentException - if some aspect of this key or value prevents it from being stored in this map.
        java.lang.NullPointerException - this map does not permit
      • add

        public java.util.Collection<V> add​(K key,
                                           V value)
        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 map m.containsKey(k) would return
        Parameters:
        key - key with which the specified value is to be associated.
        value - value to be associated with the specified key.
        Returns:
        The collection associated to the key.
        Throws:
        java.lang.UnsupportedOperationException - if the
        java.lang.ClassCastException - if the class of the specified key or value prevents it from being stored in this map.
        java.lang.IllegalArgumentException - if some aspect of this key or value prevents it from being stored in this map.
        java.lang.NullPointerException - this map does not permit
      • add

        public java.util.Collection<V> add​(K key,
                                           java.util.Collection<V> value)
        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 map m.containsKey(k) would return
        Parameters:
        key - key with which the specified value is to be associated.
        value - value to be associated with the specified key.
        Returns:
        The collection associated to the key.
        Throws:
        java.lang.UnsupportedOperationException - if the
        java.lang.ClassCastException - if the class of the specified key or value prevents it from being stored in this map.
        java.lang.IllegalArgumentException - if some aspect of this key or value prevents it from being stored in this map.
        java.lang.NullPointerException - this map does not permit
      • addAll

        public void addAll​(java.util.Map<? extends K,​? extends V> t)
        Add all elements of another map.
        Parameters:
        t - The other map.
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends java.util.Collection<V>> t)
        Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Parameters:
        t - Mappings to be stored in this map.
        Throws:
        java.lang.UnsupportedOperationException - if the
        java.lang.ClassCastException - if the class of a key or value in the specified map prevents it from being stored in this map.
        java.lang.IllegalArgumentException - some aspect of a key or value in the specified map prevents it from being stored in this map.
        java.lang.NullPointerException - the specified map is
      • clear

        public void clear()
        Removes all mappings from this map.
        Specified by:
        clear in interface java.util.Map<K,​V>
      • keySet

        public java.util.Set<K> 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 the
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Returns:
        a set view of the keys contained in this map.
      • values

        public java.util.Collection<java.util.Collection<V>> values()
        Unsupported Operation.
        Specified by:
        values in interface java.util.Map<K,​V>
        Throws:
        java.lang.UnsupportedOperationException
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​java.util.Collection<V>>> entrySet()
        Returns a set view of the mappings contained in this map. Each element in the returned set is a map 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 the
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Returns:
        a set view of the mappings contained in this map.
      • equals

        public boolean equals​(java.lang.Object o)
        Compares the specified object with this map for equality. Returns
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - object to be compared for equality with this map.
        Returns:
      • hashCode

        public int hashCode()
        Returns the hash code value for this map.
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code value for this map.
        See Also:
        Object.hashCode(), Object.equals(Object), equals(Object)
      • toString

        public java.lang.String toString()
        Create a string representation of this map.
        Overrides:
        toString in class java.lang.Object
      • remove

        public java.util.Collection<V> remove​(java.lang.Object key)
        Removes the mapping for this key from this map if it is present. More formally, if this map contains a mapping from key (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the map.
        Returns:
        collection associated with specified key, or
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - if the key is
      • getCollection

        public java.util.Collection<V> getCollection​(java.lang.Object key)
        Get the values associated to a key as collection.
        Parameters:
        key - The key.
        Returns:
        The collection of associated values.
      • getObjects

        public java.lang.Object[] getObjects()
        Get the values as array.
        Returns:
        The array of values.
      • getObjects

        public java.lang.Object[] getObjects​(java.lang.Class<?> type)
        Get the values as array.
        Parameters:
        type - The component type of the array.
        Returns:
        The array of values.
      • getKeys

        public java.lang.Object[] getKeys()
        Get the keys as array.
        Returns:
        The array of keys.
      • getKeys

        public java.lang.Object[] getKeys​(java.lang.Class<?> type)
        Get the keys as array.
        Parameters:
        type - The component type of the array.
        Returns:
        The array of keys.
      • removeObject

        public void removeObject​(java.lang.Object key,
                                 java.lang.Object value)
        Remove a special object from the collection of a defined key.
      • createCollection

        public java.util.Collection<V> createCollection​(K key)
        Create a collection instance.