Skip to content

Commit

Permalink
Refactor to make more fields final (#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay committed Jun 13, 2024
1 parent 0ceb03e commit 4e5a808
Show file tree
Hide file tree
Showing 20 changed files with 316 additions and 193 deletions.
48 changes: 39 additions & 9 deletions src/main/java/com/networknt/schema/BaseJsonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.networknt.schema.annotation.JsonNodeAnnotation;
import com.networknt.schema.i18n.DefaultMessageSource;
import com.networknt.schema.i18n.MessageSource;

import org.slf4j.Logger;

Expand All @@ -29,12 +30,15 @@
import java.util.Set;
import java.util.function.Consumer;

/**
* Base {@link JsonValidator}.
*/
public abstract class BaseJsonValidator extends ValidationMessageHandler implements JsonValidator {
protected final boolean suppressSubSchemaRetrieval;

protected final JsonNode schemaNode;

protected ValidationContext validationContext;
protected final ValidationContext validationContext;

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

/**
* Copy constructor.
*
* @param copy to copy from
* Constructor to create a copy using fields.
*
* @param suppressSubSchemaRetrieval to suppress sub schema retrieval
* @param schemaNode the schema node
* @param validationContext the validation context
* @param errorMessageType the error message type
* @param customErrorMessagesEnabled whether custom error msessages are enabled
* @param messageSource the message source
* @param keyword the keyword
* @param parentSchema the parent schema
* @param schemaLocation the schema location
* @param evaluationPath the evaluation path
* @param evaluationParentSchema the evaluation parent schema
* @param errorMessage the error message
*/
protected BaseJsonValidator(BaseJsonValidator copy) {
super(copy);
this.suppressSubSchemaRetrieval = copy.suppressSubSchemaRetrieval;
this.schemaNode = copy.schemaNode;
this.validationContext = copy.validationContext;
protected BaseJsonValidator(
/* Below from BaseJsonValidator */
boolean suppressSubSchemaRetrieval,
JsonNode schemaNode,
ValidationContext validationContext,
/* Below from ValidationMessageHandler */
ErrorMessageType errorMessageType,
boolean customErrorMessagesEnabled,
MessageSource messageSource,
Keyword keyword,
JsonSchema parentSchema,
SchemaLocation schemaLocation,
JsonNodePath evaluationPath,
JsonSchema evaluationParentSchema,
Map<String, String> errorMessage) {
super(errorMessageType, customErrorMessagesEnabled, messageSource, keyword,
parentSchema, schemaLocation, evaluationPath, evaluationParentSchema, errorMessage);
this.suppressSubSchemaRetrieval = suppressSubSchemaRetrieval;
this.schemaNode = schemaNode;
this.validationContext = validationContext;
}

private static JsonSchema obtainSubSchemaNode(final JsonNode schemaNode, final ValidationContext validationContext) {
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/networknt/schema/CollectorContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,35 @@ public class CollectorContext {
/**
* Map for holding the name and {@link Collector} or a simple Object.
*/
private Map<String, Object> collectorMap = new HashMap<>();
private final Map<String, Object> collectorMap;

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

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

/**
* Constructor that creates the context using the specified instances to store
* data.
* <p>
* If for instance the collector context needs to be shared with multiple
* threads a ConcurrentHashMap can be used.
*
* @param collectorMap the collector map
* @param collectorLoadMap the collector load map
*/
public CollectorContext(Map<String, Object> collectorMap, Map<String, Object> collectorLoadMap) {
this.collectorMap = collectorMap;
this.collectorLoadMap = collectorLoadMap;
}

/**
* Adds a collector with give name. Preserving this method for backward
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/com/networknt/schema/ContainsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public class ContainsValidator extends BaseJsonValidator {
private final JsonSchema schema;
private final boolean isMinV201909;

private Integer min = null;
private Integer max = null;
private final Integer min;
private final Integer max;

private Boolean hasUnevaluatedItemsValidator = null;

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

Integer currentMax = null;
Integer currentMin = null;
if (schemaNode.isObject() || schemaNode.isBoolean()) {
this.schema = validationContext.newSchema(schemaLocation, evaluationPath, schemaNode, parentSchema);
JsonNode parentSchemaNode = parentSchema.getSchemaNode();
Optional.ofNullable(parentSchemaNode.get(ValidatorTypeCode.MAX_CONTAINS.getValue()))
.filter(JsonNode::canConvertToExactIntegral)
.ifPresent(node -> this.max = node.intValue());
Optional<JsonNode> maxNode = Optional
.ofNullable(parentSchemaNode.get(ValidatorTypeCode.MAX_CONTAINS.getValue()))
.filter(JsonNode::canConvertToExactIntegral);
if (maxNode.isPresent()) {
currentMax = maxNode.get().intValue();
}

Optional.ofNullable(parentSchemaNode.get(ValidatorTypeCode.MIN_CONTAINS.getValue()))
.filter(JsonNode::canConvertToExactIntegral)
.ifPresent(node -> this.min = node.intValue());
Optional<JsonNode> minNode = Optional
.ofNullable(parentSchemaNode.get(ValidatorTypeCode.MIN_CONTAINS.getValue()))
.filter(JsonNode::canConvertToExactIntegral);
if (minNode.isPresent()) {
currentMin = minNode.get().intValue();
}
} else {
this.schema = null;
}
this.max = currentMax;
this.min = currentMin;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public class ContentEncodingValidator extends BaseJsonValidator {
private static final Logger logger = LoggerFactory.getLogger(ContentEncodingValidator.class);
private String contentEncoding;
private final String contentEncoding;

/**
* Constructor.
Expand Down
43 changes: 0 additions & 43 deletions src/main/java/com/networknt/schema/JsonNodeLocation.java

This file was deleted.

Loading

0 comments on commit 4e5a808

Please sign in to comment.