Skip to content

Commit 4e5a808

Browse files
authored
Refactor to make more fields final (#1060)
1 parent 0ceb03e commit 4e5a808

20 files changed

+316
-193
lines changed

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

+39-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.fasterxml.jackson.databind.node.ObjectNode;
2121
import com.networknt.schema.annotation.JsonNodeAnnotation;
2222
import com.networknt.schema.i18n.DefaultMessageSource;
23+
import com.networknt.schema.i18n.MessageSource;
2324

2425
import org.slf4j.Logger;
2526

@@ -29,12 +30,15 @@
2930
import java.util.Set;
3031
import java.util.function.Consumer;
3132

33+
/**
34+
* Base {@link JsonValidator}.
35+
*/
3236
public abstract class BaseJsonValidator extends ValidationMessageHandler implements JsonValidator {
3337
protected final boolean suppressSubSchemaRetrieval;
3438

3539
protected final JsonNode schemaNode;
3640

37-
protected ValidationContext validationContext;
41+
protected final ValidationContext validationContext;
3842

3943
public BaseJsonValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
4044
JsonSchema parentSchema, ValidatorTypeCode validatorType, ValidationContext validationContext) {
@@ -60,15 +64,41 @@ public BaseJsonValidator(SchemaLocation schemaLocation, JsonNodePath evaluationP
6064
}
6165

6266
/**
63-
* Copy constructor.
64-
*
65-
* @param copy to copy from
67+
* Constructor to create a copy using fields.
68+
*
69+
* @param suppressSubSchemaRetrieval to suppress sub schema retrieval
70+
* @param schemaNode the schema node
71+
* @param validationContext the validation context
72+
* @param errorMessageType the error message type
73+
* @param customErrorMessagesEnabled whether custom error msessages are enabled
74+
* @param messageSource the message source
75+
* @param keyword the keyword
76+
* @param parentSchema the parent schema
77+
* @param schemaLocation the schema location
78+
* @param evaluationPath the evaluation path
79+
* @param evaluationParentSchema the evaluation parent schema
80+
* @param errorMessage the error message
6681
*/
67-
protected BaseJsonValidator(BaseJsonValidator copy) {
68-
super(copy);
69-
this.suppressSubSchemaRetrieval = copy.suppressSubSchemaRetrieval;
70-
this.schemaNode = copy.schemaNode;
71-
this.validationContext = copy.validationContext;
82+
protected BaseJsonValidator(
83+
/* Below from BaseJsonValidator */
84+
boolean suppressSubSchemaRetrieval,
85+
JsonNode schemaNode,
86+
ValidationContext validationContext,
87+
/* Below from ValidationMessageHandler */
88+
ErrorMessageType errorMessageType,
89+
boolean customErrorMessagesEnabled,
90+
MessageSource messageSource,
91+
Keyword keyword,
92+
JsonSchema parentSchema,
93+
SchemaLocation schemaLocation,
94+
JsonNodePath evaluationPath,
95+
JsonSchema evaluationParentSchema,
96+
Map<String, String> errorMessage) {
97+
super(errorMessageType, customErrorMessagesEnabled, messageSource, keyword,
98+
parentSchema, schemaLocation, evaluationPath, evaluationParentSchema, errorMessage);
99+
this.suppressSubSchemaRetrieval = suppressSubSchemaRetrieval;
100+
this.schemaNode = schemaNode;
101+
this.validationContext = validationContext;
72102
}
73103

74104
private static JsonSchema obtainSubSchemaNode(final JsonNode schemaNode, final ValidationContext validationContext) {

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

+21-2
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,35 @@ public class CollectorContext {
2828
/**
2929
* Map for holding the name and {@link Collector} or a simple Object.
3030
*/
31-
private Map<String, Object> collectorMap = new HashMap<>();
31+
private final Map<String, Object> collectorMap;
3232

3333
/**
3434
* Map for holding the name and {@link Collector} class collect method output.
3535
*/
36-
private Map<String, Object> collectorLoadMap = new HashMap<>();
36+
private final Map<String, Object> collectorLoadMap;
3737

38+
/**
39+
* Default constructor will use an unsynchronized HashMap to store data. This is
40+
* suitable if the collector context is not shared with multiple threads.
41+
*/
3842
public CollectorContext() {
43+
this(new HashMap<>(), new HashMap<>());
3944
}
4045

46+
/**
47+
* Constructor that creates the context using the specified instances to store
48+
* data.
49+
* <p>
50+
* If for instance the collector context needs to be shared with multiple
51+
* threads a ConcurrentHashMap can be used.
52+
*
53+
* @param collectorMap the collector map
54+
* @param collectorLoadMap the collector load map
55+
*/
56+
public CollectorContext(Map<String, Object> collectorMap, Map<String, Object> collectorLoadMap) {
57+
this.collectorMap = collectorMap;
58+
this.collectorLoadMap = collectorLoadMap;
59+
}
4160

4261
/**
4362
* Adds a collector with give name. Preserving this method for backward

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

+19-9
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public class ContainsValidator extends BaseJsonValidator {
4242
private final JsonSchema schema;
4343
private final boolean isMinV201909;
4444

45-
private Integer min = null;
46-
private Integer max = null;
47-
45+
private final Integer min;
46+
private final Integer max;
47+
4848
private Boolean hasUnevaluatedItemsValidator = null;
4949

5050
public ContainsValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
@@ -55,19 +55,29 @@ public ContainsValidator(SchemaLocation schemaLocation, JsonNodePath evaluationP
5555
// slightly.
5656
this.isMinV201909 = MinV201909.getVersions().contains(this.validationContext.getMetaSchema().getSpecification());
5757

58+
Integer currentMax = null;
59+
Integer currentMin = null;
5860
if (schemaNode.isObject() || schemaNode.isBoolean()) {
5961
this.schema = validationContext.newSchema(schemaLocation, evaluationPath, schemaNode, parentSchema);
6062
JsonNode parentSchemaNode = parentSchema.getSchemaNode();
61-
Optional.ofNullable(parentSchemaNode.get(ValidatorTypeCode.MAX_CONTAINS.getValue()))
62-
.filter(JsonNode::canConvertToExactIntegral)
63-
.ifPresent(node -> this.max = node.intValue());
63+
Optional<JsonNode> maxNode = Optional
64+
.ofNullable(parentSchemaNode.get(ValidatorTypeCode.MAX_CONTAINS.getValue()))
65+
.filter(JsonNode::canConvertToExactIntegral);
66+
if (maxNode.isPresent()) {
67+
currentMax = maxNode.get().intValue();
68+
}
6469

65-
Optional.ofNullable(parentSchemaNode.get(ValidatorTypeCode.MIN_CONTAINS.getValue()))
66-
.filter(JsonNode::canConvertToExactIntegral)
67-
.ifPresent(node -> this.min = node.intValue());
70+
Optional<JsonNode> minNode = Optional
71+
.ofNullable(parentSchemaNode.get(ValidatorTypeCode.MIN_CONTAINS.getValue()))
72+
.filter(JsonNode::canConvertToExactIntegral);
73+
if (minNode.isPresent()) {
74+
currentMin = minNode.get().intValue();
75+
}
6876
} else {
6977
this.schema = null;
7078
}
79+
this.max = currentMax;
80+
this.min = currentMin;
7181
}
7282

7383
@Override

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public class ContentEncodingValidator extends BaseJsonValidator {
3434
private static final Logger logger = LoggerFactory.getLogger(ContentEncodingValidator.class);
35-
private String contentEncoding;
35+
private final String contentEncoding;
3636

3737
/**
3838
* Constructor.

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

-43
This file was deleted.

0 commit comments

Comments
 (0)