package com.statemachinesystems.util;

/**
 * A dynamically-scoped reference.
 *
 * @param <T>  the type of value being referenced
 */
public class DynamicReference<T> {

    private final InheritableThreadLocal<T> threadLocal;

    public DynamicReference(T initialValue) {
        this.threadLocal = new InheritableThreadLocal<T>() {
            @Override
            protected T initialValue() {
                return initialValue;
            }
        };
    }

    public T get() {
        return threadLocal.get();
    }

    public void withValue(T newValue, Runnable thunk) {
        T oldValue = threadLocal.get();
        threadLocal.set(newValue);

        try {
            thunk.run();
        } finally {
            threadLocal.set(oldValue);
        }
    }

    @Override
    public String toString() {
        return String.valueOf(get());
    }
}

@@ -222,16 +222,183 @@
 Local;%0A%0A
+    /**%0A     * Create a dynamically-scoped reference with the given initial default value.%0A     *%0A     * @param initialValue  the initial default value to use%0A     */%0A
     publ
@@ -636,65 +636,410 @@
     
-public T get() %7B%0A        return threadLocal.get();%0A    %7D%0A
+/**%0A     * Retrieve the current value of the reference.%0A     *%0A     * @return  the current value of the reference%0A     */%0A    public T get() %7B%0A        return threadLocal.get();%0A    %7D%0A%0A    /**%0A     * Override the value of the reference while executing the given block.%0A     *%0A     * @param newValue  the overridden value to use within the given block%0A     * @param block     the block to execute%0A     */
 %0A   
@@ -1082,20 +1082,20 @@
 unnable 
-thun
+bloc
 k) %7B%0A   
@@ -1197,12 +1197,12 @@
     
-thun
+bloc
 k.ru

Add JavaDoc