Skip to content

Commit cad7141

Browse files
authored
Merge pull request #289 from prashanthjos/master
Adding getAll method on CollectorContext class.
2 parents 51e18fd + 5120bce commit cad7141

File tree

2 files changed

+127
-97
lines changed

2 files changed

+127
-97
lines changed

src/main/java/com/networknt/schema/CollectorContext.java

Lines changed: 100 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -26,102 +26,115 @@
2626
*/
2727
public class CollectorContext {
2828

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";
3232

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+
}
3737

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>();
4242

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>();
4747

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+
}
5959

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+
}
7171

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+
}
90103

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+
}
104117

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+
}
112125

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+
}
124137

125-
}
138+
}
126139

127140
}

src/test/java/com/networknt/schema/CollectorContextTest.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
public class CollectorContextTest {
2929

3030
private static final String SAMPLE_COLLECTOR = "sampleCollectorType";
31+
32+
private static final String SAMPLE_COLLECTOR_OTHER = "sampleCollectorOtherType";
3133

3234
private JsonSchema jsonSchema;
3335

@@ -92,14 +94,29 @@ public void testCollectorContextWithMultiplThreads() throws Exception {
9294
Assert.assertEquals(contextValue3.get(0), "actual_value_added_to_context3");
9395
}
9496

97+
@SuppressWarnings("unchecked")
98+
@Test
99+
public void testCollectorWithFormat() throws JsonMappingException, JsonProcessingException {
100+
ObjectMapper objectMapper = new ObjectMapper();
101+
ValidationResult validationResult = jsonSchemaForCombine.validateAndCollect(objectMapper
102+
.readTree("{\"property1\":\"sample1\",\"property2\":\"sample2\",\"property3\":\"sample3\" }"));
103+
List<String> values = (List<String>) validationResult.getCollectorContext().get(SAMPLE_COLLECTOR);
104+
List<String> values1 = (List<String>) validationResult.getCollectorContext().get(SAMPLE_COLLECTOR_OTHER);
105+
Assert.assertEquals(values.size(), 1);
106+
Assert.assertEquals(values1.size(), 3);
107+
}
108+
95109
@SuppressWarnings("unchecked")
96-
@Test
97-
public void testCollectorWithFormat() throws JsonMappingException, JsonProcessingException {
98-
ObjectMapper objectMapper = new ObjectMapper();
99-
ValidationResult validationResult = jsonSchemaForCombine.validateAndCollect(objectMapper.readTree("{\"property1\":\"sample1\",\"property2\":\"sample2\",\"property3\":\"sample3\" }"));
100-
List<String> values = (List<String>) validationResult.getCollectorContext().get(SAMPLE_COLLECTOR);
101-
Assert.assertEquals(values.size(), 4);
102-
}
110+
@Test
111+
public void testCollectorGetAll() throws JsonMappingException, JsonProcessingException {
112+
ObjectMapper objectMapper = new ObjectMapper();
113+
ValidationResult validationResult = jsonSchemaForCombine.validateAndCollect(objectMapper
114+
.readTree("{\"property1\":\"sample1\",\"property2\":\"sample2\",\"property3\":\"sample3\" }"));
115+
Map<String, Object> map = validationResult.getCollectorContext().getAll();
116+
Iterator<Object> collectionIterator = map.values().iterator();
117+
Assert.assertEquals(((List<String>) collectionIterator.next()).size(), 1);
118+
Assert.assertEquals(((List<String>) collectionIterator.next()).size(), 3);
119+
}
103120

104121
private JsonMetaSchema getJsonMetaSchema(String uri) throws Exception {
105122
JsonMetaSchema jsonMetaSchema = JsonMetaSchema.builder(uri, JsonMetaSchema.getV201909())
@@ -340,10 +357,10 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
340357
// Get an instance of collector context.
341358
CollectorContext collectorContext = CollectorContext.getInstance();
342359
// If collector type is not added to context add one.
343-
if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
344-
collectorContext.add(SAMPLE_COLLECTOR, new ArrayList<String>());
360+
if (collectorContext.get(SAMPLE_COLLECTOR_OTHER) == null) {
361+
collectorContext.add(SAMPLE_COLLECTOR_OTHER, new ArrayList<String>());
345362
}
346-
List<String> returnList = (List<String>) collectorContext.get(SAMPLE_COLLECTOR);
363+
List<String> returnList = (List<String>) collectorContext.get(SAMPLE_COLLECTOR_OTHER);
347364
returnList.add(node.textValue());
348365
return new TreeSet<ValidationMessage>();
349366
}

0 commit comments

Comments
 (0)