|
3 | 3 | import java.util.HashMap; |
4 | 4 | import java.util.Map; |
5 | 5 | import java.util.Map.Entry; |
| 6 | +import java.util.Set; |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * Context for holding the output returned by the {@link Collector} |
9 | 10 | * implementations. |
10 | 11 | */ |
11 | 12 | public class CollectorContext { |
12 | 13 |
|
13 | | - static final String COLLECTOR_CONTEXT_THREAD_LOCAL_KEY = "COLLECTOR_CONTEXT_THREAD_LOCAL_KEY"; |
14 | | - |
15 | | - // Get an instance from thread info (which uses ThreadLocal). |
16 | | - public static CollectorContext getInstance() { |
17 | | - return (CollectorContext) ThreadInfo.get(COLLECTOR_CONTEXT_THREAD_LOCAL_KEY); |
18 | | - } |
19 | | - |
20 | | - /** |
21 | | - * Map for holding the collector type and {@link Collector} |
22 | | - */ |
23 | | - private Map<String, Collector<?>> collectorMap = new HashMap<String, Collector<?>>(); |
24 | | - |
25 | | - /** |
26 | | - * Map for holding the collector type and {@link Collector} class collect method |
27 | | - * output. |
28 | | - */ |
29 | | - private Map<String, Object> collectorLoadMap = new HashMap<String, Object>(); |
30 | | - |
31 | | - public <E> void add(String collectorType, Collector<E> collector) { |
32 | | - collectorMap.put(collectorType, collector); |
33 | | - } |
34 | | - |
35 | | - public Object get(String collectorType) { |
36 | | - if (collectorLoadMap.get(collectorType) == null && collectorMap.get(collectorType) != null) { |
37 | | - collectorLoadMap.put(collectorType, collectorMap.get(collectorType).collect()); |
38 | | - } |
39 | | - return collectorLoadMap.get(collectorType); |
40 | | - } |
41 | | - |
42 | | - /** |
43 | | - * Load all the collectors associated with the context. |
44 | | - */ |
45 | | - void load() { |
46 | | - for (Entry<String, Collector<?>> collectorEntrySet : collectorMap.entrySet()) { |
47 | | - collectorLoadMap.put(collectorEntrySet.getKey(), collectorEntrySet.getValue().collect()); |
48 | | - } |
49 | | - } |
50 | | - |
51 | | - /** |
52 | | - * Reset the context |
53 | | - */ |
54 | | - void reset() { |
55 | | - this.collectorMap = new HashMap<String, Collector<?>>(); |
56 | | - this.collectorLoadMap = new HashMap<String, Object>(); |
57 | | - } |
| 14 | + // Using a namespace string as key in ThreadLocal so that it is unique in |
| 15 | + // ThreadLocal. |
| 16 | + static final String COLLECTOR_CONTEXT_THREAD_LOCAL_KEY = "com.networknt.schema.CollectorKey"; |
| 17 | + |
| 18 | + // Get an instance from thread info (which uses ThreadLocal). |
| 19 | + public static CollectorContext getInstance() { |
| 20 | + return (CollectorContext) ThreadInfo.get(COLLECTOR_CONTEXT_THREAD_LOCAL_KEY); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Map for holding the name and {@link Collector} or a simple Object. |
| 25 | + */ |
| 26 | + private Map<String, Object> collectorMap = new HashMap<String, Object>(); |
| 27 | + |
| 28 | + /** |
| 29 | + * Map for holding the name and {@link Collector} class collect method output. |
| 30 | + */ |
| 31 | + private Map<String, Object> collectorLoadMap = new HashMap<String, Object>(); |
| 32 | + |
| 33 | + /** |
| 34 | + * |
| 35 | + * Adds a collector with give name. Preserving this method for backward |
| 36 | + * compatibility. |
| 37 | + * |
| 38 | + * @param <E> |
| 39 | + * @param name |
| 40 | + * @param collector |
| 41 | + */ |
| 42 | + public <E> void add(String name, Collector<E> collector) { |
| 43 | + collectorMap.put(name, collector); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * |
| 48 | + * Adds a collector or a simple object with give name. |
| 49 | + * |
| 50 | + * @param <E> |
| 51 | + * @param name |
| 52 | + * @param collector |
| 53 | + */ |
| 54 | + public <E> void add(String name, Object object) { |
| 55 | + collectorMap.put(name, object); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * |
| 60 | + * Gets the data associated with a given name. Please note if you are using a |
| 61 | + * Collector you should wait till the validation is complete to gather all data. |
| 62 | + * |
| 63 | + * For a Collector, this method will return the collector as long as load method |
| 64 | + * is not called. Once the load method is called this method will return the |
| 65 | + * actual data collected by collector. |
| 66 | + * |
| 67 | + * @param name |
| 68 | + * @return |
| 69 | + */ |
| 70 | + public Object get(String name) { |
| 71 | + Object object = collectorMap.get(name); |
| 72 | + if (object instanceof Collector<?> && (collectorLoadMap.get(name) != null)) { |
| 73 | + return collectorLoadMap.get(name); |
| 74 | + } |
| 75 | + return collectorMap.get(name); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * |
| 80 | + * Combines data with Collector identified by the given name. |
| 81 | + * |
| 82 | + * @param name |
| 83 | + * @param data |
| 84 | + */ |
| 85 | + public void combineWithCollector(String name, Object data) { |
| 86 | + Object object = collectorMap.get(name); |
| 87 | + if (object instanceof Collector<?>) { |
| 88 | + Collector<?> collector = (Collector<?>) object; |
| 89 | + collector.combine(data); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Reset the context |
| 95 | + */ |
| 96 | + void reset() { |
| 97 | + this.collectorMap = new HashMap<String, Object>(); |
| 98 | + this.collectorLoadMap = new HashMap<String, Object>(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Loads data from all collectors. |
| 103 | + */ |
| 104 | + void loadCollectors() { |
| 105 | + Set<Entry<String, Object>> entrySet = collectorMap.entrySet(); |
| 106 | + for (Entry<String, Object> entry : entrySet) { |
| 107 | + if (entry.getValue() instanceof Collector<?>) { |
| 108 | + Collector<?> collector = (Collector<?>) entry.getValue(); |
| 109 | + collectorLoadMap.put(entry.getKey(), collector.collect()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + } |
58 | 114 |
|
59 | 115 | } |
0 commit comments