Package org.onosproject.store.service
Interface ConsistentMap<K,V>
-
- Type Parameters:
K
- type of keyV
- type of value
- All Superinterfaces:
DistributedPrimitive
,java.lang.Iterable<java.util.Map.Entry<K,Versioned<V>>>
- All Known Subinterfaces:
ConsistentTreeMap<V>
- All Known Implementing Classes:
DefaultConsistentMap
,DefaultConsistentTreeMap
public interface ConsistentMap<K,V> extends DistributedPrimitive, java.lang.Iterable<java.util.Map.Entry<K,Versioned<V>>>
ConsistentMap
provides the same functionality asAsyncConsistentMap
with the only difference that all its methods block until the corresponding operation completes.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.onosproject.store.service.DistributedPrimitive
DistributedPrimitive.Status, DistributedPrimitive.Type
-
-
Field Summary
-
Fields inherited from interface org.onosproject.store.service.DistributedPrimitive
DEFAULT_OPERATION_TIMEOUT_MILLIS
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default void
addListener(MapEventListener<K,V> listener)
Registers the specified listener to be notified whenever the map is updated.void
addListener(MapEventListener<K,V> listener, java.util.concurrent.Executor executor)
Registers the specified listener to be notified whenever the map is updated.java.util.Map<K,V>
asJavaMap()
Returns a java.util.Map instance backed by this ConsistentMap.void
clear()
Removes all of the mappings from this map (optional operation).Versioned<V>
compute(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).Versioned<V>
computeIf(K key, java.util.function.Predicate<? super V> condition, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key satisfies a condition, attempts to compute a new mapping given the key and its current mapped value.Versioned<V>
computeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.Versioned<V>
computeIfPresent(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.boolean
containsKey(K key)
Returns true if this map contains a mapping for the specified key.boolean
containsValue(V value)
Returns true if this map contains the specified value.java.util.Set<java.util.Map.Entry<K,Versioned<V>>>
entrySet()
Returns the set of entries contained in this map.Versioned<V>
get(K key)
Returns the value (and version) to which the specified key is mapped, or null if this map contains no mapping for the key.Versioned<V>
getOrDefault(K key, V defaultValue)
Returns the value (and version) to which the specified key is mapped, or the provided default value if this map contains no mapping for the key.boolean
isEmpty()
Returns true if the map is empty.java.util.Set<K>
keySet()
Returns a Set view of the keys contained in this map.Versioned<V>
put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).Versioned<V>
putAndGet(K key, V value)
Associates the specified value with the specified key in this map (optional operation).Versioned<V>
putIfAbsent(K key, V value)
If the specified key is not already associated with a value associates it with the given value and returns null, else returns the current value.Versioned<V>
remove(K key)
Removes the mapping for a key from this map if it is present (optional operation).boolean
remove(K key, long version)
Removes the entry for the specified key only if its current version in the map is equal to the specified version.boolean
remove(K key, V value)
Removes the entry for the specified key only if it is currently mapped to the specified value.void
removeListener(MapEventListener<K,V> listener)
Unregisters the specified listener such that it will no longer receive map change notifications.boolean
replace(K key, long oldVersion, V newValue)
Replaces the entry for the specified key only if it is currently mapped to the specified version.Versioned<V>
replace(K key, V value)
Replaces the entry for the specified key only if there is any value which associated with specified key.boolean
replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.int
size()
Returns the number of entries in the map.default java.util.stream.Stream<java.util.Map.Entry<K,Versioned<V>>>
stream()
Streams entries from the map.java.util.Collection<Versioned<V>>
values()
Returns the collection of values (and associated versions) contained in this map.-
Methods inherited from interface org.onosproject.store.service.DistributedPrimitive
addStatusChangeListener, applicationId, destroy, name, primitiveType, removeStatusChangeListener, statusChangeListeners
-
-
-
-
Method Detail
-
size
int size()
Returns the number of entries in the map.- Returns:
- map size.
-
isEmpty
boolean isEmpty()
Returns true if the map is empty.- Returns:
- true if map has no entries, false otherwise
-
containsKey
boolean containsKey(K key)
Returns true if this map contains a mapping for the specified key.- Parameters:
key
- key- Returns:
- true if map contains key, false otherwise
-
containsValue
boolean containsValue(V value)
Returns true if this map contains the specified value.- Parameters:
value
- value- Returns:
- true if map contains value, false otherwise.
-
get
Versioned<V> get(K key)
Returns the value (and version) to which the specified key is mapped, or null if this map contains no mapping for the key.- Parameters:
key
- the key whose associated value (and version) is to be returned- Returns:
- the value (and version) to which the specified key is mapped, or null if this map contains no mapping for the key
-
getOrDefault
Versioned<V> getOrDefault(K key, V defaultValue)
Returns the value (and version) to which the specified key is mapped, or the provided default value if this map contains no mapping for the key.Note: a non-null
Versioned
value will be returned even if thedefaultValue
isnull
.- Parameters:
key
- the key whose associated value (and version) is to be returneddefaultValue
- the default value to return if the key is not set- Returns:
- the value (and version) to which the specified key is mapped, or null if this map contains no mapping for the key
-
computeIfAbsent
Versioned<V> computeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.- Parameters:
key
- key with which the specified value is to be associatedmappingFunction
- the function to compute a value- Returns:
- the current (existing or computed) value associated with the specified key,
or null if the computed value is null. Method throws
ConsistentMapException.ConcurrentModification
if a concurrent modification of map is detected
-
compute
Versioned<V> compute(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). If the computed value is null, the current mapping will be removed from the map.- Parameters:
key
- key with which the specified value is to be associatedremappingFunction
- the function to compute a value- Returns:
- the new value associated with the specified key, or null if none.
This method throws
ConsistentMapException.ConcurrentModification
if a concurrent modification of map is detected
-
computeIfPresent
Versioned<V> computeIfPresent(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value. If the computed value is null, the current mapping will be removed from the map.- Parameters:
key
- key with which the specified value is to be associatedremappingFunction
- the function to compute a value- Returns:
- the new value associated with the specified key, or null if none.
This method throws
ConsistentMapException.ConcurrentModification
if a concurrent modification of map is detected
-
computeIf
Versioned<V> computeIf(K key, java.util.function.Predicate<? super V> condition, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key satisfies a condition, attempts to compute a new mapping given the key and its current mapped value. If the computed value is null, the current mapping will be removed from the map.- Parameters:
key
- key with which the specified value is to be associatedcondition
- condition that should evaluate to true for the computation to proceedremappingFunction
- the function to compute a value- Returns:
- the new value associated with the specified key, or the old value if condition evaluates to false.
This method throws
ConsistentMapException.ConcurrentModification
if a concurrent modification of map is detected
-
put
Versioned<V> put(K key, V value)
Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value (and version) associated with key, or null if there was no mapping for key.
-
putAndGet
Versioned<V> putAndGet(K key, V value)
Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- new value.
-
remove
Versioned<V> remove(K key)
Removes the mapping for a key from this map if it is present (optional operation).- Parameters:
key
- key whose value is to be removed from the map- Returns:
- the value (and version) to which this map previously associated the key, or null if the map contained no mapping for the key.
-
clear
void clear()
Removes all of the mappings from this map (optional operation). The map will be empty after this call returns.
-
keySet
java.util.Set<K> keySet()
Returns a Set view of the keys contained in this map. This method differs from the behavior of java.util.Map.keySet() in that what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap. Attempts to modify the returned set, whether direct or via its iterator, result in an UnsupportedOperationException.- Returns:
- a set of the keys contained in this map
-
values
java.util.Collection<Versioned<V>> values()
Returns the collection of values (and associated versions) contained in this map. This method differs from the behavior of java.util.Map.values() in that what is returned is a unmodifiable snapshot view of the values in the ConsistentMap. Attempts to modify the returned collection, whether direct or via its iterator, result in an UnsupportedOperationException.- Returns:
- a collection of the values (and associated versions) contained in this map
-
entrySet
java.util.Set<java.util.Map.Entry<K,Versioned<V>>> entrySet()
Returns the set of entries contained in this map. This method differs from the behavior of java.util.Map.entrySet() in that what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap. Attempts to modify the returned set, whether direct or via its iterator, result in an UnsupportedOperationException.- Returns:
- set of entries contained in this map.
-
putIfAbsent
Versioned<V> putIfAbsent(K key, V value)
If the specified key is not already associated with a value associates it with the given value and returns null, else returns the current value.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the specified key or null if key does not already mapped to a value.
-
remove
boolean remove(K key, V value)
Removes the entry for the specified key only if it is currently mapped to the specified value.- Parameters:
key
- key with which the specified value is associatedvalue
- value expected to be associated with the specified key- Returns:
- true if the value was removed
-
remove
boolean remove(K key, long version)
Removes the entry for the specified key only if its current version in the map is equal to the specified version.- Parameters:
key
- key with which the specified version is associatedversion
- version expected to be associated with the specified key- Returns:
- true if the value was removed
-
replace
Versioned<V> replace(K key, V value)
Replaces the entry for the specified key only if there is any value which associated with specified key.- Parameters:
key
- key with which the specified value is associatedvalue
- value expected to be associated with the specified key- Returns:
- the previous value associated with the specified key or null
-
replace
boolean replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.- Parameters:
key
- key with which the specified value is associatedoldValue
- value expected to be associated with the specified keynewValue
- value to be associated with the specified key- Returns:
- true if the value was replaced
-
replace
boolean replace(K key, long oldVersion, V newValue)
Replaces the entry for the specified key only if it is currently mapped to the specified version.- Parameters:
key
- key key with which the specified value is associatedoldVersion
- version expected to be associated with the specified keynewValue
- value to be associated with the specified key- Returns:
- true if the value was replaced
-
stream
default java.util.stream.Stream<java.util.Map.Entry<K,Versioned<V>>> stream()
Streams entries from the map.This method is optimized for large maps.
- Returns:
- the map entry stream
-
addListener
default void addListener(MapEventListener<K,V> listener)
Registers the specified listener to be notified whenever the map is updated.- Parameters:
listener
- listener to notify about map events
-
addListener
void addListener(MapEventListener<K,V> listener, java.util.concurrent.Executor executor)
Registers the specified listener to be notified whenever the map is updated.- Parameters:
listener
- listener to notify about map eventsexecutor
- executor to use for handling incoming map events
-
removeListener
void removeListener(MapEventListener<K,V> listener)
Unregisters the specified listener such that it will no longer receive map change notifications.- Parameters:
listener
- listener to unregister
-
-