package com.alibaba.mtc;

import java.util.HashMap;
import java.util.Map;

/**
 * {@link MtContextThreadLocal} can transmit context from the thread of submitting task to the thread of executing task.
 * <p/>
 * Note: this class extends {@link java.lang.InheritableThreadLocal},
 * so {@link com.alibaba.mtc.MtContextThreadLocal} first is a {@link java.lang.InheritableThreadLocal}.
 *
 * @author ding.lid
 * @see MtContextRunnable
 * @see MtContextCallable
 * @since 0.10.0
 */
public class MtContextThreadLocal<T> extends InheritableThreadLocal<T> {
    /**
     * Computes the context value for this multi-thread context variable
     * as a function of the source thread's value at the time the task
     * Object is created.  This method is called from {@link com.alibaba.mtc.MtContextRunnable} or
     * {@link com.alibaba.mtc.MtContextCallable} when it create, before the task is started.
     * <p/>
     * This method merely returns reference of its source thread value, and should be overridden
     * if a different behavior is desired.
     */
    protected T copyMtContextValue() {
        return get();
    }

    /**
     * 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);
    }

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

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

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

    static ThreadLocal<Map<MtContextThreadLocal<?>, Object>> mtContextThreadLocalHolder = new ThreadLocal<Map<MtContextThreadLocal<?>, Object>>() {
        @Override
        protected Map<MtContextThreadLocal<?>, Object> initialValue() {
            return new HashMap<MtContextThreadLocal<?>, Object>();
        }
    };
    private static final Object VALUE = new Object();

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

    void removeMtContextThreadLocal() {
        mtContextThreadLocalHolder.get().remove(this);
    }

    static Map<MtContextThreadLocal<?>, Object> copy() {
        Map<MtContextThreadLocal<?>, Object> copy = new HashMap<MtContextThreadLocal<?>, Object>(mtContextThreadLocalHolder.get().size());
        for (Map.Entry<MtContextThreadLocal<?>, Object> entry : mtContextThreadLocalHolder.get().entrySet()) {
            MtContextThreadLocal<?> threadLocal = entry.getKey();
            copy.put(threadLocal, threadLocal.copyMtContextValue());
        }
        return copy;
    }

    static Map<MtContextThreadLocal<?>, Object> backupAndSet(Map<MtContextThreadLocal<?>, Object> set) {
        // backup MtContext
        Map<MtContextThreadLocal<?>, Object> backup = new HashMap<MtContextThreadLocal<?>, Object>(mtContextThreadLocalHolder.get().size());
        for (Map.Entry<MtContextThreadLocal<?>, Object> entry : set.entrySet()) {
            @SuppressWarnings("unchecked")
            MtContextThreadLocal<Object> threadLocal = (MtContextThreadLocal<Object>) entry.getKey();
            backup.put(threadLocal, threadLocal.get());

            threadLocal.set(entry.getValue());
        }
        return backup;
    }

    static void restore(Map<MtContextThreadLocal<?>, Object> backup) {
        // restore MtContext
        for (Map.Entry<MtContextThreadLocal<?>, Object> entry : backup.entrySet()) {
            @SuppressWarnings("unchecked")
            MtContextThreadLocal<Object> threadLocal = (MtContextThreadLocal<Object>) entry.getKey();
            threadLocal.set(entry.getValue());
        }
    }
}

@@ -62,19 +62,67 @@
 va.util.
-Map
+HashSet;%0Aimport java.util.Map;%0Aimport java.util.Set
 ;%0A%0A/**%0A 
@@ -1989,35 +1989,35 @@
 tic ThreadLocal%3C
-Map
+Set
 %3CMtContextThread
@@ -2024,24 +2024,16 @@
 Local%3C?%3E
-, Object
 %3E%3E mtCon
@@ -2072,19 +2072,19 @@
 adLocal%3C
-Map
+Set
 %3CMtConte
@@ -2099,24 +2099,16 @@
 Local%3C?%3E
-, Object
 %3E%3E() %7B%0A 
@@ -2142,19 +2142,19 @@
 otected 
-Map
+Set
 %3CMtConte
@@ -2169,24 +2169,16 @@
 Local%3C?%3E
-, Object
 %3E initia
@@ -2207,35 +2207,35 @@
  return new Hash
-Map
+Set
 %3CMtContextThread
@@ -2242,24 +2242,16 @@
 Local%3C?%3E
-, Object
 %3E();%0A   
@@ -2267,62 +2267,8 @@
   %7D;
-%0A    private static final Object VALUE = new Object();
 %0A%0A  
@@ -2347,23 +2347,16 @@
 t().
-put
+add
 (this
-, VALUE
 );%0A 
@@ -2668,34 +2668,24 @@
        for (
-Map.Entry%3C
 MtContextThr
@@ -2691,33 +2691,24 @@
 readLocal%3C?%3E
-, Object%3E
  entry : mtC
@@ -2736,27 +2736,16 @@
 er.get()
-.entrySet()
 ) %7B%0A    
@@ -2791,33 +2791,24 @@
 ocal = entry
-.getKey()
 ;%0A          

clean code