diff options
author | Philipp Haller <hallerp@gmail.com> | 2010-01-04 12:06:07 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2010-01-04 12:06:07 +0000 |
commit | 210a9552b51c31ed7e98dd0bdf6edfc3d804e963 (patch) | |
tree | 4b8b06f23c40836a1c176085419b9a71efc3ea39 /src/library | |
parent | e132d06e6b58c11b31824d1c9d4085636961e5f1 (diff) | |
download | scala-210a9552b51c31ed7e98dd0bdf6edfc3d804e963.tar.gz scala-210a9552b51c31ed7e98dd0bdf6edfc3d804e963.tar.bz2 scala-210a9552b51c31ed7e98dd0bdf6edfc3d804e963.zip |
Fixed #2852 and #2854.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/ref/PhantomReference.scala | 8 | ||||
-rw-r--r-- | src/library/scala/ref/ReferenceQueue.scala | 23 | ||||
-rw-r--r-- | src/library/scala/ref/ReferenceWrapper.scala | 7 | ||||
-rw-r--r-- | src/library/scala/ref/SoftReference.scala | 11 | ||||
-rw-r--r-- | src/library/scala/ref/WeakReference.scala | 11 |
5 files changed, 35 insertions, 25 deletions
diff --git a/src/library/scala/ref/PhantomReference.scala b/src/library/scala/ref/PhantomReference.scala index 7ba26389ff..73746a948f 100644 --- a/src/library/scala/ref/PhantomReference.scala +++ b/src/library/scala/ref/PhantomReference.scala @@ -15,7 +15,11 @@ package scala.ref */ class PhantomReference[+T <: AnyRef](value: T, queue: ReferenceQueue[T]) extends ReferenceWrapper[T] { val underlying: java.lang.ref.PhantomReference[_ <: T] = - new java.lang.ref.PhantomReference[T](value, queue.underlying.asInstanceOf[java.lang.ref.ReferenceQueue[T]]) - queue.register(this) + new PhantomReferenceWithWrapper[T](value, queue, this) } +/** + * @author Philipp Haller + */ +private class PhantomReferenceWithWrapper[T <: AnyRef](value: T, queue: ReferenceQueue[T], val wrapper: PhantomReference[T]) + extends java.lang.ref.PhantomReference[T](value, queue.underlying.asInstanceOf[java.lang.ref.ReferenceQueue[T]]) with ReferenceWithWrapper[T] diff --git a/src/library/scala/ref/ReferenceQueue.scala b/src/library/scala/ref/ReferenceQueue.scala index c7fae4eb79..5ef74bbc95 100644 --- a/src/library/scala/ref/ReferenceQueue.scala +++ b/src/library/scala/ref/ReferenceQueue.scala @@ -10,30 +10,23 @@ package scala.ref -import scala.collection.mutable.HashMap - /** - * @author Sean McDirmid, Philipp Haller + * @author Sean McDirmid + * @author Philipp Haller */ class ReferenceQueue[+T <: AnyRef] { + private[ref] val underlying: java.lang.ref.ReferenceQueue[_ <: T] = new java.lang.ref.ReferenceQueue[T] override def toString = underlying.toString - protected def Wrapper(jref: java.lang.ref.Reference[_]) = jref match { - case null => None - case ref => - val refWrapper = wrappers(ref) - wrappers -= ref - Some(refWrapper.asInstanceOf[Reference[T]]) - } + protected def Wrapper(jref: java.lang.ref.Reference[_]): Option[Reference[T]] = + jref match { + case null => None + case ref => Some(ref.asInstanceOf[ReferenceWithWrapper[T]].wrapper) + } def poll: Option[Reference[T]] = Wrapper(underlying.poll) def remove: Option[Reference[T]] = Wrapper(underlying.remove) def remove(timeout: Long): Option[Reference[T]] = Wrapper(underlying.remove(timeout)) - protected val wrappers = new HashMap[java.lang.ref.Reference[_], - ReferenceWrapper[_ <: AnyRef]] - def register(ref: ReferenceWrapper[_ <: AnyRef]) { - wrappers += ((ref.underlying, ref)) - } } diff --git a/src/library/scala/ref/ReferenceWrapper.scala b/src/library/scala/ref/ReferenceWrapper.scala index 7c3e8686e4..5661737053 100644 --- a/src/library/scala/ref/ReferenceWrapper.scala +++ b/src/library/scala/ref/ReferenceWrapper.scala @@ -30,3 +30,10 @@ trait ReferenceWrapper[+T <: AnyRef] extends Reference[T] with Proxy { def self = underlying } + +/** + * @author Philipp Haller + */ +private trait ReferenceWithWrapper[T <: AnyRef] { + val wrapper: ReferenceWrapper[T] +} diff --git a/src/library/scala/ref/SoftReference.scala b/src/library/scala/ref/SoftReference.scala index 5d4d470c6a..1026a85023 100644 --- a/src/library/scala/ref/SoftReference.scala +++ b/src/library/scala/ref/SoftReference.scala @@ -16,8 +16,11 @@ package scala.ref class SoftReference[+T <: AnyRef](value : T, queue : ReferenceQueue[T]) extends ReferenceWrapper[T] { def this(value : T) = this(value, null); val underlying: java.lang.ref.SoftReference[_ <: T] = - if (queue == null) new java.lang.ref.SoftReference[T](value); - else new java.lang.ref.SoftReference[T](value, queue.underlying.asInstanceOf[java.lang.ref.ReferenceQueue[T]]) - if (queue != null) - queue.register(this) + new SoftReferenceWithWrapper[T](value, queue, this) } + +/** + * @author Philipp Haller + */ +private class SoftReferenceWithWrapper[T <: AnyRef](value: T, queue: ReferenceQueue[T], val wrapper: SoftReference[T]) + extends java.lang.ref.SoftReference[T](value, if (queue == null) null else queue.underlying.asInstanceOf[java.lang.ref.ReferenceQueue[T]]) with ReferenceWithWrapper[T] diff --git a/src/library/scala/ref/WeakReference.scala b/src/library/scala/ref/WeakReference.scala index 661b2db60a..e6386d9f24 100644 --- a/src/library/scala/ref/WeakReference.scala +++ b/src/library/scala/ref/WeakReference.scala @@ -16,8 +16,11 @@ package scala.ref class WeakReference[+T <: AnyRef](value: T, queue: ReferenceQueue[T]) extends ReferenceWrapper[T] { def this(value: T) = this(value, null) val underlying: java.lang.ref.WeakReference[_ <: T] = - if (queue == null) new java.lang.ref.WeakReference[T](value) - else new java.lang.ref.WeakReference[T](value, queue.underlying.asInstanceOf[java.lang.ref.ReferenceQueue[T]]) - if (queue != null) - queue.register(this) + new WeakReferenceWithWrapper[T](value, queue, this) } + +/** + * @author Philipp Haller + */ +private class WeakReferenceWithWrapper[T <: AnyRef](value: T, queue: ReferenceQueue[T], val wrapper: WeakReference[T]) + extends java.lang.ref.WeakReference[T](value, if (queue == null) null else queue.underlying.asInstanceOf[java.lang.ref.ReferenceQueue[T]]) with ReferenceWithWrapper[T] |