org.mmbase.util
Class LinkMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by org.mmbase.util.LinkMap<K,V>
All Implemented Interfaces:
Map<K,V>

public class LinkMap<K,V>
extends AbstractMap<K,V>

Combines to Maps to one new map. One map is 'leading' and determins wich keys are mapped. The second map can override values, if it contains the same mapping. There are several ways to describe what must happen with changes on the map. You can e.g. maintain original values of a map values like so:

    Map originals = new HashMap();
    Map wrapper = new LinkMap(values, originals, LinkMap.Changes.CONSERVE);

    wrapper.put(key, value);

    Object newValue = values.get(key);
    Object originalValue = originals.get(key);
    assert originalValue == wrapper.get(key);
 
Changes on a map can be made temporay by wrapping it like this:
    Map wrapper = new LinkMap(values, new HashMap(), LinkMap.Changes.SECOND);
    wrapper.put(key, value);
    Object newValue = wrapper.get(key);
    Object oldValue = values.get(key);
    assert value == newValue;

 

Since:
MMBase-1.9
Version:
$Id: LinkMap.java 41055 2010-02-15 23:59:16Z michiel $
Author:
Michiel Meeuwissen

Nested Class Summary
static class LinkMap.Changes
          Enum for the parameter of the constructor LinkMap(Map, Map, Changes)
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
LinkMap(Map<K,V> m1, Map<K,V> m2)
          Creates an unmodifiable Linked Map
LinkMap(Map<K,V> m1, Map<K,V> m2, LinkMap.Changes c)
          Creates a (modifiable) Linked Map.
 
Method Summary
 boolean containsKey(Object key)
           
 Set<Map.Entry<K,V>> entrySet()
           
 V get(Object key)
           
static void main(String[] args)
           
 V put(K key, V v)
           
 int size()
           
 
Methods inherited from class java.util.AbstractMap
clear, clone, containsValue, equals, hashCode, isEmpty, keySet, putAll, remove, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LinkMap

public LinkMap(Map<K,V> m1,
               Map<K,V> m2,
               LinkMap.Changes c)
Creates a (modifiable) Linked Map. What precisely happens on modification is ruled by the c parameter.

See Also:
LinkMap.Changes.FIRST, LinkMap.Changes.SECOND, LinkMap.Changes.BOTH, LinkMap.Changes.CONSERVE, LinkMap.Changes.NONE

LinkMap

public LinkMap(Map<K,V> m1,
               Map<K,V> m2)
Creates an unmodifiable Linked Map

Method Detail

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>
Specified by:
entrySet in class AbstractMap<K,V>

size

public int size()
Specified by:
size in interface Map<K,V>
Overrides:
size in class AbstractMap<K,V>

get

public V get(Object key)
Specified by:
get in interface Map<K,V>
Overrides:
get in class AbstractMap<K,V>

put

public V put(K key,
             V v)
Specified by:
put in interface Map<K,V>
Overrides:
put in class AbstractMap<K,V>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey in class AbstractMap<K,V>

main

public static void main(String[] args)


MMBase 2.0-SNAPSHOT - null