com.faunos.util
Class AbstractCopyOnWriteMap<K,V,M extends Map<K,V>>

java.lang.Object
  extended by com.faunos.util.AbstractCopyOnWriteMap<K,V,M>
All Implemented Interfaces:
Map<K,V>
Direct Known Subclasses:
CopyOnWriteMap, CopyOnWriteSortedMap

public abstract class AbstractCopyOnWriteMap<K,V,M extends Map<K,V>>
extends Object
implements Map<K,V>

Base class for copy-on-write Map or SortedMap mimplementation. The implementation is designed to be suitable for use cases in which updates are very few and reads are many and concurrent.

Initial tests hint that read performance of this implementation may be better than through synchronized access or through a ConcurrentHashMap. But the main take away from the tests was that performance was "good enough" across all implementations.

Implementation Interface Violations

This implementation does not expose indirect updates through the return value of the following methods: These would not constitute violations of the Map interface were this implementation read-only. Moreover, the onerous interface requirement that changes to the map be reflected through the views represented by the return values of the above method is also not met. Instead, the return values represent a read-only snapshot of the state of map.

Author:
Babak Farhang

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  M impl
          The internal implementation.
 
Constructor Summary
AbstractCopyOnWriteMap()
           
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
protected abstract  M emptyMap()
          Returns an implementation-specific empty map.
 Set<Map.Entry<K,V>> entrySet()
          Returns a read-only, snapshot of this instance's entry set.
 boolean equals(Object object)
           
 V get(Object key)
           
 int hashCode()
           
 boolean isEmpty()
           
 Set<K> keySet()
          Returns a read-only, snapshot of this instance's key set.
protected abstract  M newMap()
          Returns a new implementation-specific empty map.
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> t)
           
 V remove(Object key)
           
 void setMap(Map<? extends K,? extends V> t)
          Atomically sets the contents of this map to be equal to the contents of the given map t.
 int size()
           
 Collection<V> values()
          Returns a read-only, snapshot of this instance's values.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

impl

protected volatile M extends Map<K,V> impl
The internal implementation.

BF: The other choice (instead of the volatile keyword) would be to use an atomic reference. The perf comp test, however, suggests our current choice is faster.

Constructor Detail

AbstractCopyOnWriteMap

public AbstractCopyOnWriteMap()
Method Detail

clear

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

containsKey

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

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()
Returns a read-only, snapshot of this instance's entry set.

Specified by:
entrySet in interface Map<K,V>
See Also:
Implementation interface violations

get

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

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>

keySet

public Set<K> keySet()
Returns a read-only, snapshot of this instance's key set.

Specified by:
keySet in interface Map<K,V>
See Also:
Implementation interface violations

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>

putAll

public void putAll(Map<? extends K,? extends V> t)
Specified by:
putAll in interface Map<K,V>

setMap

public void setMap(Map<? extends K,? extends V> t)
Atomically sets the contents of this map to be equal to the contents of the given map t. This method is not part of the Map interface. Using that interface only, the same effect can be achieved in 2 steps, not 1.


remove

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

size

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

values

public Collection<V> values()
Returns a read-only, snapshot of this instance's values.

Specified by:
values in interface Map<K,V>
See Also:
Implementation interface violations

equals

public boolean equals(Object object)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object

hashCode

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

emptyMap

protected abstract M emptyMap()
Returns an implementation-specific empty map. The returned map may be read-only. An implementation ought to return the same empty instance across all instances of its class.


newMap

protected abstract M newMap()
Returns a new implementation-specific empty map. The returned map must be read/write.



SourceForge.net Logo