Class HashNMap

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public class HashNMap
    extends java.lang.Object
    implements java.io.Serializable, java.lang.Cloneable
    The HashNMap can be used to store multiple values by a single key value. The values stored can be retrieved using a direct query or by creating an enumeration over the stored elements.
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  HashNMap.EmptyIterator
      An helper class to implement an empty iterator.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.Object[] EMPTY_ARRAY
      An empty array.
      private static java.util.Iterator EMPTY_ITERATOR
      A singleton instance of the empty iterator.
      private static long serialVersionUID
      Serialization support.
      private java.util.HashMap table
      The underlying storage.
    • Constructor Summary

      Constructors 
      Constructor Description
      HashNMap()
      Default constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(java.lang.Object key, java.lang.Object val)
      Adds a new key/value pair into this map.
      void clear()
      Clears all keys and values of this map.
      java.lang.Object clone()
      Creates a deep copy of this HashNMap.
      boolean contains​(java.lang.Object value)
      Tests whether this map contains the given key or value.
      boolean containsKey​(java.lang.Object key)
      Tests whether this map contains the given key.
      boolean containsValue​(java.lang.Object value)
      Tests whether this map contains the given value.
      boolean containsValue​(java.lang.Object key, java.lang.Object value)
      Tests whether this map contains the given value.
      protected java.util.List createList()
      Returns a new empty list.
      java.lang.Object get​(java.lang.Object key, int n)
      Retrieves the n-th value registered for an key or null if there was no such key in the list.
      java.util.Iterator getAll​(java.lang.Object key)
      Returns an iterator over all elements registered to the given key.
      java.lang.Object getFirst​(java.lang.Object key)
      Retrieves the first value registered for an key or null if there was no such key in the list.
      int getValueCount​(java.lang.Object key)
      Returns the number of elements registered with the given key.
      java.util.Iterator keys()
      Returns all registered keys as an enumeration.
      java.util.Set keySet()
      Returns all registered keys as set.
      boolean put​(java.lang.Object key, java.lang.Object val)
      Inserts a new key/value pair into the map.
      boolean remove​(java.lang.Object key, java.lang.Object value)
      Removes the key/value pair from the map.
      void removeAll​(java.lang.Object key)
      Removes all elements for the given key.
      java.lang.Object[] toArray​(java.lang.Object key)
      Returns the contents for the given key as object array.
      java.lang.Object[] toArray​(java.lang.Object key, java.lang.Object[] data)
      Returns the contents for the given key as object array.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • serialVersionUID

        private static final long serialVersionUID
        Serialization support.
        See Also:
        Constant Field Values
      • EMPTY_ITERATOR

        private static final java.util.Iterator EMPTY_ITERATOR
        A singleton instance of the empty iterator. This object can be safely shared.
      • table

        private java.util.HashMap table
        The underlying storage.
      • EMPTY_ARRAY

        private static final java.lang.Object[] EMPTY_ARRAY
        An empty array.
    • Constructor Detail

      • HashNMap

        public HashNMap()
        Default constructor.
    • Method Detail

      • createList

        protected java.util.List createList()
        Returns a new empty list.
        Returns:
        A new empty list.
      • put

        public boolean put​(java.lang.Object key,
                           java.lang.Object val)
        Inserts a new key/value pair into the map. If such a pair already exists, it gets replaced with the given values.
        Parameters:
        key - the key.
        val - the value.
        Returns:
        A boolean.
      • add

        public boolean add​(java.lang.Object key,
                           java.lang.Object val)
        Adds a new key/value pair into this map. If the key is not yet in the map, it gets added to the map and the call is equal to put(Object,Object).
        Parameters:
        key - the key.
        val - the value.
        Returns:
        true, if the value has been added, false otherwise
      • getFirst

        public java.lang.Object getFirst​(java.lang.Object key)
        Retrieves the first value registered for an key or null if there was no such key in the list.
        Parameters:
        key - the key.
        Returns:
        the value.
      • get

        public java.lang.Object get​(java.lang.Object key,
                                    int n)
        Retrieves the n-th value registered for an key or null if there was no such key in the list. An index out of bounds exception is thrown if there are less than n elements registered to this key.
        Parameters:
        key - the key.
        n - the index.
        Returns:
        the object.
      • getAll

        public java.util.Iterator getAll​(java.lang.Object key)
        Returns an iterator over all elements registered to the given key.
        Parameters:
        key - the key.
        Returns:
        an iterator.
      • keys

        public java.util.Iterator keys()
        Returns all registered keys as an enumeration.
        Returns:
        an enumeration of the keys.
      • keySet

        public java.util.Set keySet()
        Returns all registered keys as set.
        Returns:
        a set of keys.
      • remove

        public boolean remove​(java.lang.Object key,
                              java.lang.Object value)
        Removes the key/value pair from the map. If the removed entry was the last entry for this key, the key gets also removed.
        Parameters:
        key - the key.
        value - the value.
        Returns:
        true, if removing the element was successfull, false otherwise.
      • removeAll

        public void removeAll​(java.lang.Object key)
        Removes all elements for the given key.
        Parameters:
        key - the key.
      • clear

        public void clear()
        Clears all keys and values of this map.
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Tests whether this map contains the given key.
        Parameters:
        key - the key.
        Returns:
        true if the key is contained in the map
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Tests whether this map contains the given value.
        Parameters:
        value - the value.
        Returns:
        true if the value is registered in the map for an key.
      • containsValue

        public boolean containsValue​(java.lang.Object key,
                                     java.lang.Object value)
        Tests whether this map contains the given value.
        Parameters:
        value - the value.
        key - the key under which to find the value
        Returns:
        true if the value is registered in the map for an key.
      • contains

        public boolean contains​(java.lang.Object value)
        Tests whether this map contains the given key or value.
        Parameters:
        value - the value.
        Returns:
        true if the key or value is contained in the map
      • clone

        public java.lang.Object clone()
                               throws java.lang.CloneNotSupportedException
        Creates a deep copy of this HashNMap.
        Overrides:
        clone in class java.lang.Object
        Returns:
        a clone.
        Throws:
        java.lang.CloneNotSupportedException - this should never happen.
      • toArray

        public java.lang.Object[] toArray​(java.lang.Object key,
                                          java.lang.Object[] data)
        Returns the contents for the given key as object array. If there were no objects registered with that key, an empty object array is returned.
        Parameters:
        key - the key.
        data - the object array to receive the contents.
        Returns:
        the contents.
      • toArray

        public java.lang.Object[] toArray​(java.lang.Object key)
        Returns the contents for the given key as object array. If there were no objects registered with that key, an empty object array is returned.
        Parameters:
        key - the key.
        Returns:
        the contents.
      • getValueCount

        public int getValueCount​(java.lang.Object key)
        Returns the number of elements registered with the given key.
        Parameters:
        key - the key.
        Returns:
        the number of element for this key, or 0 if there are no elements registered.