aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/util/TriemapAtomicGetOrElseUpdate.scala
blob: cd457cdc4f382b26febf3584b2e7411a249c392c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package kamon.util

import scala.collection.concurrent.TrieMap

object TriemapAtomicGetOrElseUpdate {

  /**
   *  Workaround to the non thread-safe [[scala.collection.concurrent.TrieMap#getOrElseUpdate]] method. More details on
   *  why this is necessary can be found at [[https://issues.scala-lang.org/browse/SI-7943]].
   */
  implicit class Syntax[K, V](val trieMap: TrieMap[K, V]) extends AnyVal {
    def atomicGetOrElseUpdate(key: K, op:  V): V =
      trieMap.get(key) match {
        case Some(v)  v
        case None     val d = op; trieMap.putIfAbsent(key, d).getOrElse(d)
      }
  }
}