Permalink
Browse files

Rewrite SoftReferenceCache.apply for type safety.

We were using a tuple assignment, which converts into a Scala pattern
match that doesn't carry particularly nice type safety guarantees. We
now instead assign to a typed tuple and then unpack the tuple with our
own match statement, with a clarifying comment on the original
assignment to indicate what each part of the tuple means.
  • Loading branch information...
1 parent 51466b1 commit 688db0048f2af1e744ecd538f431cd51e9f9506c @Shadowfiend Shadowfiend committed with fmpwizard Oct 25, 2012
Showing with 13 additions and 10 deletions.
  1. +13 −10 core/util/src/main/scala/net/liftweb/util/SoftReferenceCache.scala
View
23 core/util/src/main/scala/net/liftweb/util/SoftReferenceCache.scala
@@ -119,27 +119,30 @@ class SoftReferenceCache[K, V](cacheSize: Int) {
* @return Box[V]
*/
def apply(key: K): Box[V] = {
- val (doRemove:Boolean, retval:Box[V]) =
+ val result:(Boolean,Box[V]) /* (doRemove, retval) */ =
lock(readLock) {
Box.!!(cache.get(key)) match {
case Full(value) =>
- Box.!!(value.get).map((false, _)) openOr {
+ Box.!!(value.get).map(value => (false, Full(value))) openOr {
(true, Empty)
}
case _ => (false, Empty)
}
}
- if (doRemove) {
- lock(writeLock) {
- val value = cache.get(key)
+ result match {
+ case (doRemove, retval) if doRemove =>
+ lock(writeLock) {
+ val value = cache.get(key)
- if (value != null && value.get == null)
- remove(key)
- }
- }
+ if (value != null && value.get == null)
+ remove(key)
+ }
- retval
+ retval
+ case (_, retval) =>
+ retval
+ }
}
/**

0 comments on commit 688db00

Please sign in to comment.