-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOptContext.java
49 lines (42 loc) · 1.11 KB
/
OptContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package kim.hanjie.common.opt;
import java.util.HashMap;
import java.util.Map;
/**
* @author han
* @date 2021/3/10
*/
public class OptContext {
private static ThreadLocal<Map<String, String>> optContextHolder = new ThreadLocal<>();
public static void put(String key, String value) {
if (key == null) {
return;
}
Map<String, String> map = optContextHolder.get();
if (map == null) {
map = new HashMap<>(16);
}
map.put(key, value);
optContextHolder.set(map);
}
public static String get(String key) {
if (key == null) {
return null;
}
Map<String, String> map = optContextHolder.get();
if (map != null) {
return map.get(key);
}
return null;
}
public static void clean() {
optContextHolder.remove();
}
public static Map<String, String> getCopyOfContextMap() {
Map<String, String> map = optContextHolder.get();
if (map == null) {
return null;
} else {
return new HashMap<>(map);
}
}
}