Skip to content

Commit 49215e6

Browse files
committed
upgrade to 1.0.39 and update changelog
1 parent cad7141 commit 49215e6

File tree

4 files changed

+131
-124
lines changed

4 files changed

+131
-124
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
## 1.0.39 - 2020-04-28
13+
14+
### Added
15+
16+
### Changed
17+
18+
- fixes #289 Adding getAll method on CollectorContext class. Thanks @prashanthjos
19+
1220
## 1.0.38 - 2020-04-12
1321

1422
### Added

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<modelVersion>4.0.0</modelVersion>
2121
<groupId>com.networknt</groupId>
2222
<artifactId>json-schema-validator</artifactId>
23-
<version>1.0.38</version>
23+
<version>1.0.39</version>
2424
<packaging>bundle</packaging>
2525
<description>A json schema validator that supports draft v4, v6, v7 and v2019-09</description>
2626
<url>https://github.com/networknt/json-schema-validator</url>

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

+99-100
Original file line numberDiff line numberDiff line change
@@ -26,115 +26,114 @@
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+
public <E> void add(String name, Object object) {
68+
collectorMap.put(name, object);
69+
}
7170

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

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

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

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

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

140139
}

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

+23-23
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public class CollectorContextTest {
2929

3030
private static final String SAMPLE_COLLECTOR = "sampleCollectorType";
31-
31+
3232
private static final String SAMPLE_COLLECTOR_OTHER = "sampleCollectorOtherType";
3333

3434
private JsonSchema jsonSchema;
@@ -94,29 +94,29 @@ public void testCollectorContextWithMultiplThreads() throws Exception {
9494
Assert.assertEquals(contextValue3.get(0), "actual_value_added_to_context3");
9595
}
9696

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-
10997
@SuppressWarnings("unchecked")
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-
}
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+
109+
@SuppressWarnings("unchecked")
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+
}
120120

121121
private JsonMetaSchema getJsonMetaSchema(String uri) throws Exception {
122122
JsonMetaSchema jsonMetaSchema = JsonMetaSchema.builder(uri, JsonMetaSchema.getV201909())

0 commit comments

Comments
 (0)