package com.alibaba.mtc;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ding.lid
 * @since 0.10.0
 */
public class MtContextThreadLocal<T> extends InheritableThreadLocal<T> {
    /**
     * Override this method to have an initial value other than <tt>null</tt>.
     *
     * @see {@link ThreadLocal#initialValue()}
     */
    @Override
    protected T initialValue() {
        return super.initialValue();
    }

    /**
     * @see {@link java.lang.InheritableThreadLocal#childValue(Object)}
     */
    @Override
    protected T childValue(T parentValue) {
        return super.childValue(parentValue);
    }

    protected T copiedMtContextValue() {
        return get();
    }

    @Override
    public T get() {
        T value = super.get();
        addMtContextThreadLocal();
        return value;
    }

    @Override
    public void set(T value) {
        super.set(value);
        addMtContextThreadLocal();
    }

    @Override
    public void remove() {
        removeMtContextThreadLocal();
        super.remove();
    }

    private static final Object VALUE = new Object();
    final static Map<MtContextThreadLocal<?>, Object> holder = new ConcurrentHashMap<MtContextThreadLocal<?>, Object>(); // FIXME Use WeakReference so as to avoid memory leak 

    void addMtContextThreadLocal() {
        holder.put(this, VALUE);
    }

    void removeMtContextThreadLocal() {
        holder.remove(this);
    }
}

@@ -642,24 +642,533 @@
 ue);%0A    %7D%0A%0A
+    /**%0A     * Computes the context value for this multi-thread thread-local variable%0A     * as a function of the source thread's value at the time the task%0A     * Object is created.  This method is called from %7B@link com.alibaba.mtc.MtContextRunnable%7D or%0A     * %7B@link com.alibaba.mtc.MtContextCallable%7D when it create, before the task is started.%0A     * %3Cp/%3E%0A     * This method merely returns reference of its source thread value, and should be overridden%0A     * if a different behavior is desired.%0A     */%0A
     protecte

update javadoc