package com.oldratlee.mtc;

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

/**
 * {@link MtContext} maintains the multi-thread context.
 *
 * @author ding.lid
 */
public final class MtContext implements Serializable {
    private static final long serialVersionUID = -6658225204997224329L;

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

    public Map<String, Object> get() {
        return content;
    }

    public Object get(String key) {
        return content.get(key);
    }

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

    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) {
            MtContext ret = new MtContext();
            ret.content = new HashMap<String, Object>(parentValue.content); // shallow copied map!!
            return ret;
        }
    };

    public static MtContext getContext() {
        return contextHolder.get();
    }
}

@@ -389,16 +389,66 @@
 ct%3E();%0A%0A
+    /**%0A     * Get whole context content.%0A     */%0A
     publ
@@ -501,32 +501,88 @@
 content;%0A    %7D%0A%0A
+    /**%0A     * Get the value of key in context.%0A     */%0A
     public Objec
@@ -633,32 +633,78 @@
 et(key);%0A    %7D%0A%0A
+    /**%0A     * reset context content.%0A     */%0A
     public void 
@@ -940,24 +940,69 @@
 ap!!%0A    %7D%0A%0A
+    /**%0A     * set the value of key.%0A     */%0A
     public v
@@ -1567,16 +1567,74 @@
     %7D;%0A%0A
+    /**%0A     * Get the context of current thread.%0A     */%0A
     publ

improve javadoc of MtContext