summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/concurrent/TrieMap.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/library/scala/collection/concurrent/TrieMap.scala')
-rw-r--r--src/library/scala/collection/concurrent/TrieMap.scala38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/library/scala/collection/concurrent/TrieMap.scala b/src/library/scala/collection/concurrent/TrieMap.scala
index fccc1d81b9..bcfea7a463 100644
--- a/src/library/scala/collection/concurrent/TrieMap.scala
+++ b/src/library/scala/collection/concurrent/TrieMap.scala
@@ -873,6 +873,44 @@ extends scala.collection.concurrent.Map[K, V]
insertifhc(k, hc, v, INode.KEY_ABSENT)
}
+ // TODO once computeIfAbsent is added to concurrent.Map,
+ // move the comment there and tweak the 'at most once' part
+ /** If the specified key is not already in the map, computes its value using
+ * the given thunk `op` and enters it into the map.
+ *
+ * Since concurrent maps cannot contain `null` for keys or values,
+ * a `NullPointerException` is thrown if the thunk `op`
+ * returns `null`.
+ *
+ * If the specified mapping function throws an exception,
+ * that exception is rethrown.
+ *
+ * Note: This method will invoke op at most once.
+ * However, `op` may be invoked without the result being added to the map if
+ * a concurrent process is also trying to add a value corresponding to the
+ * same key `k`.
+ *
+ * @param k the key to modify
+ * @param op the expression that computes the value
+ * @return the newly added value
+ */
+ override def getOrElseUpdate(k: K, op: =>V): V = {
+ val oldv = lookup(k)
+ if (oldv != null) oldv.asInstanceOf[V]
+ else {
+ val v = op
+ if (v == null) {
+ throw new NullPointerException("Concurrent TrieMap values cannot be null.")
+ } else {
+ val hc = computeHash(k)
+ insertifhc(k, hc, v, INode.KEY_ABSENT) match {
+ case Some(oldv) => oldv
+ case None => v
+ }
+ }
+ }
+ }
+
def remove(k: K, v: V): Boolean = {
val hc = computeHash(k)
removehc(k, v, hc).nonEmpty