com.massimo.utils
Class HierarchicalMap<V>

java.lang.Object
  extended by com.massimo.utils.HierarchicalMap<V>
Type Parameters:
V -
All Implemented Interfaces:
Map<String,V>

public class HierarchicalMap<V>
extends Object
implements Map<String,V>

A simple heirachical Map implementation. The key must be a String. Each instance can access but not modify it's parent's entries. Any parent entries are considered part of the hierarchical Map for the purpose of size, isEmpty(), contains, and for key, entry and value collections. However, put, remove and clear act on the current, internal Map instance only. The optional name is provided to help identify levels and nodes of the hierarchy.


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  String name
          An optional instance name
protected  Map<String,V> node
          A Map for this level of the context heirachy
protected  Map<String,V> parent
          The parent level of the context heirachy.
 
Constructor Summary
HierarchicalMap(int initialCapacity, Map<String,V> parent)
           
HierarchicalMap(Map<String,V> parent)
           
HierarchicalMap(String name, boolean caseSensitive, int initialCapacity, Map<String,V> parent)
           
HierarchicalMap(String name, boolean caseSensitive, Map<String,V> parent)
           
HierarchicalMap(String name, int initialCapacity, Map<String,V> parent)
           
HierarchicalMap(String name, Map<String,V> parent)
           
 
Method Summary
 void clear()
          Implement Map Removes all mappings from this node.
 boolean containsKey(Object key)
          Implement Map
 boolean containsValue(Object value)
          Implement Map
 Set<Map.Entry<String,V>> entrySet()
          Implement Map
 boolean equals(Object o)
          Implement Map
 V get(Object key)
          Implement Map
 V getLocal(Object key)
          As for Map.get(Object) but only look at the current leaf node.
 String getName()
           
 Map<String,V> getParent()
           
 int hashCode()
          Override Object This implementation iterates over entrySet(), calling hashCode on each element (entry) in the Collection, and adding up the results.
 boolean isEmpty()
          Implement Map
 Set<String> keySet()
          Implement Map
 V put(String key, V value)
          Implement Map
 void putAll(Map<? extends String,? extends V> t)
          Implement Map
 V remove(Object key)
          Implement Map
 int size()
          Implement Map
 String toString()
          Override Object
 Collection<V> values()
          Implement Map
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected String name
An optional instance name


node

protected Map<String,V> node
A Map for this level of the context heirachy


parent

protected Map<String,V> parent
The parent level of the context heirachy. This will often be a HierarchicalMap itself.

Constructor Detail

HierarchicalMap

public HierarchicalMap(int initialCapacity,
                       Map<String,V> parent)
Parameters:
initialCapacity - The initial capacity of the level's Map
parent - Parent context Map. This will often be a HierarchicalMap.

HierarchicalMap

public HierarchicalMap(Map<String,V> parent)
Parameters:
parent - Parent context Map. This will often be a HierarchicalMap.

HierarchicalMap

public HierarchicalMap(String name,
                       boolean caseSensitive,
                       int initialCapacity,
                       Map<String,V> parent)
Parameters:
name - Optional instance name
caseSensitive - Should this Map be case sensitive?
initialCapacity - The initial capacity of the level's Map
parent - Parent context Map. This will often be a HierarchicalMap.

HierarchicalMap

public HierarchicalMap(String name,
                       boolean caseSensitive,
                       Map<String,V> parent)
Parameters:
name - Pptional instance name
caseSensitive -
parent - Parent context Map. This will often be a HierarchicalMap.

HierarchicalMap

public HierarchicalMap(String name,
                       int initialCapacity,
                       Map<String,V> parent)
Parameters:
name - Pptional instance name
initialCapacity - The initial capacity of the level's Map
parent - Parent context Map. This will often be a HierarchicalMap.

HierarchicalMap

public HierarchicalMap(String name,
                       Map<String,V> parent)
Parameters:
name - Pptional instance name
parent - Parent context Map. This will often be a HierarchicalMap.
Method Detail

clear

public void clear()
Implement Map Removes all mappings from this node. Does not clear the parent node.

Specified by:
clear in interface Map<String,V>

containsKey

public boolean containsKey(Object key)
Implement Map

Specified by:
containsKey in interface Map<String,V>
Parameters:
key - key whose presence in this map is to be tested.
Returns:
boolean true if this node or it's parent contains a mapping for the specified key.
Throws:
NullPointerException - if the key is null and this map does not not permit null keys (optional).

containsValue

public boolean containsValue(Object value)
Implement Map

Specified by:
containsValue in interface Map<String,V>
Parameters:
value - value whose presence in this map is to be tested.
Returns:
boolean true if this node or it's parent maps one or more keys to the specified value.
Throws:
NullPointerException - if the value is null and this map does not not permit null values (optional).

entrySet

public Set<Map.Entry<String,V>> entrySet()
Implement Map

Specified by:
entrySet in interface Map<String,V>
Returns:
Set A unmodifiable Set view of the entries contained in this node and the parent node.

equals

public boolean equals(Object o)
Implement Map

Specified by:
equals in interface Map<String,V>
Overrides:
equals in class Object
Parameters:
o - object to be compared for equality with this map.
Returns:
boolean true if the specified object is equal to this map.

get

public V get(Object key)
Implement Map

Specified by:
get in interface Map<String,V>
Parameters:
key - key whose associated value is to be returned.
Returns:
Object The value to which this node or it's parent maps the specified key, or null if the map contains no mapping for this key.
Throws:
NullPointerException - key is null and this map does not not permit null keys (optional).
See Also:
get(Object)

getLocal

public V getLocal(Object key)
As for Map.get(Object) but only look at the current leaf node.

Parameters:
key - key whose associated value is to be returned.
Returns:
Object The value to which this node maps the specified key, or null if the map contains no mapping for this key.
Throws:
NullPointerException - key is null and this map does not not permit null keys (optional).

getName

public String getName()
Returns:
String Optional name, including HierarchicalMap parent's name

getParent

public Map<String,V> getParent()
Returns:
Map This instance's parent Map, often another HierarchicalMap instance.

hashCode

public int hashCode()
Override Object This implementation iterates over entrySet(), calling hashCode on each element (entry) in the Collection, and adding up the results.

Specified by:
hashCode in interface Map<String,V>
Overrides:
hashCode in class Object
Returns:
the hash code value for this map.

isEmpty

public boolean isEmpty()
Implement Map

Specified by:
isEmpty in interface Map<String,V>
Returns:
boolean true if this node and it's parent contains no key-value mappings.

keySet

public Set<String> keySet()
Implement Map

Specified by:
keySet in interface Map<String,V>
Returns:
Set A unmodifiable Set view of the keys contained in this node and the parent node.

put

public V put(String key,
             V value)
Implement Map

Specified by:
put in interface Map<String,V>
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
Object Previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.
Throws:
UnsupportedOperationException - if the put operation is not supported by this map.
ClassCastException - if the class of the specified key or value prevents it from being stored in this map.
IllegalArgumentException - if some aspect of this key or value prevents it from being stored in this map.
NullPointerException - this map does not permit null keys or values, and the specified key or value is null.

putAll

public void putAll(Map<? extends String,? extends V> t)
Implement Map

Specified by:
putAll in interface Map<String,V>
Parameters:
t - Mappings to be stored in this map.
Throws:
UnsupportedOperationException - if the putAll method is not supported by this map.
ClassCastException - if the class of a key or value in the specified map prevents it from being stored in this map.
IllegalArgumentException - some aspect of a key or value in the specified map prevents it from being stored in this map.
NullPointerException - the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys or values.

remove

public V remove(Object key)
Implement Map

Specified by:
remove in interface Map<String,V>
Parameters:
key - key whose mapping is to be removed from the map.
Returns:
Object Previous value associated with specified key, or null if there was no mapping for key.
Throws:
ClassCastException - if the key is of an inappropriate type for this map (optional).
NullPointerException - if the key is null and this map does not not permit null keys (optional).
UnsupportedOperationException - if the remove method is not supported by this map.

size

public int size()
Implement Map

Specified by:
size in interface Map<String,V>
Returns:
int The number of key-value mappings in this node and it's parent.

toString

public String toString()
Override Object

Overrides:
toString in class Object
Returns:
String String representation of this, including the parent

values

public Collection<V> values()
Implement Map

Specified by:
values in interface Map<String,V>
Returns:
Collection A unmodifiable collection of the values contained in this node and the parent node.