2
* Copyright 2009 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS-IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package net.appjet.oui;
19
class NoninheritedDynamicVariable[T](init: T) {
20
private val tl = new ThreadLocal[T] {
21
override def initialValue = init.asInstanceOf[T with AnyRef]
24
/** Retrieve the current value */
25
def value: T = tl.get.asInstanceOf[T]
28
/** Set the value of the variable while executing the specified
31
* @param newval The value to which to set the fluid
32
* @param thunk The code to evaluate under the new setting
34
def withValue[S](newval: T)(thunk: =>S): S = {
38
try { thunk } finally {
43
/** Change the currently bound value, discarding the old value.
44
* Usually <code>withValue()</code> gives better semantics.
46
def value_=(newval: T) = { tl.set(newval) }
48
override def toString: String = "NoninheritedDynamicVariable(" + value +")"