Class CopyOnWriteSortedMap<K,V extends @Nullable Object>
- java.lang.Object
-
- com.google.common.collect.ForwardingObject
-
- com.google.common.collect.ForwardingMap<K,V>
-
- com.google.common.collect.ForwardingSortedMap<K,V>
-
- com.google.common.collect.ForwardingNavigableMap<K,V>
-
- org.sosy_lab.common.collect.CopyOnWriteSortedMap<K,V>
-
- Type Parameters:
K
- The type of keys.V
- The type of values.
- All Implemented Interfaces:
Map<K,V>
,NavigableMap<K,V>
,SortedMap<K,V>
public final class CopyOnWriteSortedMap<K,V extends @Nullable Object> extends ForwardingNavigableMap<K,V>
This is a map implementation that uses copy-on-write behavior. This may be a good fit when you want to keep old snapshots of the map while modifying it. Through the use of aPersistentMap
backend, snapshots and modifying operations are both cheap (O(1) for the former, usually O(log n) for the latter).There are two usage patterns for this map. First, you can keep one instance of of the map which you modify, and eventually call
getSnapshot()
to take immutable snapshots. Second, you can use an instance of the map and create copies of it with thecopyOf(CopyOnWriteSortedMap)
method (these copies are O(1)). Then you can modify both the old and the new instance, but modifications to one of it won't be reflected by the other.All collection views returned my methods of this map are live views and change if this map is modified. However, they currently do not support any modifying operations. All iterators produced by the collection views iterate over an immutable snapshot of the map taken at iterator creation time and thus do not reflect intermediate changes to the map. The iterators also don't support the
Iterator.remove()
method. Thus it is safe to iterate over the map while changing it.This implementation is thread-safe and lock free, but does not guarantee freedom of starvation. Bulk operations are not atomic.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.google.common.collect.ForwardingNavigableMap
ForwardingNavigableMap.StandardDescendingMap, ForwardingNavigableMap.StandardNavigableKeySet
-
Nested classes/interfaces inherited from class com.google.common.collect.ForwardingSortedMap
ForwardingSortedMap.StandardKeySet
-
Nested classes/interfaces inherited from class com.google.common.collect.ForwardingMap
ForwardingMap.StandardEntrySet, ForwardingMap.StandardValues
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
SeeMap.clear()
.static <K extends Comparable<? super K>,V>
CopyOnWriteSortedMap<K,V>copyOf(CopyOnWriteSortedMap<K,V> pMap)
Create a new map instance containing all entries of the given map.static <K extends Comparable<? super K>,V>
CopyOnWriteSortedMap<K,V>copyOf(PersistentSortedMap<K,V> pMap)
Create a new map instance with an initial content of the given map.protected NavigableMap<K,V>
delegate()
NavigableSet<Map.Entry<K,V>>
entrySet()
PersistentSortedMap<K,V>
getSnapshot()
Return a immutable snapshot of the current state of the map.NavigableMap<K,V>
headMap(K pToKey)
NavigableSet<K>
keySet()
@Nullable Map.Entry<K,V>
pollFirstEntry()
This method is not starvation free, and thus not strictly guaranteed to terminate in presence of concurrent modifying operations.@Nullable Map.Entry<K,V>
pollLastEntry()
This method is not starvation free, and thus not strictly guaranteed to terminate in presence of concurrent modifying operations.@Nullable V
put(K pKey, V pValue)
This method is not starvation free, and thus not strictly guaranteed to terminate in presence of concurrent modifying operations.void
putAll(Map<? extends K,? extends V> pMap)
This method is not atomic! It inserts all keys one after the other, and in between each operation arbitrary operations from other threads might get executed.@Nullable V
remove(Object pKey)
This method is not starvation free, and thus not strictly guaranteed to terminate in presence of concurrent modifying operations.NavigableMap<K,V>
subMap(K pFromKey, K pToKey)
NavigableMap<K,V>
tailMap(K pFromKey)
Collection<V>
values()
-
Methods inherited from class com.google.common.collect.ForwardingNavigableMap
ceilingEntry, ceilingKey, descendingKeySet, descendingMap, firstEntry, floorEntry, floorKey, headMap, higherEntry, higherKey, lastEntry, lowerEntry, lowerKey, navigableKeySet, standardCeilingEntry, standardCeilingKey, standardDescendingKeySet, standardFirstEntry, standardFirstKey, standardFloorEntry, standardFloorKey, standardHeadMap, standardHigherEntry, standardHigherKey, standardLastEntry, standardLastKey, standardLowerEntry, standardLowerKey, standardPollFirstEntry, standardPollLastEntry, standardSubMap, standardTailMap, subMap, tailMap
-
Methods inherited from class com.google.common.collect.ForwardingSortedMap
comparator, firstKey, lastKey, standardContainsKey
-
Methods inherited from class com.google.common.collect.ForwardingMap
containsKey, containsValue, equals, get, hashCode, isEmpty, size, standardClear, standardContainsValue, standardEquals, standardHashCode, standardIsEmpty, standardPutAll, standardRemove, standardToString
-
Methods inherited from class com.google.common.collect.ForwardingObject
toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, equals, forEach, get, getOrDefault, hashCode, isEmpty, merge, putIfAbsent, remove, replace, replace, replaceAll, size
-
Methods inherited from interface java.util.SortedMap
comparator, firstKey, lastKey
-
-
-
-
Method Detail
-
copyOf
public static <K extends Comparable<? super K>,V> CopyOnWriteSortedMap<K,V> copyOf(PersistentSortedMap<K,V> pMap)
Create a new map instance with an initial content of the given map. To create an empty instance, get an empty instance of your favoritePersistentSortedMap
implementation and pass it to this method.
-
copyOf
public static <K extends Comparable<? super K>,V> CopyOnWriteSortedMap<K,V> copyOf(CopyOnWriteSortedMap<K,V> pMap)
Create a new map instance containing all entries of the given map. The snapshot of the given map is created atomically. Changes to the new map don't reflect in the given map and vice-versa.
-
delegate
protected NavigableMap<K,V> delegate()
- Specified by:
delegate
in classForwardingNavigableMap<K,V extends @Nullable Object>
-
getSnapshot
public PersistentSortedMap<K,V> getSnapshot()
Return a immutable snapshot of the current state of the map.
-
put
@CanIgnoreReturnValue public @Nullable V put(K pKey, V pValue)
This method is not starvation free, and thus not strictly guaranteed to terminate in presence of concurrent modifying operations.
-
remove
@CanIgnoreReturnValue public @Nullable V remove(Object pKey)
This method is not starvation free, and thus not strictly guaranteed to terminate in presence of concurrent modifying operations.
-
pollFirstEntry
@CanIgnoreReturnValue public @Nullable Map.Entry<K,V> pollFirstEntry()
This method is not starvation free, and thus not strictly guaranteed to terminate in presence of concurrent modifying operations.- Specified by:
pollFirstEntry
in interfaceNavigableMap<K,V extends @Nullable Object>
- Overrides:
pollFirstEntry
in classForwardingNavigableMap<K,V extends @Nullable Object>
- See Also:
NavigableMap.pollFirstEntry()
-
pollLastEntry
@CanIgnoreReturnValue public @Nullable Map.Entry<K,V> pollLastEntry()
This method is not starvation free, and thus not strictly guaranteed to terminate in presence of concurrent modifying operations.- Specified by:
pollLastEntry
in interfaceNavigableMap<K,V extends @Nullable Object>
- Overrides:
pollLastEntry
in classForwardingNavigableMap<K,V extends @Nullable Object>
- See Also:
NavigableMap.pollLastEntry()
-
putAll
public void putAll(Map<? extends K,? extends V> pMap)
This method is not atomic! It inserts all keys one after the other, and in between each operation arbitrary operations from other threads might get executed.This method is not starvation free, and thus not strictly guaranteed to terminate in presence of concurrent modifying operations.
-
clear
public void clear()
SeeMap.clear()
.
-
entrySet
public NavigableSet<Map.Entry<K,V>> entrySet()
-
keySet
public NavigableSet<K> keySet()
-
values
public Collection<V> values()
-
headMap
public NavigableMap<K,V> headMap(K pToKey)
-
tailMap
public NavigableMap<K,V> tailMap(K pFromKey)
-
-