|
26 | 26 | */
|
27 | 27 | public class CollectorContext {
|
28 | 28 |
|
29 |
| - // Using a namespace string as key in ThreadLocal so that it is unique in |
30 |
| - // ThreadLocal. |
31 |
| - static final String COLLECTOR_CONTEXT_THREAD_LOCAL_KEY = "com.networknt.schema.CollectorKey"; |
| 29 | + // Using a namespace string as key in ThreadLocal so that it is unique in |
| 30 | + // ThreadLocal. |
| 31 | + static final String COLLECTOR_CONTEXT_THREAD_LOCAL_KEY = "com.networknt.schema.CollectorKey"; |
32 | 32 |
|
33 |
| - // Get an instance from thread info (which uses ThreadLocal). |
34 |
| - public static CollectorContext getInstance() { |
35 |
| - return (CollectorContext) ThreadInfo.get(COLLECTOR_CONTEXT_THREAD_LOCAL_KEY); |
36 |
| - } |
| 33 | + // Get an instance from thread info (which uses ThreadLocal). |
| 34 | + public static CollectorContext getInstance() { |
| 35 | + return (CollectorContext) ThreadInfo.get(COLLECTOR_CONTEXT_THREAD_LOCAL_KEY); |
| 36 | + } |
37 | 37 |
|
38 |
| - /** |
39 |
| - * Map for holding the name and {@link Collector} or a simple Object. |
40 |
| - */ |
41 |
| - private Map<String, Object> collectorMap = new HashMap<String, Object>(); |
| 38 | + /** |
| 39 | + * Map for holding the name and {@link Collector} or a simple Object. |
| 40 | + */ |
| 41 | + private Map<String, Object> collectorMap = new HashMap<String, Object>(); |
42 | 42 |
|
43 |
| - /** |
44 |
| - * Map for holding the name and {@link Collector} class collect method output. |
45 |
| - */ |
46 |
| - private Map<String, Object> collectorLoadMap = new HashMap<String, Object>(); |
| 43 | + /** |
| 44 | + * Map for holding the name and {@link Collector} class collect method output. |
| 45 | + */ |
| 46 | + private Map<String, Object> collectorLoadMap = new HashMap<String, Object>(); |
47 | 47 |
|
48 |
| - /** |
49 |
| - * Adds a collector with give name. Preserving this method for backward |
50 |
| - * compatibility. |
51 |
| - * |
52 |
| - * @param <E> element |
53 |
| - * @param name String |
54 |
| - * @param collector Collector |
55 |
| - */ |
56 |
| - public <E> void add(String name, Collector<E> collector) { |
57 |
| - collectorMap.put(name, collector); |
58 |
| - } |
| 48 | + /** |
| 49 | + * Adds a collector with give name. Preserving this method for backward |
| 50 | + * compatibility. |
| 51 | + * |
| 52 | + * @param <E> element |
| 53 | + * @param name String |
| 54 | + * @param collector Collector |
| 55 | + */ |
| 56 | + public <E> void add(String name, Collector<E> collector) { |
| 57 | + collectorMap.put(name, collector); |
| 58 | + } |
59 | 59 |
|
60 |
| - /** |
61 |
| - * Adds a collector or a simple object with give name. |
62 |
| - * |
63 |
| - * @param <E> element |
64 |
| - * @param object Object |
65 |
| - * @param name String |
66 |
| - * |
67 |
| - */ |
68 |
| - public <E> void add(String name, Object object) { |
69 |
| - collectorMap.put(name, object); |
70 |
| - } |
| 60 | + /** |
| 61 | + * Adds a collector or a simple object with give name. |
| 62 | + * |
| 63 | + * @param <E> element |
| 64 | + * @param object Object |
| 65 | + * @param name String |
| 66 | + */ |
| 67 | + public <E> void add(String name, Object object) { |
| 68 | + collectorMap.put(name, object); |
| 69 | + } |
71 | 70 |
|
72 |
| - /** |
73 |
| - * Gets the data associated with a given name. Please note if you are collecting |
74 |
| - * {@link Collector} instances you should wait till the validation is complete |
75 |
| - * to gather all data. |
76 |
| - * <p> |
77 |
| - * When {@link CollectorContext} is used to collect {@link Collector} instances |
78 |
| - * for a particular key, this method will return the {@link Collector} instance |
79 |
| - * as long as {@link #loadCollectors} method is not called. Once |
80 |
| - * the {@link #loadCollectors} method is called this method will |
81 |
| - * return the actual data collected by collector. |
82 |
| - * |
83 |
| - * @param name String |
84 |
| - * @return Object |
85 |
| - */ |
86 |
| - public Object get(String name) { |
87 |
| - Object object = collectorMap.get(name); |
88 |
| - if (object instanceof Collector<?> && (collectorLoadMap.get(name) != null)) { |
89 |
| - return collectorLoadMap.get(name); |
90 |
| - } |
91 |
| - return collectorMap.get(name); |
92 |
| - } |
93 |
| - |
94 |
| - /** |
95 |
| - * Returns all the collected data. Please look into {@link #get(String)} method for more details. |
96 |
| - */ |
97 |
| - public Map<String,Object> getAll() { |
98 |
| - Map<String,Object> mergedMap = new HashMap<String, Object>(); |
99 |
| - mergedMap.putAll(collectorMap); |
100 |
| - mergedMap.putAll(collectorLoadMap); |
101 |
| - return mergedMap; |
102 |
| - } |
| 71 | + /** |
| 72 | + * Gets the data associated with a given name. Please note if you are collecting |
| 73 | + * {@link Collector} instances you should wait till the validation is complete |
| 74 | + * to gather all data. |
| 75 | + * <p> |
| 76 | + * When {@link CollectorContext} is used to collect {@link Collector} instances |
| 77 | + * for a particular key, this method will return the {@link Collector} instance |
| 78 | + * as long as {@link #loadCollectors} method is not called. Once |
| 79 | + * the {@link #loadCollectors} method is called this method will |
| 80 | + * return the actual data collected by collector. |
| 81 | + * |
| 82 | + * @param name String |
| 83 | + * @return Object |
| 84 | + */ |
| 85 | + public Object get(String name) { |
| 86 | + Object object = collectorMap.get(name); |
| 87 | + if (object instanceof Collector<?> && (collectorLoadMap.get(name) != null)) { |
| 88 | + return collectorLoadMap.get(name); |
| 89 | + } |
| 90 | + return collectorMap.get(name); |
| 91 | + } |
103 | 92 |
|
104 |
| - /** |
105 |
| - * Combines data with Collector identified by the given name. |
106 |
| - * |
107 |
| - * @param name String |
108 |
| - * @param data Object |
109 |
| - */ |
110 |
| - public void combineWithCollector(String name, Object data) { |
111 |
| - Object object = collectorMap.get(name); |
112 |
| - if (object instanceof Collector<?>) { |
113 |
| - Collector<?> collector = (Collector<?>) object; |
114 |
| - collector.combine(data); |
115 |
| - } |
116 |
| - } |
| 93 | + /** |
| 94 | + * Returns all the collected data. Please look into {@link #get(String)} method for more details. |
| 95 | + */ |
| 96 | + public Map<String, Object> getAll() { |
| 97 | + Map<String, Object> mergedMap = new HashMap<String, Object>(); |
| 98 | + mergedMap.putAll(collectorMap); |
| 99 | + mergedMap.putAll(collectorLoadMap); |
| 100 | + return mergedMap; |
| 101 | + } |
117 | 102 |
|
118 |
| - /** |
119 |
| - * Reset the context |
120 |
| - */ |
121 |
| - void reset() { |
122 |
| - this.collectorMap = new HashMap<String, Object>(); |
123 |
| - this.collectorLoadMap = new HashMap<String, Object>(); |
124 |
| - } |
| 103 | + /** |
| 104 | + * Combines data with Collector identified by the given name. |
| 105 | + * |
| 106 | + * @param name String |
| 107 | + * @param data Object |
| 108 | + */ |
| 109 | + public void combineWithCollector(String name, Object data) { |
| 110 | + Object object = collectorMap.get(name); |
| 111 | + if (object instanceof Collector<?>) { |
| 112 | + Collector<?> collector = (Collector<?>) object; |
| 113 | + collector.combine(data); |
| 114 | + } |
| 115 | + } |
125 | 116 |
|
126 |
| - /** |
127 |
| - * Loads data from all collectors. |
128 |
| - */ |
129 |
| - void loadCollectors() { |
130 |
| - Set<Entry<String, Object>> entrySet = collectorMap.entrySet(); |
131 |
| - for (Entry<String, Object> entry : entrySet) { |
132 |
| - if (entry.getValue() instanceof Collector<?>) { |
133 |
| - Collector<?> collector = (Collector<?>) entry.getValue(); |
134 |
| - collectorLoadMap.put(entry.getKey(), collector.collect()); |
135 |
| - } |
136 |
| - } |
| 117 | + /** |
| 118 | + * Reset the context |
| 119 | + */ |
| 120 | + void reset() { |
| 121 | + this.collectorMap = new HashMap<String, Object>(); |
| 122 | + this.collectorLoadMap = new HashMap<String, Object>(); |
| 123 | + } |
137 | 124 |
|
138 |
| - } |
| 125 | + /** |
| 126 | + * Loads data from all collectors. |
| 127 | + */ |
| 128 | + void loadCollectors() { |
| 129 | + Set<Entry<String, Object>> entrySet = collectorMap.entrySet(); |
| 130 | + for (Entry<String, Object> entry : entrySet) { |
| 131 | + if (entry.getValue() instanceof Collector<?>) { |
| 132 | + Collector<?> collector = (Collector<?>) entry.getValue(); |
| 133 | + collectorLoadMap.put(entry.getKey(), collector.collect()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + } |
139 | 138 |
|
140 | 139 | }
|
0 commit comments