1 package org.slf4j.helpers;
2
3 import java.util.ArrayDeque;
4 import java.util.Deque;
5 import java.util.HashMap;
6 import java.util.Map;
7
8
9
10
11
12
13
14
15
16 public class ThreadLocalMapOfStacks {
17
18 final ThreadLocal<Map<String, Deque<String>>> tlMapOfStacks = new ThreadLocal<>();
19
20 public void pushByKey(String key, String value) {
21 if (key == null)
22 return;
23
24 Map<String, Deque<String>> map = tlMapOfStacks.get();
25
26 if (map == null) {
27 map = new HashMap<>();
28 tlMapOfStacks.set(map);
29 }
30
31 Deque<String> deque = map.get(key);
32 if (deque == null) {
33 deque = new ArrayDeque<>();
34 }
35 deque.push(value);
36 map.put(key, deque);
37 }
38
39 public String popByKey(String key) {
40 if (key == null)
41 return null;
42
43 Map<String, Deque<String>> map = tlMapOfStacks.get();
44 if (map == null)
45 return null;
46 Deque<String> deque = map.get(key);
47 if (deque == null)
48 return null;
49 return deque.pop();
50 }
51
52 public Deque<String> getCopyOfDequeByKey(String key) {
53 if (key == null)
54 return null;
55
56 Map<String, Deque<String>> map = tlMapOfStacks.get();
57 if (map == null)
58 return null;
59 Deque<String> deque = map.get(key);
60 if (deque == null)
61 return null;
62
63 return new ArrayDeque<String>(deque);
64 }
65
66
67
68
69
70
71
72
73 public void clearDequeByKey(String key) {
74 if (key == null)
75 return;
76
77 Map<String, Deque<String>> map = tlMapOfStacks.get();
78 if (map == null)
79 return;
80 Deque<String> deque = map.get(key);
81 if (deque == null)
82 return;
83 deque.clear();
84 }
85
86 }