org.geotools.util
Class CheckedHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<K,V>
              extended by org.geotools.util.CheckedHashMap<K,V>
Type Parameters:
K - The type of keys in the map.
V - The type of values in the map.
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<K,V>

public class CheckedHashMap<K,V>
extends java.util.LinkedHashMap<K,V>
implements org.opengis.util.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
Version:
$Id: CheckedHashMap.java 30852 2008-07-03 15:59:42Z desruisseaux $
Author:
Jody Garnett (Refractions Research), Martin Desruisseaux (IRD)
See Also:
Collections#checkedMap, Collections#synchronizedMap, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
CheckedHashMap(java.lang.Class<K> keyType, java.lang.Class<V> valueType)
          Constructs a map of the specified type.
 
Method Summary
protected  void checkWritePermission()
          Checks if changes in this collection are allowed.
 void clear()
          Removes all of the elements from this map.
 CheckedHashMap<K,V> clone()
          Returns a shallow copy of this map.
 boolean containsKey(java.lang.Object key)
          Returns true if this map contains the specified key.
 boolean containsValue(java.lang.Object value)
          Returns true if this map contains the specified value.
 boolean equals(java.lang.Object o)
          Compares the specified object with this map for equality.
 V get(java.lang.Object key)
          Returns the value to which the specified key is mapped, or null if none.
protected  java.lang.Object getLock()
          Returns the synchronization lock.
 int hashCode()
          Returns the hash code value for this map.
 boolean isEmpty()
          Returns true if this map contains no elements.
 V put(K key, V value)
          Associates the specified value with the specified key in this map.
 void putAll(java.util.Map<? extends K,? extends V> m)
          Copies all of the mappings from the specified map to this map.
 V remove(java.lang.Object key)
          Removes the mapping for the specified key from this map if present.
 int size()
          Returns the number of elements in this map.
 java.lang.String toString()
          Returns a string representation of this map.
 
Methods inherited from class java.util.LinkedHashMap
removeEldestEntry
 
Methods inherited from class java.util.HashMap
entrySet, keySet, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
entrySet, keySet, values
 

Constructor Detail

CheckedHashMap

public CheckedHashMap(java.lang.Class<K> keyType,
                      java.lang.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 java.lang.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:
java.lang.UnsupportedOperationException - if this collection is unmodifiable.
Since:
2.5

getLock

protected java.lang.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 java.util.Map<K,V>
Overrides:
size in class java.util.HashMap<K,V>

isEmpty

public boolean isEmpty()
Returns true if this map contains no elements.

Specified by:
isEmpty in interface java.util.Map<K,V>
Overrides:
isEmpty in class java.util.HashMap<K,V>

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this map contains the specified key.

Specified by:
containsKey in interface java.util.Map<K,V>
Overrides:
containsKey in class java.util.HashMap<K,V>

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if this map contains the specified value.

Specified by:
containsValue in interface java.util.Map<K,V>
Overrides:
containsValue in class java.util.LinkedHashMap<K,V>

get

public V get(java.lang.Object key)
Returns the value to which the specified key is mapped, or null if none.

Specified by:
get in interface java.util.Map<K,V>
Overrides:
get in class java.util.LinkedHashMap<K,V>

put

public V put(K key,
             V value)
      throws java.lang.IllegalArgumentException,
             java.lang.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 java.util.Map<K,V>
Overrides:
put in class java.util.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:
java.lang.IllegalArgumentException - if the key or the value is not of the expected type.
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

putAll

public void putAll(java.util.Map<? extends K,? extends V> m)
            throws java.lang.UnsupportedOperationException
Copies all of the mappings from the specified map to this map.

Specified by:
putAll in interface java.util.Map<K,V>
Overrides:
putAll in class java.util.HashMap<K,V>
Throws:
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

remove

public V remove(java.lang.Object key)
         throws java.lang.UnsupportedOperationException
Removes the mapping for the specified key from this map if present.

Specified by:
remove in interface java.util.Map<K,V>
Overrides:
remove in class java.util.HashMap<K,V>
Throws:
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

clear

public void clear()
           throws java.lang.UnsupportedOperationException
Removes all of the elements from this map.

Specified by:
clear in interface java.util.Map<K,V>
Overrides:
clear in class java.util.LinkedHashMap<K,V>
Throws:
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

toString

public java.lang.String toString()
Returns a string representation of this map.

Overrides:
toString in class java.util.AbstractMap<K,V>

equals

public boolean equals(java.lang.Object o)
Compares the specified object with this map for equality.

Specified by:
equals in interface java.util.Map<K,V>
Overrides:
equals in class java.util.AbstractMap<K,V>

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.util.AbstractMap<K,V>

clone

public CheckedHashMap<K,V> clone()
Returns a shallow copy of this map.

Overrides:
clone in class java.util.HashMap<K,V>
Returns:
A shallow copy of this map.


Copyright © 1996-2010 Geotools. All Rights Reserved.