public class DynamicVariable<T>
extends java.lang.Object
The current value can be retrieved with the value() method.
New values should be pushed using the withValue(Object, F0) method.
Values pushed via withValue(Object, F0) only stay valid while the
withValue(Object, F0)'s second argument, a parameterless closure,
executes.
When the second argument finishes, the variable reverts to the previous
value.
someDynamicVariable.withValue(newValue, () -> {
// ... code called in here that calls value ...
// ... will be given back the newValue ...
})
Each thread gets its own stack of bindings. When a new thread is created, the `DynamicVariable` 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.
This class was ported from Scala 2.11 (`scala.util.DynamicVariable`), which as orginally written by Lex Spoon.
| Constructor and Description |
|---|
DynamicVariable(T init) |
| Modifier and Type | Method and Description |
|---|---|
void |
setValue(T newVal)
Change the currently bound value, discarding the old value.
|
java.lang.String |
toString() |
T |
value()
Retrieve the current value.
|
<S> S |
withValue(T newVal,
de.tototec.utils.functional.F0<S> thunk)
Set the value of the variable while executing the specified thunk.
|
public DynamicVariable(T init)
public T value()
public <S> S withValue(T newVal, de.tototec.utils.functional.F0<S> thunk)
newval - The value to which to set the variable.thunk - The code to evaluate under the new setting.public void setValue(T newVal)
withValue(Object, F0) gives better semantics.public java.lang.String toString()
toString in class java.lang.Object