viceroy.misc.typeSafeCollections
Class TypeSafeMap

java.lang.Object
  |
  +--viceroy.misc.typeSafeCollections.TypeSafeMap
All Implemented Interfaces:
java.util.Map
Direct Known Subclasses:
TypeSafeSortedMap

class TypeSafeMap
extends java.lang.Object
implements java.util.Map

Implementation of the java.util.Map class which accepts only key and value objects of a given type for put operations. Is backed by another implementation of Map.


Nested Class Summary
 
Nested classes inherited from class java.util.Map
java.util.Map.Entry
 
Field Summary
private  java.lang.Class _keytype
           
private  java.util.Map _map
           
private  java.lang.Class _valuetype
           
 
Constructor Summary
TypeSafeMap(java.util.Map map, java.lang.Class keyType, java.lang.Class valueType)
          Constructs a new TypeSafeMap object.
 
Method Summary
protected  void checkKeyType(java.lang.Object o)
          Tests whether a key object is an instance of the allowed type.
protected  void checkType(java.lang.Object o, java.lang.Class type)
          Tests whether an object is an instance of the allowed type.
protected  void checkValueType(java.lang.Object o)
          Tests whether a value object is an instance of the allowed type.
 void clear()
          see java.util.Map
 boolean containsKey(java.lang.Object key)
          see java.util.Map
 boolean containsValue(java.lang.Object value)
          see java.util.Map
 java.util.Set entrySet()
          see java.util.Map Returns a type safe java.util.Set which prevents values of the wrong type to be set via the stored java.util.Map.Entry object.
 boolean equals(java.lang.Object o)
          see java.util.Map
 java.lang.Object get(java.lang.Object key)
          see java.util.Map
protected  java.lang.Class getKeyType()
          Returns the type of the keys.
protected  java.lang.Class getValueType()
          Returns the type of the values.
 boolean isEmpty()
          see java.util.Map
 java.util.Set keySet()
          see java.util.Map
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          see java.util.Map Checks the types of the key and value objects
 void putAll(java.util.Map t)
          see java.util.Map Checks the types of the key and value objects in the Map
 java.lang.Object remove(java.lang.Object key)
          see java.util.Map
 int size()
          see java.util.Map
 java.util.Collection values()
          see java.util.Map
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
hashCode
 

Field Detail

_map

private java.util.Map _map

_keytype

private java.lang.Class _keytype

_valuetype

private java.lang.Class _valuetype
Constructor Detail

TypeSafeMap

public TypeSafeMap(java.util.Map map,
                   java.lang.Class keyType,
                   java.lang.Class valueType)
Constructs a new TypeSafeMap object. if performTypeCheck flag is true, it iterates all over the keys and the values, and checks that the types existing in the giving map match keyType and valueType

Parameters:
map - the map to be wrapped
keyType - the type of the objects which can be used as keys
valueType - the type of the objects which can be added as values
Method Detail

checkType

protected void checkType(java.lang.Object o,
                         java.lang.Class type)
Tests whether an object is an instance of the allowed type.

Parameters:
o - The object to be checked
Throws:
java.lang.ClassCastException - if the object is not of the right type

checkKeyType

protected void checkKeyType(java.lang.Object o)
Tests whether a key object is an instance of the allowed type.

Parameters:
o - The key object to be checked
Throws:
java.lang.ClassCastException - if the object is not of the right type

checkValueType

protected void checkValueType(java.lang.Object o)
Tests whether a value object is an instance of the allowed type.

Parameters:
o - The value object to be checked
Throws:
java.lang.ClassCastException - if the object is not of the right type

getKeyType

protected java.lang.Class getKeyType()
Returns the type of the keys.

Returns:
The class descriptor for the accepted key objects

getValueType

protected java.lang.Class getValueType()
Returns the type of the values.

Returns:
The class descriptor for the accepted value objects

size

public int size()
see java.util.Map

Specified by:
size in interface java.util.Map

isEmpty

public boolean isEmpty()
see java.util.Map

Specified by:
isEmpty in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
see java.util.Map

Specified by:
containsKey in interface java.util.Map

containsValue

public boolean containsValue(java.lang.Object value)
see java.util.Map

Specified by:
containsValue in interface java.util.Map

get

public java.lang.Object get(java.lang.Object key)
see java.util.Map

Specified by:
get in interface java.util.Map

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
see java.util.Map Checks the types of the key and value objects

Specified by:
put in interface java.util.Map

remove

public java.lang.Object remove(java.lang.Object key)
see java.util.Map

Specified by:
remove in interface java.util.Map

putAll

public void putAll(java.util.Map t)
see java.util.Map Checks the types of the key and value objects in the Map

Specified by:
putAll in interface java.util.Map

clear

public void clear()
see java.util.Map

Specified by:
clear in interface java.util.Map

keySet

public java.util.Set keySet()
see java.util.Map

Specified by:
keySet in interface java.util.Map

values

public java.util.Collection values()
see java.util.Map

Specified by:
values in interface java.util.Map

entrySet

public java.util.Set entrySet()
see java.util.Map Returns a type safe java.util.Set which prevents values of the wrong type to be set via the stored java.util.Map.Entry object.

Specified by:
entrySet in interface java.util.Map

equals

public boolean equals(java.lang.Object o)
see java.util.Map

Specified by:
equals in interface java.util.Map
Overrides:
equals in class java.lang.Object