com.atlassian.util.concurrent
Class CopyOnWriteSortedMap<K,V>
java.lang.Object
com.atlassian.util.concurrent.CopyOnWriteSortedMap<K,V>
- Type Parameters:
K
- the key typeV
- the value type
- All Implemented Interfaces:
- java.io.Serializable, java.util.Map<K,V>, java.util.SortedMap<K,V>
@ThreadSafe
public abstract class CopyOnWriteSortedMap<K,V>
- extends java.lang.Object
- implements java.util.SortedMap<K,V>
/** A thread-safe variant of SortedMap
in which all mutative
operations (the "destructive" operations described by SortedMap
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 the abstract copy(Map)
method. This
implementation of this method is responsible for the underlying
SortedMap
implementation (for instance a TreeMap
) and
therefore the semantics of what this map will cope with as far as null keys
and values, iteration ordering etc.
Views of the keys, values and entries are modifiable and will cause a copy.
Views taken using subMap(Object, Object)
, headMap(Object)
and tailMap(Object)
are unmodifiable.
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.
- Author:
- Jed Wesley-Smith
- See Also:
- Serialized Form
Nested classes/interfaces inherited from interface java.util.Map |
java.util.Map.Entry<K,V> |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Map |
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values |
CopyOnWriteSortedMap
public CopyOnWriteSortedMap()
- Create a new empty
CopyOnWriteMap
.
CopyOnWriteSortedMap
public CopyOnWriteSortedMap(java.util.Map<? extends K,? extends V> map)
- Create a new
CopyOnWriteMap
with the supplied Map
to
initialize the values.
- Parameters:
map
- the initial map to initialize with
copy
public abstract <N extends java.util.Map<? extends K,? extends V>> java.util.SortedMap<K,V> copy(N map)
comparator
public java.util.Comparator<? super K> comparator()
- Specified by:
comparator
in interface java.util.SortedMap<K,V>
firstKey
public K firstKey()
- Specified by:
firstKey
in interface java.util.SortedMap<K,V>
lastKey
public K lastKey()
- Specified by:
lastKey
in interface java.util.SortedMap<K,V>
headMap
public java.util.SortedMap<K,V> headMap(K toKey)
- Specified by:
headMap
in interface java.util.SortedMap<K,V>
tailMap
public java.util.SortedMap<K,V> tailMap(K fromKey)
- Specified by:
tailMap
in interface java.util.SortedMap<K,V>
subMap
public java.util.SortedMap<K,V> subMap(K fromKey,
K toKey)
- Specified by:
subMap
in interface java.util.SortedMap<K,V>
clear
public final void clear()
- Specified by:
clear
in interface java.util.Map<K,V>
remove
public final V remove(java.lang.Object key)
- Specified by:
remove
in interface java.util.Map<K,V>
put
public final V put(K key,
V value)
- Specified by:
put
in interface java.util.Map<K,V>
putAll
public final void putAll(java.util.Map<? extends K,? extends V> t)
- Specified by:
putAll
in interface java.util.Map<K,V>
removeAll
protected void removeAll(java.util.Collection<K> keys)
copy
protected M copy()
set
protected void set(M map)
entrySet
public final java.util.Set<java.util.Map.Entry<K,V>> entrySet()
- Specified by:
entrySet
in interface java.util.Map<K,V>
keySet
public final java.util.Set<K> keySet()
- Specified by:
keySet
in interface java.util.Map<K,V>
values
public final java.util.Collection<V> values()
- Specified by:
values
in interface java.util.Map<K,V>
containsKey
public final boolean containsKey(java.lang.Object key)
- Specified by:
containsKey
in interface java.util.Map<K,V>
containsValue
public final boolean containsValue(java.lang.Object value)
- Specified by:
containsValue
in interface java.util.Map<K,V>
get
public final V get(java.lang.Object key)
- Specified by:
get
in interface java.util.Map<K,V>
isEmpty
public final boolean isEmpty()
- Specified by:
isEmpty
in interface java.util.Map<K,V>
size
public final int size()
- Specified by:
size
in interface java.util.Map<K,V>
equals
public final boolean equals(java.lang.Object o)
- Specified by:
equals
in interface java.util.Map<K,V>
- Overrides:
equals
in class java.lang.Object
hashCode
public final int hashCode()
- Specified by:
hashCode
in interface java.util.Map<K,V>
- Overrides:
hashCode
in class java.lang.Object
getDelegate
protected final M getDelegate()
toString
public java.lang.String toString()
- Overrides:
toString
in class java.lang.Object
Copyright © 2009 Atlassian Pty Ltd. All Rights Reserved.