一个优雅的 ThreadLocal 工具类

Java 版

import java.util.*;

public final class ThreadLocalUtil {
    private static final ThreadLocal> threadLocal = new ThreadLocal() {
        protected Map initialValue() {
            return new HashMap(4);
        }
    };

    public static Map getThreadLocal(){
        return threadLocal.get();
    }
    public static  T get(String key) {
        Map map = (Map)threadLocal.get();
        return (T)map.get(key);
    }

    public static  T get(String key,T defaultValue) {
        Map map = (Map)threadLocal.get();
        return (T)map.get(key) == null ? defaultValue : (T)map.get(key);
    }

    public static void set(String key, Object value) {
        Map map = (Map)threadLocal.get();
        map.put(key, value);
    }

    public static void set(Map keyValueMap) {
        Map map = (Map)threadLocal.get();
        map.putAll(keyValueMap);
    }

    public static void remove() {
        threadLocal.remove();
    }

    public static  Map fetchVarsByPrefix(String prefix) {
        Map vars = new HashMap<>();
        if( prefix == null ){
            return vars;
        }
        Map map = (Map)threadLocal.get();
        Set set = map.entrySet();

        for( Map.Entry entry : set ){
            Object key = entry.getKey();
            if( key instanceof String ){
                if( ((String) key).startsWith(prefix) ){
                    vars.put((String)key,(T)entry.getValue());
                }
            }
        }
        return vars;
    }

    public static  T remove(String key) {
        Map map = (Map)threadLocal.get();
        return (T)map.remove(key);
    }

    public static void clear(String prefix) {
        if( prefix == null ){
            return;
        }
        Map map = (Map)threadLocal.get();
        Set set = map.entrySet();
        List removeKeys = new ArrayList<>();

        for( Map.Entry entry : set ){
            Object key = entry.getKey();
            if( key instanceof String ){
                if( ((String) key).startsWith(prefix) ){
                    removeKeys.add((String)key);
                }
            }
        }
        for( String key : removeKeys ){
            map.remove(key);
        }
    }
}

Kotlin 版

import java.util.ArrayList
import java.util.HashMap

open class ThreadLocalUtil {

    companion object {

        private val threadLocal = object : ThreadLocal>() {
            protected override fun initialValue(): Map {
                return HashMap(4)
            }
        }

        fun getThreadLocal(): Map {
            return threadLocal.get()
        }

        operator fun  get(key: String): T {
            val map: Map  = threadLocal.get() as HashMap
            return map[key] as T
        }

        operator fun  get(key: String, defaultValue: T): T {
            val map = threadLocal.get() as HashMap
            return if (map[key] as T == null) defaultValue else map[key] as T
        }

        operator fun set(key: String, value: Any) {
            val amap = threadLocal.get() as HashMap
            amap.put(key, value)
        }

        fun set(keyValueMap: Map) {
            val map = threadLocal.get() as HashMap
            map.putAll(keyValueMap)
        }

        fun remove() {
            threadLocal.remove()
        }

        fun  fetchVarsByPrefix(prefix: String?): Map {
            val vars = HashMap()
            if (prefix == null) {
                return vars
            }
            val map = threadLocal.get() as HashMap
            val set = map.entries

            for ((key, value) in set) {
                if (key is String) {
                    if (key.startsWith(prefix)) {
                        vars.put(key, value as T)
                    }
                }
            }
            return vars
        }

        fun  remove(key: String): T {
            val map = threadLocal.get() as HashMap
            return map.remove(key) as T
        }

        fun clear(prefix: String?) {
            if (prefix == null) {
                return
            }
            val map = threadLocal.get() as HashMap
            val set = map.entries
            val removeKeys = ArrayList()

            for ((key) in set) {
                if (key is String) {
                    if (key.startsWith(prefix)) {
                        removeKeys.add(key)
                    }
                }
            }
            for (key in removeKeys) {
                map.remove(key)
            }
        }
    }

}

你可能感兴趣的:(一个优雅的 ThreadLocal 工具类)