java.lang.ref.Reference.scala Maven / Gradle / Ivy
The newest version!
/*
* scalajs-weakreferences (https://github.com/scala-js/scala-js-weakreferences)
*
* Copyright EPFL.
*
* Licensed under Apache License 2.0
* (https://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package java.lang.ref
import scala.scalajs.js
/* The JavaDoc says that the methods are concrete in `Reference`, and not
* overridden in `WeakReference`. To mimic this setup, and since
* `WeakReference` is the only implemented subclass, we actually implement the
* behavior of `WeakReference` in `Reference`.
*/
abstract class Reference[T] private[ref] (referent: T, queue: ReferenceQueue[_ >: T]) {
private[this] var weakRef = new js.WeakRef(referent)
private[ref] var enqueued: Boolean = false
if (queue != null)
queue.register(this, referent)
def get(): T =
if (weakRef == null) null.asInstanceOf[T]
else weakRef.deref().getOrElse(null.asInstanceOf[T])
def clear(): Unit = {
if (queue != null)
queue.unregister(this)
weakRef = null
}
def isEnqueued(): Boolean =
enqueued
def enqueue(): Boolean = {
if (queue != null && queue.enqueue(this)) {
queue.unregister(this)
true
} else {
false
}
}
}