org.jboss.cache.util
Class DeltaMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by org.jboss.cache.util.DeltaMap<K,V>
Type Parameters:
K - key type
V - value type
All Implemented Interfaces:
Map<K,V>

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

Wraps an existing map, which is not modified, reflecting modifications in an internal modification set.

This is to minimize the amount of data copying, for instance in the case few changes are applied.

Example usage:

 HashMap<String, String> hm = new HashMap<String, String>();
 hm.put("a", "apple");
 DeltaMap dm = DeltaMap.create(hm);
 dm.remove("a");
 assert hm.containsKey("a");
 assert !dm.containsKey("a");
 dm.commit();
 assert !hm.containsKey("a");
 

Author:
Elias Ross

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Method Summary
 void clear()
           
 void commit()
          Commits the changes to the original map.
 boolean containsKey(Object key)
           
static
<K,V> DeltaMap<K,V>
create()
          Creates and returns a DeltaMap for an empty map.
static
<K,V> DeltaMap<K,V>
create(Map<K,V> original)
          Creates and returns a DeltaMap for an original map.
 Set<Map.Entry<K,V>> entrySet()
           
static
<K,V> DeltaMap<K,V>
excludeKeys(Map<K,V> original, K... exclude)
          Creates and returns a DeltaMap for an original map, excluding some key mappings.
static
<K,V> DeltaMap<K,V>
excludeKeys(Map<K,V> original, Set<K> exclude)
          Creates and returns a DeltaMap for an original map, excluding some key mappings.
 V get(Object key)
           
 Map<K,V> getChanged()
          Returns a Map of the entries changed, not including those removed.
 Map<K,V> getOriginal()
          Returns the original wrapped Map.
 Set<K> getRemoved()
          Returns the entries removed, including entries excluded by the constructor.
 boolean isModified()
          Returns true if the internal map was modified.
 V put(K key, V value)
           
 V remove(Object key)
           
 void setOriginal(Map<K,V> original)
          Sets the original values of this delta map.
 int size()
           
 String toDebugString()
          Returns a debug string.
 
Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

create

public static <K,V> DeltaMap<K,V> create(Map<K,V> original)
Creates and returns a DeltaMap for an original map.

Parameters:
original - will not be modified, except by commit()
Returns:
a new instance

create

public static <K,V> DeltaMap<K,V> create()
Creates and returns a DeltaMap for an empty map.

Returns:
a new instance

excludeKeys

public static <K,V> DeltaMap<K,V> excludeKeys(Map<K,V> original,
                                              Set<K> exclude)
Creates and returns a DeltaMap for an original map, excluding some key mappings.

Parameters:
original - will not be modified, except by commit()
exclude - entries not to include
Returns:
a new instance

excludeKeys

public static <K,V> DeltaMap<K,V> excludeKeys(Map<K,V> original,
                                              K... exclude)
Creates and returns a DeltaMap for an original map, excluding some key mappings.


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>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey 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 value)
Specified by:
put in interface Map<K,V>
Overrides:
put in class AbstractMap<K,V>

remove

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

commit

public void commit()
Commits the changes to the original map. Clears the list of removed keys.


isModified

public boolean isModified()
Returns true if the internal map was modified.


toDebugString

public String toDebugString()
Returns a debug string.


clear

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

getOriginal

public Map<K,V> getOriginal()
Returns the original wrapped Map.


setOriginal

public void setOriginal(Map<K,V> original)
Sets the original values of this delta map.


getChanged

public Map<K,V> getChanged()
Returns a Map of the entries changed, not including those removed.


getRemoved

public Set<K> getRemoved()
Returns the entries removed, including entries excluded by the constructor.



Copyright © 2009 JBoss, a division of Red Hat. All Rights Reserved.