All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.chobit.spring.autoconfigure.trace.InheritableTraceHolder Maven / Gradle / Ivy

The newest version!
package org.chobit.spring.autoconfigure.trace;

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

public final class InheritableTraceHolder {


    private final InheritableThreadLocal> inheritableThreadLocal = new InheritableThreadLocal>() {
        @Override
        protected Map childValue(Map parentValue) {
            if (parentValue == null) {
                return null;
            }
            return new HashMap<>(parentValue);
        }
    };



    public void put(String key, String val) {
        if (key == null) {
            throw new IllegalArgumentException("key cannot be null");
        }
        Map map = inheritableThreadLocal.get();
        if (map == null) {
            map = new HashMap<>();
            inheritableThreadLocal.set(map);
        }
        map.put(key, val);
    }



    public String get(String key) {
        Map map = inheritableThreadLocal.get();
        if ((map != null) && (key != null)) {
            return map.get(key);
        } else {
            return null;
        }
    }


    public void remove(String key) {
        Map map = inheritableThreadLocal.get();
        if (map != null) {
            map.remove(key);
        }
    }



    public void clear() {
        Map map = inheritableThreadLocal.get();
        if (map != null) {
            map.clear();
            inheritableThreadLocal.remove();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy