|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Network New Technologies Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
1 | 16 | package com.networknt.schema;
|
2 | 17 |
|
3 | 18 | import java.util.HashMap;
|
|
11 | 26 | */
|
12 | 27 | public class CollectorContext {
|
13 | 28 |
|
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"; |
| 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"; |
17 | 32 |
|
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 |
| - } |
| 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 | + } |
22 | 37 |
|
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>(); |
| 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>(); |
27 | 42 |
|
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>(); |
| 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>(); |
32 | 47 |
|
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 |
| - } |
| 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 | + } |
45 | 59 |
|
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 |
| - } |
| 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 | + } |
57 | 71 |
|
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 |
| - } |
| 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 | + } |
77 | 90 |
|
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 |
| - } |
| 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 | + } |
92 | 104 |
|
93 |
| - /** |
94 |
| - * Reset the context |
95 |
| - */ |
96 |
| - void reset() { |
97 |
| - this.collectorMap = new HashMap<String, Object>(); |
98 |
| - this.collectorLoadMap = new HashMap<String, Object>(); |
99 |
| - } |
| 105 | + /** |
| 106 | + * Reset the context |
| 107 | + */ |
| 108 | + void reset() { |
| 109 | + this.collectorMap = new HashMap<String, Object>(); |
| 110 | + this.collectorLoadMap = new HashMap<String, Object>(); |
| 111 | + } |
100 | 112 |
|
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 |
| - } |
| 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 | + } |
112 | 124 |
|
113 |
| - } |
| 125 | + } |
114 | 126 |
|
115 | 127 | }
|
0 commit comments