Skip to content

Set initial array list size #1062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public AdditionalPropertiesValidator(SchemaLocation schemaLocation, JsonNodePath

JsonNode patternPropertiesNode = parentSchema.getSchemaNode().get(PatternPropertiesValidator.PROPERTY);
if (patternPropertiesNode != null) {
this.patternProperties = new ArrayList<>();
this.patternProperties = new ArrayList<>(patternPropertiesNode.size());
for (Iterator<String> it = patternPropertiesNode.fieldNames(); it.hasNext(); ) {
patternProperties.add(RegularExpression.compile(it.next(), validationContext));
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/networknt/schema/AllOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class AllOfValidator extends BaseJsonValidator {
private static final Logger logger = LoggerFactory.getLogger(AllOfValidator.class);

private final List<JsonSchema> schemas = new ArrayList<>();
private final List<JsonSchema> schemas;

public AllOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext);
Expand All @@ -44,6 +44,7 @@ public AllOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath
.build());
}
int size = schemaNode.size();
this.schemas = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
this.schemas.add(validationContext.newSchema(schemaLocation.append(i), evaluationPath.append(i),
schemaNode.get(i), parentSchema));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/networknt/schema/AnyOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class AnyOfValidator extends BaseJsonValidator {
private static final Logger logger = LoggerFactory.getLogger(AnyOfValidator.class);
private static final String DISCRIMINATOR_REMARK = "and the discriminator-selected candidate schema didn't pass validation";

private final List<JsonSchema> schemas = new ArrayList<>();
private final List<JsonSchema> schemas;

private Boolean canShortCircuit = null;

Expand All @@ -46,6 +46,7 @@ public AnyOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath
.build());
}
int size = schemaNode.size();
this.schemas = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
this.schemas.add(validationContext.newSchema(schemaLocation.append(i), evaluationPath.append(i),
schemaNode.get(i), parentSchema));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/networknt/schema/ItemsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ public ItemsValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath

Boolean additionalItems = null;

this.tupleSchema = new ArrayList<>();
JsonSchema foundSchema = null;
JsonSchema foundAdditionalSchema = null;
JsonNode additionalItemsSchemaNode = null;

if (schemaNode.isObject() || schemaNode.isBoolean()) {
foundSchema = validationContext.newSchema(schemaLocation, evaluationPath, schemaNode, parentSchema);
this.tupleSchema = Collections.emptyList();
} else {
int i = 0;
this.tupleSchema = new ArrayList<>(schemaNode.size());
for (JsonNode s : schemaNode) {
this.tupleSchema.add(validationContext.newSchema(schemaLocation.append(i), evaluationPath.append(i),
s, parentSchema));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/networknt/schema/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,9 @@ private JsonNode handleNullNode(String ref, JsonSchema schema) {
* Please note that the key in {@link #validators} map is the evaluation path.
*/
private List<JsonValidator> read(JsonNode schemaNode) {
List<JsonValidator> validators = new ArrayList<>();
List<JsonValidator> validators;
if (schemaNode.isBoolean()) {
validators = new ArrayList<>(1);
if (schemaNode.booleanValue()) {
JsonNodePath path = getEvaluationPath().append("true");
JsonValidator validator = this.validationContext.newValidator(getSchemaLocation().append("true"), path,
Expand All @@ -437,6 +438,7 @@ private List<JsonValidator> read(JsonNode schemaNode) {
JsonValidator refValidator = null;

Iterator<Entry<String, JsonNode>> iterator = schemaNode.fields();
validators = new ArrayList<>(schemaNode.size());
while (iterator.hasNext()) {
Entry<String, JsonNode> entry = iterator.next();
String pname = entry.getKey();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/networknt/schema/NotAllowedValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@
public class NotAllowedValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(NotAllowedValidator.class);

private final List<String> fieldNames = new ArrayList<>();
private final List<String> fieldNames;

public NotAllowedValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.NOT_ALLOWED, validationContext);
if (schemaNode.isArray()) {
int size = schemaNode.size();
this.fieldNames = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
fieldNames.add(schemaNode.get(i).asText());
}
} else {
this.fieldNames = Collections.emptyList();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/networknt/schema/OneOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public class OneOfValidator extends BaseJsonValidator {
private static final Logger logger = LoggerFactory.getLogger(OneOfValidator.class);

private final List<JsonSchema> schemas = new ArrayList<>();
private final List<JsonSchema> schemas;

private Boolean canShortCircuit = null;

Expand All @@ -45,6 +45,7 @@ public OneOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath
.build());
}
int size = schemaNode.size();
this.schemas = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
JsonNode childNode = schemaNode.get(i);
this.schemas.add(validationContext.newSchema( schemaLocation.append(i), evaluationPath.append(i), childNode, parentSchema));
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/networknt/schema/PrefixItemsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ public class PrefixItemsValidator extends BaseJsonValidator {
public PrefixItemsValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.PREFIX_ITEMS, validationContext);

this.tupleSchema = new ArrayList<>();

if (schemaNode instanceof ArrayNode && 0 < schemaNode.size()) {
int i = 0;
this.tupleSchema = new ArrayList<>(schemaNode.size());
for (JsonNode s : schemaNode) {
this.tupleSchema.add(validationContext.newSchema(schemaLocation.append(i), evaluationPath.append(i), s,
parentSchema));
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/networknt/schema/RequiredValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
public class RequiredValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(RequiredValidator.class);

private final List<String> fieldNames = new ArrayList<>();
private final List<String> fieldNames;

public RequiredValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.REQUIRED, validationContext);
if (schemaNode.isArray()) {
this.fieldNames = new ArrayList<>(schemaNode.size());
for (JsonNode fieldNme : schemaNode) {
fieldNames.add(fieldNme.asText());
}
} else {
this.fieldNames = Collections.emptyList();
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/networknt/schema/UnionTypeValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class UnionTypeValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(UnionTypeValidator.class);

private final List<JsonValidator> schemas = new ArrayList<>();
private final List<JsonValidator> schemas;
private final String error;

public UnionTypeValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
Expand All @@ -41,21 +41,24 @@ public UnionTypeValidator(SchemaLocation schemaLocation, JsonNodePath evaluation
String sep = "";
errorBuilder.append('[');

if (!schemaNode.isArray())
if (!schemaNode.isArray()) {
throw new JsonSchemaException("Expected array for type property on Union Type Definition.");
}

int i = 0;
this.schemas = new ArrayList<>(schemaNode.size());
for (JsonNode n : schemaNode) {
JsonType t = TypeFactory.getSchemaNodeType(n);
errorBuilder.append(sep).append(t);
sep = ", ";

if (n.isObject())
if (n.isObject()) {
schemas.add(validationContext.newSchema(schemaLocation.append(ValidatorTypeCode.TYPE.getValue()),
evaluationPath.append(ValidatorTypeCode.TRUE.getValue()), n, parentSchema));
else
schemas.add(new TypeValidator(schemaLocation.append(i), evaluationPath.append(i), n, parentSchema, validationContext));

} else {
schemas.add(new TypeValidator(schemaLocation.append(i), evaluationPath.append(i), n, parentSchema,
validationContext));
}
i++;
}

Expand Down