diff options
author | Lex Spoon <lex@lexspoon.org> | 2007-05-21 15:41:16 +0000 |
---|---|---|
committer | Lex Spoon <lex@lexspoon.org> | 2007-05-21 15:41:16 +0000 |
commit | 22a8618b48e945323723755525bbdeb97c63da3e (patch) | |
tree | 3f73ee56fd3889997aed3ca514e7b45d0f1f3852 | |
parent | 9c91674927c2b0fa9e72f9b5a622255ac02400eb (diff) | |
download | scala-22a8618b48e945323723755525bbdeb97c63da3e.tar.gz scala-22a8618b48e945323723755525bbdeb97c63da3e.tar.bz2 scala-22a8618b48e945323723755525bbdeb97c63da3e.zip |
Renamed Fluid to DynamicVariable
-rw-r--r-- | src/library/scala/Console.scala | 18 | ||||
-rw-r--r-- | src/library/scala/util/DynamicVariable.scala | 84 | ||||
-rw-r--r-- | src/library/scala/util/Fluid.scala | 69 |
3 files changed, 98 insertions, 73 deletions
diff --git a/src/library/scala/Console.scala b/src/library/scala/Console.scala index 73f1e8c63e..0897f9a7cb 100644 --- a/src/library/scala/Console.scala +++ b/src/library/scala/Console.scala @@ -15,7 +15,7 @@ import java.io.{InputStream, Reader, InputStreamReader, BufferedReader} import java.io.{OutputStream, PrintStream} import java.text.MessageFormat -import scala.util.Fluid +import scala.util.DynamicVariable import Predef._ @@ -57,12 +57,12 @@ object Console { final val REVERSED = "\033[7m" final val INVISIBLE = "\033[8m" - private val outFluid = new Fluid[PrintStream](java.lang.System.out) - private val inFluid = new Fluid[BufferedReader]( + private val outVar = new DynamicVariable[PrintStream](java.lang.System.out) + private val inVar = new DynamicVariable[BufferedReader]( new BufferedReader(new InputStreamReader(java.lang.System.in))) - def out = outFluid.value - def in = inFluid.value + def out = outVar.value + def in = inVar.value val err = java.lang.System.err @@ -70,7 +70,7 @@ object Console { * * @param out the new output stream. */ - def setOut(out: PrintStream): Unit = outFluid.value = out + def setOut(out: PrintStream): Unit = outVar.value = out /** Set the default output stream for the duration * of execution of one thunk. @@ -81,7 +81,7 @@ object Console { * @return ... */ def withOut[T](out: PrintStream)(thunk: =>T): T = - outFluid.withValue(out)(thunk) + outVar.withValue(out)(thunk) /** Set the default output stream. * @@ -107,7 +107,7 @@ object Console { * @param reader specifies the new input stream. */ def setIn(reader: Reader): Unit = { - inFluid.value = new BufferedReader(reader) + inVar.value = new BufferedReader(reader) } /** Set the default input stream for the duration @@ -118,7 +118,7 @@ object Console { * the new input stream active */ def withIn[T](reader: Reader)(thunk: =>T): T = - inFluid.withValue(new BufferedReader(reader))(thunk) + inVar.withValue(new BufferedReader(reader))(thunk) /** Set the default input stream. diff --git a/src/library/scala/util/DynamicVariable.scala b/src/library/scala/util/DynamicVariable.scala new file mode 100644 index 0000000000..d6718c6d52 --- /dev/null +++ b/src/library/scala/util/DynamicVariable.scala @@ -0,0 +1,84 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.util + + +import Predef._ +import java.lang.InheritableThreadLocal + +/** <p> + * DynamicVariables provide a binding mechanism where the current + * value is found through <em>dynamic scope</em>, but where + * access to the variable itself is resolved through <em>static + * scope</em>. + * </p> + * <p> + * The current value can be retrieved with the + * <code>value</code> method. New values should be + * pushed using the <code>withValue</code> method. + * Values pushed via <code>withValue</code> only + * stay valid while the <code>withValue</code>'s + * <em>second</em> argument, a parameterless closure, + * executes. When the second argument finishes, + * the variable reverts to the previous value. + * </p> + * <p> + * Usage of <code>withValue</code> looks like this: + * </p> + * <blockquote><pre> + * someDynamicVariable.withValue(newValue) { + * // ... code called in here that calls value ... + * // ... will be given back the newValue ... + * } + * </pre></blockquote> + * <p> + * Each thread gets its own stack of bindings. When a + * new thread is created, the fluid gets a copy of + * the stack of bindings from the parent thread, and + * from then on the bindings for the new thread + * are independent of those for the original thread. + * </p> + * + * @author Lex Spoon + * @version 1.1, 2007-5-21 + */ +class DynamicVariable[T](init: T) { + private val tl = new InheritableThreadLocal { + override def initialValue = init.asInstanceOf[AnyRef] + } + + /** Retrieve the current value */ + def value: T = tl.get.asInstanceOf[T] + + + /** Set the value of the variable while executing the specified + * thunk. + * + * @param newval The value to which to set the fluid + * @param thunk The code to evaluate under the new setting + */ + def withValue[S](newval: T)(thunk: =>S): S = { + val oldval = value + tl.set(newval) + + try { thunk } finally { + tl.set(oldval) + } + } + + /** Change the currently bound value, discarding the old value. + * Usually <code>withValue()</code> gives better semantics. + */ + def value_=(newval: T) = { tl.set(newval) } + + override def toString: String = "DynamicVariable(" + value +")" +} diff --git a/src/library/scala/util/Fluid.scala b/src/library/scala/util/Fluid.scala index 046ad2decc..0b59ff3629 100644 --- a/src/library/scala/util/Fluid.scala +++ b/src/library/scala/util/Fluid.scala @@ -15,70 +15,11 @@ package scala.util import Predef._ import java.lang.InheritableThreadLocal -/** <p> - * Fluids provide a binding mechanism where the current - * value is found through <em>dynamic scope</em>, but where - * access to the fluid itself is resolved through </em>static - * binding</em> to a variable referencing the fluid. - * </p> - * <p> - * The current value can be retrieved with the - * <code>value</code> method. New values can be - * pushed using the <code>withValue</code> method. - * Values pushed via <code>withValue</code> only - * stay valid while the <code>withValue</code>'s - * <em>second</em> argument, a parameterless closure, - * executes. When the second argument finishes, - * the fluid reverts to the previous value. - * </p> - * <p> - * Usage of <code>withValue</code> looks like this: - * </p> - * <blockquote><pre> - * someFluid.withValue(newValue) { - * // ... code called in here that calls value ... - * // ... will be given back the newValue ... - * } - * </pre></blockquote> - * <p> - * Each thread gets its own stack of bindings. When a - * new thread is created, the fluid gets a copy of - * the stack of bindings from the parent thread, and - * from then on the bindings for the new thread - * are independent of those for the original thread. - * </p> +/** <p>This is the same as DynamicVariable. The name "fluid" + * is confusing, and perhaps not even accurate.</p> * * @author Lex Spoon - * @version 1.0, 21/03/2006 + * @version 1.1, 2007-5-21 */ -class Fluid[T](init: T) { - private val tl = new InheritableThreadLocal { - override def initialValue = init.asInstanceOf[AnyRef] - } - - /** Retrieve the current value */ - def value: T = tl.get.asInstanceOf[T] - - - /** Set the value of the fluid while executing the specified - * thunk. - * - * @param newval The value to which to set the fluid - * @param thunk The code to evaluate under the new setting - */ - def withValue[S](newval: T)(thunk: =>S): S = { - val oldval = value - tl.set(newval) - - try { thunk } finally { - tl.set(oldval) - } - } - - /** Change the currently bound value, discarding the old value. - * Usually <code>withValue()</code> gives better semantics. - */ - def value_=(newval: T) = { tl.set(newval) } - - override def toString: String = "Fluid(" + value +")" -} +@deprecated +class Fluid[T](init: T) extends DynamicVariable[T](init) |