All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalapb.descriptors.ConcurrentWeakReferenceMap.scala Maven / Gradle / Ivy

There is a newer version: 1.0.0-alpha.1
Show newest version
package scalapb.descriptors

import scala.collection.mutable
import scala.ref.WeakReference

/** JVM implementation of ConcurrentWeakReferenceMap. We provide a separate implementation here
  * since some of the classes used are not available in JS runtime.
  */
private[descriptors] class ConcurrentWeakReferenceMap[K, V <: AnyRef] {
  private val underlying = mutable.WeakHashMap.empty[K, WeakReference[V]]

  def getOrElseUpdate(key: K, newValue: => V): V = this.synchronized {
    val optVal = for {
      wr <- underlying.get(key)
      v  <- wr.get
    } yield v
    optVal.getOrElse {
      val r: V = newValue
      underlying.put(key, new WeakReference[V](r))
      r
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy