org.jibx.util
Class ChainedMap

java.lang.Object
  extended by org.jibx.util.ChainedMap
All Implemented Interfaces:
java.util.Map

public class ChainedMap
extends java.lang.Object
implements java.util.Map

Partial implementation of Map which provides a merged view of a defaults map with an overrides map. Although this can be used as a map for most purposes, methods which return live views of the keys or values in the map only take into account the overrides, not the defaults.


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
private  java.util.Map m_defaults
          Default values map.
private  java.util.Map m_overrides
          Override values map.
 
Constructor Summary
ChainedMap(java.util.Map defaults)
          Constructor.
 
Method Summary
 void clear()
          Clear all override key-value pairs.
 boolean containsKey(java.lang.Object key)
          Check if a key has a defined value.
 boolean containsValue(java.lang.Object value)
          Check if a value is present.
 java.util.Set entrySet()
          Get the set of entries.
 java.lang.Object get(java.lang.Object key)
          Get value for key.
 boolean isEmpty()
          Check if no overrides are defined.
 java.util.Set keySet()
          Get the set of keys.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Set an override value.
 void putAll(java.util.Map map)
          Add all key-value pairs from another map into the overrides map.
 java.lang.Object remove(java.lang.Object key)
          Remove a key-value pair.
 int size()
          Get the number of entries in the map.
 java.util.Collection values()
          Get the values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

m_defaults

private final java.util.Map m_defaults
Default values map.


m_overrides

private final java.util.Map m_overrides
Override values map.

Constructor Detail

ChainedMap

public ChainedMap(java.util.Map defaults)
Constructor.

Parameters:
defaults - map providing defaults for keys not set directly
Method Detail

clear

public void clear()
Clear all override key-value pairs. This only effects the overrides, not the defaults.

Specified by:
clear in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
Check if a key has a defined value. This will return true if the key is present in the overrides map with a non-null value, or if the key is not present in the overrides map but is present in the defaults map.

Specified by:
containsKey in interface java.util.Map
Parameters:
key -
Returns:
true if key defined, false if not

containsValue

public boolean containsValue(java.lang.Object value)
Check if a value is present. This only checks for the value in the overrides map.

Specified by:
containsValue in interface java.util.Map
Parameters:
value -
Returns:
true if value present as an override, false if not

entrySet

public java.util.Set entrySet()
Get the set of entries. This only returns the entries in the overrides map.

Specified by:
entrySet in interface java.util.Map
Returns:
override entries

get

public java.lang.Object get(java.lang.Object key)
Get value for key. If the key is present in the overrides map, the value from that map is returned; otherwise, the value for the key in the defaults map is returned.

Specified by:
get in interface java.util.Map
Parameters:
key -
Returns:
value (null if key not present)

isEmpty

public boolean isEmpty()
Check if no overrides are defined.

Specified by:
isEmpty in interface java.util.Map
Returns:
true if no overrides, false if any present

keySet

public java.util.Set keySet()
Get the set of keys. This only returns the keys in the overrides map.

Specified by:
keySet in interface java.util.Map
Returns:
keys

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Set an override value. This just adds the key-value pair to the override map.

Specified by:
put in interface java.util.Map
Parameters:
key -
value -
Returns:
previous value for key (from default map, if not present in overrides)

putAll

public void putAll(java.util.Map map)
Add all key-value pairs from another map into the overrides map.

Specified by:
putAll in interface java.util.Map
Parameters:
map -

remove

public java.lang.Object remove(java.lang.Object key)
Remove a key-value pair. If the key was previously present in the overrides map it is simply removed from that map. If it was not present in the overrides map but is present in the defaults map, a null entry is added to the overrides map for that key.

Specified by:
remove in interface java.util.Map
Parameters:
key -
Returns:
previous value for key

size

public int size()
Get the number of entries in the map. This returns the entry count for the overrides map only.

Specified by:
size in interface java.util.Map
Returns:
entry count

values

public java.util.Collection values()
Get the values. This returns only the values in the overrides map.

Specified by:
values in interface java.util.Map
Returns:
values


Project Web Site