[ONOS-6324] Add getOrDefault method to ConsistentMap.
Change-Id: Ice7ad6260c6eb8076320ef469874c0c4ceeadc19
diff --git a/core/api/src/main/java/org/onosproject/store/primitives/DefaultConsistentMap.java b/core/api/src/main/java/org/onosproject/store/primitives/DefaultConsistentMap.java
index 4b7b1a8..db6f548 100644
--- a/core/api/src/main/java/org/onosproject/store/primitives/DefaultConsistentMap.java
+++ b/core/api/src/main/java/org/onosproject/store/primitives/DefaultConsistentMap.java
@@ -86,6 +86,11 @@
}
@Override
+ public Versioned<V> getOrDefault(K key, V defaultValue) {
+ return complete(asyncMap.getOrDefault(key, defaultValue));
+ }
+
+ @Override
public Versioned<V> computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction) {
return computeIf(key, Objects::isNull, (k, v) -> mappingFunction.apply(k));
diff --git a/core/api/src/main/java/org/onosproject/store/primitives/DefaultConsistentTreeMap.java b/core/api/src/main/java/org/onosproject/store/primitives/DefaultConsistentTreeMap.java
index a38da9e..d98eb17 100644
--- a/core/api/src/main/java/org/onosproject/store/primitives/DefaultConsistentTreeMap.java
+++ b/core/api/src/main/java/org/onosproject/store/primitives/DefaultConsistentTreeMap.java
@@ -176,6 +176,11 @@
}
@Override
+ public Versioned<V> getOrDefault(String key, V defaultValue) {
+ return complete(treeMap.getOrDefault(key, defaultValue));
+ }
+
+ @Override
public Versioned<V> computeIfAbsent(String key,
Function<? super String,
? extends V> mappingFunction) {
diff --git a/core/api/src/main/java/org/onosproject/store/service/AsyncConsistentMap.java b/core/api/src/main/java/org/onosproject/store/service/AsyncConsistentMap.java
index f25fadc..8bfd952 100644
--- a/core/api/src/main/java/org/onosproject/store/service/AsyncConsistentMap.java
+++ b/core/api/src/main/java/org/onosproject/store/service/AsyncConsistentMap.java
@@ -110,6 +110,17 @@
CompletableFuture<Versioned<V>> get(K key);
/**
+ * 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.
+ *
+ * @param key the key whose associated value (and version) is to be returned
+ * @param defaultValue the default value to return if the key is not set
+ * @return a future value (and version) to which the specified key is mapped, or null if
+ * this map contains no mapping for the key
+ */
+ CompletableFuture<Versioned<V>> getOrDefault(K key, V defaultValue);
+
+ /**
* 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.
diff --git a/core/api/src/main/java/org/onosproject/store/service/ConsistentMap.java b/core/api/src/main/java/org/onosproject/store/service/ConsistentMap.java
index f5e8c13..3d14a68 100644
--- a/core/api/src/main/java/org/onosproject/store/service/ConsistentMap.java
+++ b/core/api/src/main/java/org/onosproject/store/service/ConsistentMap.java
@@ -77,6 +77,20 @@
Versioned<V> get(K key);
/**
+ * 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.
+ * <p>
+ * Note: a non-null {@link Versioned} value will be returned even if the {@code defaultValue}
+ * is {@code null}.
+ *
+ * @param key the key whose associated value (and version) is to be returned
+ * @param defaultValue the default value to return if the key is not set
+ * @return 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);
+
+ /**
* 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.
diff --git a/core/api/src/main/java/org/onosproject/store/service/Versioned.java b/core/api/src/main/java/org/onosproject/store/service/Versioned.java
index 6c6834e..7de5be7 100644
--- a/core/api/src/main/java/org/onosproject/store/service/Versioned.java
+++ b/core/api/src/main/java/org/onosproject/store/service/Versioned.java
@@ -97,7 +97,7 @@
* @return mapped instance
*/
public synchronized <U> Versioned<U> map(Function<V, U> transformer) {
- return new Versioned<>(transformer.apply(value), version, creationTime);
+ return new Versioned<>(value != null ? transformer.apply(value) : null, version, creationTime);
}
/**