|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.atlassian.util.concurrent.CopyOnWriteMap<K,V>
K
- the key typeV
- the value type@ThreadSafe public abstract class CopyOnWriteMap<K,V>
A thread-safe variant of Map
in which all mutative operations (the
"destructive" operations described by Map
put, remove and so on) are
implemented by making a fresh copy of the underlying map.
This is ordinarily too costly, but may be more efficient than
alternatives when traversal operations vastly out-number mutations, and is
useful when you cannot or don't want to synchronize traversals, yet need to
preclude interference among concurrent threads. The "snapshot" style
iterators on the collections returned by entrySet()
,
keySet()
and values()
use a reference to the internal map
at the point that the iterator was created. This map never changes during the
lifetime of the iterator, so interference is impossible and the iterator is
guaranteed not to throw ConcurrentModificationException. The
iterators will not reflect additions, removals, or changes to the list since
the iterator was created. Removing elements via these iterators is not
supported. The mutable operations on these collections (remove, retain etc.)
are supported but as with the Map
interface, add and addAll are not
and throw UnsupportedOperationException
.
The actual copy is performed by an abstract copy(Map)
method. The
method is responsible for the underlying Map implementation (for instance a
HashMap
, TreeMap
, LinkedHashMap
etc.) and therefore
the semantics of what this map will cope with as far as null keys and values,
iteration ordering etc. See the note below about suitable candidates for
underlying Map implementations
There are supplied implementations for the common j.u.c Map
implementations via the CopyOnWriteMaps
static CopyOnWriteMap.Builder
.
Collection views of the keys, values and entries are optionally
live
or stable
. Live views
are modifiable will cause a copy if a modifying method is called on them.
Methods on these will reflect the current state of the collection, although
iterators will be snapshot style. If the collection views are stable they are
unmodifiable, and will be a snapshot of the state of the map at the time the
collection was asked for.
Please note that the thread-safety guarantees are limited to
the thread-safety of the non-mutative (non-destructive) operations of the
underlying map implementation. For instance some implementations such as
WeakHashMap
and LinkedHashMap
with access ordering are
actually structurally modified by the get(Object)
method and are
therefore not suitable candidates as delegates for this class.
Nested Class Summary | |
---|---|
protected static class |
AbstractCopyOnWriteMap.CollectionView<E>
|
static class |
AbstractCopyOnWriteMap.View<K,V>
Provides access to the views of the underlying key, value and entry collections. |
static class |
CopyOnWriteMap.Builder<K,V>
Build a CopyOnWriteMap and specify all the options. |
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Constructor Summary | |
---|---|
protected |
CopyOnWriteMap()
Deprecated. since 0.0.12 use the versions that explicitly specify View.Type |
protected |
CopyOnWriteMap(AbstractCopyOnWriteMap.View.Type viewType)
Create a new empty CopyOnWriteMap . |
protected |
CopyOnWriteMap(Map<? extends K,? extends V> map)
Deprecated. since 0.0.12 use the versions that explicitly specify View.Type |
protected |
CopyOnWriteMap(Map<? extends K,? extends V> map,
AbstractCopyOnWriteMap.View.Type viewType)
Create a new CopyOnWriteMap with the supplied Map to
initialize the values. |
Method Summary | ||
---|---|---|
static
|
builder()
Get a CopyOnWriteMap.Builder for a CopyOnWriteMap instance. |
|
void |
clear()
|
|
boolean |
containsKey(Object key)
|
|
boolean |
containsValue(Object value)
|
|
protected M |
copy()
|
|
protected abstract
|
copy(N map)
Copy function, implemented by sub-classes. |
|
Set<Map.Entry<K,V>> |
entrySet()
|
|
boolean |
equals(Object o)
|
|
V |
get(Object key)
|
|
protected M |
getDelegate()
|
|
int |
hashCode()
|
|
boolean |
isEmpty()
|
|
Set<K> |
keySet()
|
|
static
|
newHashMap()
Creates a new CopyOnWriteMap with an underlying HashMap . |
|
static
|
newHashMap(Map<? extends K,? extends V> map)
Creates a new CopyOnWriteMap with an underlying HashMap
using the supplied map as the initial values. |
|
static
|
newLinkedMap()
Creates a new CopyOnWriteMap with an underlying
LinkedHashMap . |
|
static
|
newLinkedMap(Map<? extends K,? extends V> map)
Creates a new CopyOnWriteMap with an underlying
LinkedHashMap using the supplied map as the initial values. |
|
V |
put(K key,
V value)
|
|
void |
putAll(Map<? extends K,? extends V> t)
|
|
V |
putIfAbsent(K key,
V value)
|
|
V |
remove(Object key)
|
|
boolean |
remove(Object key,
Object value)
|
|
V |
replace(K key,
V value)
|
|
boolean |
replace(K key,
V oldValue,
V newValue)
|
|
protected void |
set(M map)
|
|
int |
size()
|
|
String |
toString()
|
|
Collection<V> |
values()
|
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
@Deprecated protected CopyOnWriteMap(Map<? extends K,? extends V> map)
CopyOnWriteMap
with the supplied Map
to
initialize the values.
map
- the initial map to initialize with@Deprecated protected CopyOnWriteMap()
CopyOnWriteMap
.
protected CopyOnWriteMap(Map<? extends K,? extends V> map, AbstractCopyOnWriteMap.View.Type viewType)
CopyOnWriteMap
with the supplied Map
to
initialize the values. This map may be optionally modified using any of the
key, entry or value views
map
- the initial map to initialize withprotected CopyOnWriteMap(AbstractCopyOnWriteMap.View.Type viewType)
CopyOnWriteMap
. This map may be optionally
modified using any of the key, entry or value views
Method Detail |
---|
public static <K,V> CopyOnWriteMap.Builder<K,V> builder()
CopyOnWriteMap.Builder
for a CopyOnWriteMap
instance.
K
- key typeV
- value type
public static <K,V> CopyOnWriteMap<K,V> newHashMap()
CopyOnWriteMap
with an underlying HashMap
.
This map has stable
views.
public static <K,V> CopyOnWriteMap<K,V> newHashMap(Map<? extends K,? extends V> map)
CopyOnWriteMap
with an underlying HashMap
using the supplied map as the initial values.
This map has stable
views.
public static <K,V> CopyOnWriteMap<K,V> newLinkedMap()
CopyOnWriteMap
with an underlying
LinkedHashMap
. Iterators for this map will be return elements in
insertion order.
This map has stable
views.
public static <K,V> CopyOnWriteMap<K,V> newLinkedMap(Map<? extends K,? extends V> map)
CopyOnWriteMap
with an underlying
LinkedHashMap
using the supplied map as the initial values.
Iterators for this map will be return elements in insertion order.
This map has stable
views.
protected abstract <N extends Map<? extends K,? extends V>> Map<K,V> copy(N map)
N
- the map to copy and return.map
- the initial values of the newly created map.
public final void clear()
clear
in interface Map<K,V>
public final V remove(Object key)
remove
in interface Map<K,V>
public boolean remove(Object key, Object value)
remove
in interface ConcurrentMap<K,V>
public boolean replace(K key, V oldValue, V newValue)
replace
in interface ConcurrentMap<K,V>
public V replace(K key, V value)
replace
in interface ConcurrentMap<K,V>
public final V put(K key, V value)
put
in interface Map<K,V>
public V putIfAbsent(K key, V value)
putIfAbsent
in interface ConcurrentMap<K,V>
public final void putAll(Map<? extends K,? extends V> t)
putAll
in interface Map<K,V>
protected M copy()
protected void set(M map)
public final Set<Map.Entry<K,V>> entrySet()
entrySet
in interface Map<K,V>
public final Set<K> keySet()
keySet
in interface Map<K,V>
public final Collection<V> values()
values
in interface Map<K,V>
public final boolean containsKey(Object key)
containsKey
in interface Map<K,V>
public final boolean containsValue(Object value)
containsValue
in interface Map<K,V>
public final V get(Object key)
get
in interface Map<K,V>
public final boolean isEmpty()
isEmpty
in interface Map<K,V>
public final int size()
size
in interface Map<K,V>
public final boolean equals(Object o)
equals
in interface Map<K,V>
equals
in class Object
public final int hashCode()
hashCode
in interface Map<K,V>
hashCode
in class Object
protected final M getDelegate()
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |