Added alter method to TransactionalRef

This commit is contained in:
Peter Vlugter 2010-04-09 05:17:36 +08:00 committed by Jonas Bonér
parent e780ba0f82
commit ebab76b348

View file

@ -102,6 +102,13 @@ class TransactionalRef[T] extends Transactional {
ref.set(elem)
}
def alter(f: T => T): T = {
ensureIsInTransaction
ensureNotNull
ref.set(f(ref.get))
ref.get
}
def get: Option[T] = {
ensureIsInTransaction
if (ref.isNull) None
@ -171,6 +178,9 @@ class TransactionalRef[T] extends Transactional {
private def ensureIsInTransaction =
if (getThreadLocalTransaction eq null) throw new NoTransactionInScopeException
private def ensureNotNull =
if (ref.isNull) throw new RuntimeException("Cannot alter Ref's value when it is null")
}
object TransactionalMap {