Class KeyedItemHashMap<T extends KeyedItem>

  • All Implemented Interfaces:
    Map<String,​T>

    public final class KeyedItemHashMap<T extends KeyedItem>
    extends Object
    implements Map<String,​T>
    Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. Unlike a typical hash map, this implementation uses KeyedItem's as the value. A KeyedItem must implement the KeyedItem interface and provide a unique ID to avoid collisions within the map. For more details see the HashMap class.
    • Constructor Detail

      • KeyedItemHashMap

        public KeyedItemHashMap()
        Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
    • Method Detail

      • size

        public int size()
        Returns the number of key-value mappings in this map.
        Specified by:
        size in interface Map<String,​T extends KeyedItem>
        Returns:
        The number of key-value mappings in this map.
      • isEmpty

        public boolean isEmpty()
        Returns true if this map contains no key-value mappings.
        Specified by:
        isEmpty in interface Map<String,​T extends KeyedItem>
        Returns:
        true if this map contains no key-value mappings.
      • containsKey

        public boolean containsKey​(Object key)
        Returns true if this map contains a mapping for the specified key.
        Specified by:
        containsKey in interface Map<String,​T extends KeyedItem>
        Parameters:
        key - The key whose presence in this map is to be tested.
        Returns:
        true if this map contains a mapping for the specified key.
      • containsValue

        public boolean containsValue​(Object keyedItem)
        Returns true if this map maps a key to the specified value.
        Specified by:
        containsValue in interface Map<String,​T extends KeyedItem>
        Parameters:
        keyedItem - value whose presence in this map is to be tested.
        Returns:
        true if this map maps the value to an id of an item.
      • get

        public T get​(Object obj)
        Returns the string representation of the Object argument.
        Specified by:
        get in interface Map<String,​T extends KeyedItem>
        Parameters:
        obj - an Object.
        Returns:
        if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
        See Also:
        Object.toString()
      • put

        public T put​(String key,
                     T keyedItem)
        Associates the specified value with the keyedItem's ID in this map. If the map previously contained a mapping for the keyedItem's ID, the old value is replaced.
        Specified by:
        put in interface Map<String,​T extends KeyedItem>
        Parameters:
        key - UNUSED here but necessary for override.
        keyedItem - Value that implements the KeyedItem interface. The getId() function will be used to determine the key for the map.
        Returns:
        the previous value associated with keyedItem.getId(), or null if there was no mapping for keyedItem.getId(). (A null return can also indicate that the map previously associated null with keyedItem.getId().)
      • put

        public T put​(T keyedItem)
        Associates the specified value with the keyedItem's ID in this map. If the map previously contained a mapping for the keyedItem's ID, the old value is replaced.
        Parameters:
        keyedItem - Value that implements the KeyedItem interface. The getId() function will be used to determine the key for the map.
        Returns:
        the previous value associated with keyedItem.getId(), or null if there was no mapping for keyedItem.getId(). (A null return can also indicate that the map previously associated null with keyedItem.getId().)
      • remove

        public T remove​(Object keyedItem)
        Removes the mapping for the specified keyedItem's ID from this map if present.
        Specified by:
        remove in interface Map<String,​T extends KeyedItem>
        Parameters:
        keyedItem - KeyedItem whose mapping is to be removed from the map.
        Returns:
        the previous value associated with keyedItem.getId(), or null if there was no mapping for keyedItem.getId(). (A null return can also indicate that the map previously associated null with keyedItem.getId().)
        Throws:
        ClassCastException - if the keyedItem does not implement the KeyedItem interface.
      • putAll

        public void putAll​(Map<? extends String,​? extends T> map)
        Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
        Specified by:
        putAll in interface Map<String,​T extends KeyedItem>
        Parameters:
        map - mappings to be stored in this map
        Throws:
        NullPointerException - if the specified map is null
      • clear

        public void clear()
        Removes all of the mappings from this map. The map will be empty after this call returns.
        Specified by:
        clear in interface Map<String,​T extends KeyedItem>
      • keySet

        public Set<String> 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 (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.
        Specified by:
        keySet in interface Map<String,​T extends KeyedItem>
        Returns:
        a set view of the keys contained in this map
      • values

        public Collection<T> values()
        Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
        Specified by:
        values in interface Map<String,​T extends KeyedItem>
        Returns:
        a view of the values contained in this map
      • entrySet

        public Set<Map.Entry<String,​T>> entrySet()
        Returns a Set view of the mappings 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 (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
        Specified by:
        entrySet in interface Map<String,​T extends KeyedItem>
        Returns:
        a set view of the mappings contained in this map