|
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 | + */ |
| 68 | + public <E> void add(String name, Object object) { |
| 69 | + collectorMap.put(name, object); |
| 70 | + } |
71 | 71 |
|
72 |
| - /** |
73 |
| - * Gets the data associated with a given name. Please note if you are using a |
74 |
| - * Collector you should wait till the validation is complete to gather all data. |
75 |
| - * <p> |
76 |
| - * For a Collector, this method will return the collector as long as load method |
77 |
| - * is not called. Once the load method is called this method will return the |
78 |
| - * actual data collected by collector. |
79 |
| - * |
80 |
| - * @param name String |
81 |
| - * @return Object |
82 |
| - */ |
83 |
| - public Object get(String name) { |
84 |
| - Object object = collectorMap.get(name); |
85 |
| - if (object instanceof Collector<?> && (collectorLoadMap.get(name) != null)) { |
86 |
| - return collectorLoadMap.get(name); |
87 |
| - } |
88 |
| - return collectorMap.get(name); |
89 |
| - } |
| 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 | + } |
90 | 103 |
|
91 |
| - /** |
92 |
| - * Combines data with Collector identified by the given name. |
93 |
| - * |
94 |
| - * @param name String |
95 |
| - * @param data Object |
96 |
| - */ |
97 |
| - public void combineWithCollector(String name, Object data) { |
98 |
| - Object object = collectorMap.get(name); |
99 |
| - if (object instanceof Collector<?>) { |
100 |
| - Collector<?> collector = (Collector<?>) object; |
101 |
| - collector.combine(data); |
102 |
| - } |
103 |
| - } |
| 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 | + } |
104 | 117 |
|
105 |
| - /** |
106 |
| - * Reset the context |
107 |
| - */ |
108 |
| - void reset() { |
109 |
| - this.collectorMap = new HashMap<String, Object>(); |
110 |
| - this.collectorLoadMap = new HashMap<String, Object>(); |
111 |
| - } |
| 118 | + /** |
| 119 | + * Reset the context |
| 120 | + */ |
| 121 | + void reset() { |
| 122 | + this.collectorMap = new HashMap<String, Object>(); |
| 123 | + this.collectorLoadMap = new HashMap<String, Object>(); |
| 124 | + } |
112 | 125 |
|
113 |
| - /** |
114 |
| - * Loads data from all collectors. |
115 |
| - */ |
116 |
| - void loadCollectors() { |
117 |
| - Set<Entry<String, Object>> entrySet = collectorMap.entrySet(); |
118 |
| - for (Entry<String, Object> entry : entrySet) { |
119 |
| - if (entry.getValue() instanceof Collector<?>) { |
120 |
| - Collector<?> collector = (Collector<?>) entry.getValue(); |
121 |
| - collectorLoadMap.put(entry.getKey(), collector.collect()); |
122 |
| - } |
123 |
| - } |
| 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 | + } |
124 | 137 |
|
125 |
| - } |
| 138 | + } |
126 | 139 |
|
127 | 140 | }
|
0 commit comments