package com.alibaba.mtc;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * {@link MtContext} maintains the multi-thread context.
 * <p/>
 * Use method {@link #get()} to get {@link MtContext} instance.
 *
 * @author ding.lid
 * @since 0.9.0
 */
public final class MtContext implements Serializable {
    private static final long serialVersionUID = -6658225204997224329L;

    private Map<String, Object> content = new HashMap<String, Object>();

    MtContext() {
    }

    MtContext(MtContext context) {
        this.content.putAll(context.content); // shallow copied map!!
    }

    /**
     * Get the whole context content.
     */
    public Map<String, Object> get() {
        return content;
    }

    /**
     * Get the value of key in context.
     */
    @SuppressWarnings("unchecked")
    public <V> V get(String key) {
        return (V) content.get(key);
    }

    /**
     * reset context content.
     */
    public void set(Map<String, Object> context) {
        if (null == context) {
            throw new NullPointerException("context argument is null!");
        }
        this.content.clear();
        this.content.putAll(context); // shallow copied map!!
    }

    /**
     * set the value of key.
     */
    public void set(String key, Object value) {
        content.put(key, value);
    }

    private static InheritableThreadLocal<MtContext> contextHolder = new InheritableThreadLocal<MtContext>() {
        @Override
        protected MtContext initialValue() {
            return new MtContext();
        }

        @Override
        protected MtContext childValue(MtContext parentValue) {
            return new MtContext(parentValue);
        }
    };

    /**
     * Get the context of current thread.
     */
    public static MtContext getContext() {
        return contextHolder.get();
    }
}

@@ -635,16 +635,35 @@
 Get the 
+%3Cb%3Ecopy%3C/b%3E of the 
 whole co
@@ -669,32 +669,32 @@
 ontext content.%0A
-
      */%0A    publ
@@ -720,24 +720,117 @@
 ct%3E get() %7B%0A
+        return new HashMap%3CString, Object%3E(content);%0A    %7D%0A%0A    Map%3CString, Object%3E get0() %7B%0A
         retu
@@ -1105,25 +1105,25 @@
 bject%3E conte
-x
+n
 t) %7B%0A       
@@ -1136,25 +1136,25 @@
 ull == conte
-x
+n
 t) %7B%0A       
@@ -1292,17 +1292,17 @@
 ll(conte
-x
+n
 t); // s
@@ -1320,32 +1320,123 @@
 ed map!!%0A    %7D%0A%0A
+    public void set0(Map%3CString, Object%3E content) %7B%0A        this.content = content;%0A    %7D%0A%0A
     /**%0A     * s

add inner method for MtContext