Skip to content

Commit

Permalink
Delegate null test to Option
Browse files Browse the repository at this point in the history
Option(null) is None while Option(v) is Some(v) which makes the null
test redundant.
  • Loading branch information
janekdb committed Aug 4, 2015
1 parent a745f06 commit 58ae3e5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 41 deletions.
41 changes: 8 additions & 33 deletions src/library/scala/collection/convert/Wrappers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,11 @@ private[collection] trait Wrappers {
def +=(kv: (A, B)): this.type = { underlying.put(kv._1, kv._2); this }
def -=(key: A): this.type = { underlying remove key; this }

override def put(k: A, v: B): Option[B] = {
val r = underlying.put(k, v)
if (r != null) Some(r) else None
}
override def put(k: A, v: B): Option[B] = Option(underlying.put(k, v))

override def update(k: A, v: B) { underlying.put(k, v) }

override def remove(k: A): Option[B] = {
val r = underlying remove k
if (r != null) Some(r) else None
}
override def remove(k: A): Option[B] = Option(underlying remove k)

def iterator: Iterator[(A, B)] = new AbstractIterator[(A, B)] {
val ui = underlying.entrySet.iterator
Expand Down Expand Up @@ -326,25 +320,15 @@ private[collection] trait Wrappers {
* are not guaranteed to be atomic.
*/
case class JConcurrentMapWrapper[A, B](underlying: juc.ConcurrentMap[A, B]) extends mutable.AbstractMap[A, B] with JMapWrapperLike[A, B, JConcurrentMapWrapper[A, B]] with concurrent.Map[A, B] {
override def get(k: A) = {
val v = underlying get k
if (v != null) Some(v)
else None
}
override def get(k: A) = Option(underlying get k)

override def empty = new JConcurrentMapWrapper(new juc.ConcurrentHashMap[A, B])

def putIfAbsent(k: A, v: B): Option[B] = {
val r = underlying.putIfAbsent(k, v)
if (r != null) Some(r) else None
}
def putIfAbsent(k: A, v: B): Option[B] = Option(underlying.putIfAbsent(k, v))

def remove(k: A, v: B): Boolean = underlying.remove(k, v)

def replace(k: A, v: B): Option[B] = {
val prev = underlying.replace(k, v)
if (prev != null) Some(prev) else None
}
def replace(k: A, v: B): Option[B] = Option(underlying.replace(k, v))

def replace(k: A, oldvalue: B, newvalue: B): Boolean =
underlying.replace(k, oldvalue, newvalue)
Expand Down Expand Up @@ -380,25 +364,16 @@ private[collection] trait Wrappers {
case class JDictionaryWrapper[A, B](underlying: ju.Dictionary[A, B]) extends mutable.AbstractMap[A, B] with mutable.Map[A, B] {
override def size: Int = underlying.size

def get(k: A) = {
val v = underlying get k
if (v != null) Some(v) else None
}
def get(k: A) = Option(underlying get k)

def +=(kv: (A, B)): this.type = { underlying.put(kv._1, kv._2); this }
def -=(key: A): this.type = { underlying remove key; this }

override def put(k: A, v: B): Option[B] = {
val r = underlying.put(k, v)
if (r != null) Some(r) else None
}
override def put(k: A, v: B): Option[B] = Option(underlying.put(k, v))

override def update(k: A, v: B) { underlying.put(k, v) }

override def remove(k: A): Option[B] = {
val r = underlying remove k
if (r != null) Some(r) else None
}
override def remove(k: A): Option[B] = Option(underlying remove k)

def iterator = enumerationAsScalaIterator(underlying.keys) map (k => (k, underlying get k))

Expand Down
5 changes: 1 addition & 4 deletions src/library/scala/ref/WeakReference.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ object WeakReference {
def apply[T <: AnyRef](value: T) = new WeakReference(value)

/** Optionally returns the referenced value, or `None` if that value no longer exists */
def unapply[T <: AnyRef](wr: WeakReference[T]): Option[T] = {
val x = wr.underlying.get
if (x != null) Some(x) else None
}
def unapply[T <: AnyRef](wr: WeakReference[T]): Option[T] = Option(wr.underlying.get)
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/reflect/scala/reflect/runtime/TwoWayCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ private[runtime] class TwoWayCache[J, S] {
private object SomeRef {
def unapply[T](optRef: Option[WeakReference[T]]): Option[T] =
if (optRef.nonEmpty) {
val result = optRef.get.get
if (result != null) Some(result) else None
Option(optRef.get.get)
} else None
}

Expand Down
3 changes: 1 addition & 2 deletions src/reflect/scala/reflect/runtime/TwoWayCaches.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ private[runtime] trait TwoWayCaches { self: SymbolTable =>
private object SomeRef {
def unapply[T](optRef: Option[WeakReference[T]]): Option[T] =
if (optRef.nonEmpty) {
val result = optRef.get.get
if (result != null) Some(result) else None
Option(optRef.get.get)
} else None
}

Expand Down

0 comments on commit 58ae3e5

Please sign in to comment.