Class CheckedHashMap<K,​V>

  • Type Parameters:
    K - The type of keys in the map.
    V - The type of values in the map.
    All Implemented Interfaces:
    Serializable, Cloneable, Map<K,​V>, Cloneable

    public class CheckedHashMap<K,​V>
    extends LinkedHashMap<K,​V>
    implements Cloneable
    A checked and synchronized Map. Type checks are performed at run-time in addition of compile-time checks. The synchronization lock can be modified at runtime by overriding the getLock() method.

    This class is similar to using the wrappers provided in Collections, minus the cost of indirection levels and with the addition of overrideable methods.

    Since:
    2.1
    Author:
    Jody Garnett (Refractions Research), Martin Desruisseaux (IRD)
    See Also:
    Collections#checkedMap, Collections#synchronizedMap, Serialized Form
    • Constructor Detail

      • CheckedHashMap

        public CheckedHashMap​(Class<K> keyType,
                              Class<V> valueType)
        Constructs a map of the specified type.
        Parameters:
        keyType - The key type (should not be null).
        valueType - The value type (should not be null).
    • Method Detail

      • checkWritePermission

        protected void checkWritePermission()
                                     throws UnsupportedOperationException
        Checks if changes in this collection are allowed. This method is automatically invoked after this collection got the lock and before any operation that may change the content. The default implementation does nothing (i.e. this collection is modifiable). Subclasses should override this method if they want to control write access.
        Throws:
        UnsupportedOperationException - if this collection is unmodifiable.
        Since:
        2.5
      • getLock

        protected Object getLock()
        Returns the synchronization lock. The default implementation returns this. Subclasses that override this method should be careful to update the lock reference when this set is cloned.
        Returns:
        The synchronization lock.
        Since:
        2.5
      • size

        public int size()
        Returns the number of elements in this map.
        Specified by:
        size in interface Map<K,​V>
        Overrides:
        size in class HashMap<K,​V>
      • isEmpty

        public boolean isEmpty()
        Returns true if this map contains no elements.
        Specified by:
        isEmpty in interface Map<K,​V>
        Overrides:
        isEmpty in class HashMap<K,​V>
      • containsKey

        public boolean containsKey​(Object key)
        Returns true if this map contains the specified key.
        Specified by:
        containsKey in interface Map<K,​V>
        Overrides:
        containsKey in class HashMap<K,​V>
      • get

        public V get​(Object key)
        Returns the value to which the specified key is mapped, or null if none.
        Specified by:
        get in interface Map<K,​V>
        Overrides:
        get in class LinkedHashMap<K,​V>
      • put

        public V put​(K key,
                     V value)
              throws IllegalArgumentException,
                     UnsupportedOperationException
        Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.
        Specified by:
        put in interface Map<K,​V>
        Overrides:
        put in class HashMap<K,​V>
        Parameters:
        key - key with which the specified value is to be associated.
        value - value to be associated with the specified key.
        Returns:
        previous value associated with specified key, or null.
        Throws:
        IllegalArgumentException - if the key or the value is not of the expected type.
        UnsupportedOperationException - if this collection is unmodifiable.
      • equals

        public boolean equals​(Object o)
        Compares the specified object with this map for equality.
        Specified by:
        equals in interface Map<K,​V>
        Overrides:
        equals in class AbstractMap<K,​V>
      • hashCode

        public int hashCode()
        Returns the hash code value for this map.
        Specified by:
        hashCode in interface Map<K,​V>
        Overrides:
        hashCode in class AbstractMap<K,​V>