Class CopyOnWriteMap<K,V>

java.lang.Object
com.tangosol.util.CopyOnWriteMap<K,V>
All Implemented Interfaces:
Map<K,V>

public class CopyOnWriteMap<K,V> extends Object implements Map<K,V>
A thread-safe variant of Map in which all mutating operations (e.g. put, putAll) are implemented by making a fresh copy of the underlying map.

Iterators over this map are guaranteed to produce a safe-iteration and not to throw ConcurrentModificationException. The iterator will not reflect concurrent additions, removals, or changes to this map.

Note: mutations on this map are costly, but may be more efficient than alternatives when "get" operations vastly outnumber mutations. All mutating operations are synchronized, so concurrent mutation can be prevented by holding synchronization on this object.

Since:
Coherence 3.7.2
Author:
pp 2011.09.17, rhl 2010.09.09 (from CopyOnWriteLongArray)
  • Constructor Details

    • CopyOnWriteMap

      public CopyOnWriteMap(Class clazz)
    • CopyOnWriteMap

      public CopyOnWriteMap(Map<K,V> map)
      Construct a CopyOnWriteMap, initialized with the contents of the specified map.
      Parameters:
      map - the initial map
  • Method Details

    • getInternalMap

      protected Map<K,V> getInternalMap()
      Return the internal map.
      Returns:
      the internal map
    • setInternalMap

      protected void setInternalMap(Map<K,V> map)
      Set the internal map.
      Parameters:
      map - the new internal map
    • instantiateMap

      protected Map<K,V> instantiateMap(Class clazz)
      Create a new instance of Map based on the provided Class.
      Parameters:
      clazz - the type of Map to instantiate
      Returns:
      a new empty instance of Map
      Throws:
      IllegalArgumentException - if the provided Class does not implement Map.
    • copyMap

      protected Map<K,V> copyMap(Map<K,V> map)
      Create an instance of Map based on the contents of the provided map.
      Parameters:
      map - the map to copy
      Returns:
      an instance of Map populated with the contents of the provided map
    • size

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

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

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

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

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

      public V put(K oKey, V oValue)
      Specified by:
      put in interface Map<K,V>
    • remove

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

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

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

      public Set<K> keySet()
      Returns a Set view of the keys contained in the underlying map.
      Specified by:
      keySet in interface Map<K,V>
      Returns:
      a Set view of the keys contained in the underlying map
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Return a set view of the underlying map.
      Specified by:
      entrySet in interface Map<K,V>
      Returns:
      a set view of the underlying map
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • equals

      public boolean equals(Object o)
      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
    • toString

      public String toString()
      Overrides:
      toString in class Object