Skip to content

Commit 7c4944b

Browse files
authored
Clean up code (#1130)
* Simplify code, add final keyword, use diamond operator, correct English, use lambdas, remove .toString, use .isEmpty(), use .addAll, and more * Thanks to IntelliJ code analyses feature
1 parent d4d0bb3 commit 7c4944b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+146
-200
lines changed

src/main/java/com/networknt/org/apache/commons/validator/routines/DomainValidator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public enum ArrayType {
9696
* @since 1.7
9797
*/
9898
LOCAL_MINUS
99-
;
10099
}
101100

102101
private static class IDNBUGHOLDER {

src/main/java/com/networknt/org/apache/commons/validator/routines/EmailValidator.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,7 @@ public boolean isValid(final String email) {
186186
return false;
187187
}
188188

189-
if (!isValidDomain(emailMatcher.group(2))) {
190-
return false;
191-
}
192-
193-
return true;
189+
return isValidDomain(emailMatcher.group(2));
194190
}
195191

196192
/**

src/main/java/com/networknt/org/apache/commons/validator/routines/InetAddressValidator.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,6 @@ public boolean isValidInet6Address(String inet6Address) {
213213
}
214214
validOctets++;
215215
}
216-
if (validOctets > IPV6_MAX_HEX_GROUPS || validOctets < IPV6_MAX_HEX_GROUPS && !containsCompressedZeroes) {
217-
return false;
218-
}
219-
return true;
216+
return validOctets <= IPV6_MAX_HEX_GROUPS && (validOctets >= IPV6_MAX_HEX_GROUPS || containsCompressedZeroes);
220217
}
221218
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
4444
return Collections.emptySet();
4545
}
4646

47-
protected Object getAnnotationValue(JsonNode schemaNode) {
47+
private Object getAnnotationValue(JsonNode schemaNode) {
4848
if (schemaNode.isTextual()) {
4949
return schemaNode.textValue();
5050
} else if (schemaNode.isNumber()) {
@@ -62,7 +62,7 @@ public AnnotationKeyword(String keyword) {
6262

6363
@Override
6464
public JsonValidator newValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
65-
JsonSchema parentSchema, ValidationContext validationContext) throws JsonSchemaException, Exception {
65+
JsonSchema parentSchema, ValidationContext validationContext) {
6666
return new Validator(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, this);
6767
}
6868
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static void debug(Logger logger, ExecutionContext executionContext, JsonN
147147

148148
/**
149149
* Checks based on the current {@link DiscriminatorContext} whether the provided {@link JsonSchema} a match against
150-
* against the current discriminator.
150+
* the current discriminator.
151151
*
152152
* @param currentDiscriminatorContext the currently active {@link DiscriminatorContext}
153153
* @param discriminator the discriminator to use for the check

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ public interface Collector<E> {
3232
* at multiple touch points or accumulating data at same touch point.
3333
* @param object Object
3434
*/
35-
public void combine(Object object);
35+
void combine(Object object);
3636

3737
/**
3838
* Final method called by the framework that returns the actual collected data.
3939
* If the collector is not accumulating data or being used to collect data at
4040
* multiple touch points, only this method can be implemented.
4141
* @return E element
4242
*/
43-
public E collect();
43+
E collect();
4444

4545

4646
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.networknt.schema;
1717

18+
import java.util.Map;
1819
import java.util.Map.Entry;
1920

2021
import com.fasterxml.jackson.databind.JsonNode;
@@ -60,7 +61,7 @@ protected JsonMetaSchema.Builder loadMetaSchemaBuilder(String iri, JsonSchemaFac
6061
// Process vocabularies
6162
JsonNode vocabulary = schema.getSchemaNode().get("$vocabulary");
6263
if (vocabulary != null) {
63-
builder.vocabularies(vocabularies -> vocabularies.clear());
64+
builder.vocabularies(Map::clear);
6465
for (Entry<String, JsonNode> vocabs : vocabulary.properties()) {
6566
builder.vocabulary(vocabs.getKey(), vocabs.getValue().booleanValue());
6667
}
@@ -71,7 +72,7 @@ protected JsonMetaSchema.Builder loadMetaSchemaBuilder(String iri, JsonSchemaFac
7172
}
7273

7374
private static class Holder {
74-
private static DefaultJsonMetaSchemaFactory INSTANCE = new DefaultJsonMetaSchemaFactory();
75+
private static final DefaultJsonMetaSchemaFactory INSTANCE = new DefaultJsonMetaSchemaFactory();
7576
}
7677

7778
public static DefaultJsonMetaSchemaFactory getInstance() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
*/
2828
public class DependenciesValidator extends BaseJsonValidator implements JsonValidator {
2929
private static final Logger logger = LoggerFactory.getLogger(DependenciesValidator.class);
30-
private final Map<String, List<String>> propertyDeps = new HashMap<String, List<String>>();
31-
private final Map<String, JsonSchema> schemaDeps = new HashMap<String, JsonSchema>();
30+
private final Map<String, List<String>> propertyDeps = new HashMap<>();
31+
private final Map<String, JsonSchema> schemaDeps = new HashMap<>();
3232

3333
/**
3434
* Constructor.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
public class DependentRequired extends BaseJsonValidator implements JsonValidator {
2929
private static final Logger logger = LoggerFactory.getLogger(DependentRequired.class);
30-
private final Map<String, List<String>> propertyDependencies = new HashMap<String, List<String>>();
30+
private final Map<String, List<String>> propertyDependencies = new HashMap<>();
3131

3232
public DependentRequired(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3333

@@ -49,7 +49,7 @@ public DependentRequired(SchemaLocation schemaLocation, JsonNodePath evaluationP
4949
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
5050
debug(logger, executionContext, node, rootNode, instanceLocation);
5151

52-
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
52+
Set<ValidationMessage> errors = new LinkedHashSet<>();
5353

5454
for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) {
5555
String pname = it.next();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public JsonMetaSchema getMetaSchema(String iri, JsonSchemaFactory schemaFactory,
2828
}
2929

3030
private static class Holder {
31-
private static DisallowUnknownJsonMetaSchemaFactory INSTANCE = new DisallowUnknownJsonMetaSchemaFactory();
31+
private static final DisallowUnknownJsonMetaSchemaFactory INSTANCE = new DisallowUnknownJsonMetaSchemaFactory();
3232
}
3333

3434
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Keyword getKeyword(String value, ValidationContext validationContext) {
3333
}
3434

3535
private static class Holder {
36-
private static DisallowUnknownKeywordFactory INSTANCE = new DisallowUnknownKeywordFactory();
36+
private static final DisallowUnknownKeywordFactory INSTANCE = new DisallowUnknownKeywordFactory();
3737
}
3838

3939
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static String asText(JsonNode node) {
4848
public EnumValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
4949
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ENUM, validationContext);
5050
if (schemaNode != null && schemaNode.isArray()) {
51-
nodes = new HashSet<JsonNode>();
51+
nodes = new HashSet<>();
5252
StringBuilder sb = new StringBuilder();
5353

5454
sb.append('[');
@@ -144,7 +144,7 @@ protected ArrayNode processArrayNode(ArrayNode node) {
144144
if (!hasNumber(node)) {
145145
return node;
146146
}
147-
ArrayNode a = (ArrayNode) node.deepCopy();
147+
ArrayNode a = node.deepCopy();
148148
for (int x = 0; x < a.size(); x++) {
149149
JsonNode v = a.get(x);
150150
if (v.isNumber()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
125125
}
126126

127127
boolean hasAdditionalItem = false;
128-
SetView<ValidationMessage> errors = new SetView<ValidationMessage>();
128+
SetView<ValidationMessage> errors = new SetView<>();
129129
if (node.isArray()) {
130130
int i = 0;
131131
for (JsonNode n : node) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode n
146146
}
147147
}
148148
} else {
149-
// If the node is not an ArrayNode, eg. ObjectNode or null then the instance is null.
149+
// If the node is not an ArrayNode, e.g. ObjectNode or null then the instance is null.
150150
// The instance location starts at the end of the prefix count.
151151
walkSchema(executionContext, this.schema, null, rootNode, instanceLocation.append(this.prefixCount),
152152
shouldValidateSchema, validationMessages);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public static class Builder {
5656
private String iri;
5757
private String idKeyword = "$id";
5858
private VersionFlag specification = null;
59-
private Map<String, Keyword> keywords = new HashMap<>();
60-
private Map<String, Format> formats = new HashMap<>();
61-
private Map<String, Boolean> vocabularies = new HashMap<>();
59+
private final Map<String, Keyword> keywords = new HashMap<>();
60+
private final Map<String, Format> formats = new HashMap<>();
61+
private final Map<String, Boolean> vocabularies = new HashMap<>();
6262
private FormatKeywordFactory formatKeywordFactory = null;
6363
private VocabularyFactory vocabularyFactory = null;
6464
private KeywordFactory unknownKeywordFactory = null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public String getName(int index) {
120120
public Object getElement(int index) {
121121
if (index == -1) {
122122
if (this.pathSegmentIndex != -1) {
123-
return Integer.valueOf(this.pathSegmentIndex);
123+
return this.pathSegmentIndex;
124124
} else {
125125
return this.pathSegment;
126126
}

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public JsonSchema getRefSchema(JsonNodePath fragment) {
332332
} else {
333333
// Anchor
334334
String base = this.getSchemaLocation().getAbsoluteIri() != null ? this.schemaLocation.getAbsoluteIri().toString() : "";
335-
String anchor = base + "#" + fragment.toString();
335+
String anchor = base + "#" + fragment;
336336
JsonSchema result = this.validationContext.getSchemaResources().get(anchor);
337337
if (result == null) {
338338
result = this.validationContext.getDynamicAnchors().get(anchor);
@@ -468,11 +468,8 @@ public boolean isSchemaResourceRoot() {
468468
return true;
469469
}
470470
// The schema should not cross
471-
if (!Objects.equals(getSchemaLocation().getAbsoluteIri(),
472-
getParentSchema().getSchemaLocation().getAbsoluteIri())) {
473-
return true;
474-
}
475-
return false;
471+
return !Objects.equals(getSchemaLocation().getAbsoluteIri(),
472+
getParentSchema().getSchemaLocation().getAbsoluteIri());
476473
}
477474

478475
public String getId() {
@@ -581,7 +578,7 @@ private long activeDialect() {
581578
* A comparator that sorts validators, such that 'properties' comes before 'required',
582579
* so that we can apply default values before validating required.
583580
*/
584-
private static Comparator<JsonValidator> VALIDATOR_SORT = (lhs, rhs) -> {
581+
private static final Comparator<JsonValidator> VALIDATOR_SORT = (lhs, rhs) -> {
585582
String lhsName = lhs.getEvaluationPath().getName(-1);
586583
String rhsName = rhs.getEvaluationPath().getName(-1);
587584

@@ -771,9 +768,7 @@ public <T> T validate(JsonNode rootNode, OutputFormat<T> format, ExecutionContex
771768
* @return the result
772769
*/
773770
public <T> T validate(JsonNode rootNode, OutputFormat<T> format, Consumer<ExecutionContext> executionCustomizer) {
774-
return validate(createExecutionContext(), rootNode, format, (executionContext, validationContext) -> {
775-
executionCustomizer.accept(executionContext);
776-
});
771+
return validate(createExecutionContext(), rootNode, format, (executionContext, validationContext) -> executionCustomizer.accept(executionContext));
777772
}
778773

779774
/**
@@ -892,9 +887,7 @@ public <T> T validate(String input, InputFormat inputFormat, OutputFormat<T> for
892887
* @return the result
893888
*/
894889
public <T> T validate(String input, InputFormat inputFormat, OutputFormat<T> format, Consumer<ExecutionContext> executionCustomizer) {
895-
return validate(createExecutionContext(), deserialize(input, inputFormat), format, (executionContext, validationContext) -> {
896-
executionCustomizer.accept(executionContext);
897-
});
890+
return validate(createExecutionContext(), deserialize(input, inputFormat), format, (executionContext, validationContext) -> executionCustomizer.accept(executionContext));
898891
}
899892

900893
/**
@@ -1241,9 +1234,7 @@ public ValidationResult walkAtNode(ExecutionContext executionContext, JsonNode n
12411234
private <T> T walkAtNodeInternal(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
12421235
JsonNodePath instanceLocation, boolean validate, OutputFormat<T> format, Consumer<ExecutionContext> executionCustomizer) {
12431236
return walkAtNodeInternal(executionContext, node, rootNode, instanceLocation, validate, format,
1244-
(executeContext, validationContext) -> {
1245-
executionCustomizer.accept(executeContext);
1246-
});
1237+
(executeContext, validationContext) -> executionCustomizer.accept(executeContext));
12471238
}
12481239

12491240
private <T> T walkAtNodeInternal(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static class Builder {
5757
private ObjectMapper yamlMapper = null;
5858
private JsonNodeReader jsonNodeReader = null;
5959
private String defaultMetaSchemaIri;
60-
private final ConcurrentMap<String, JsonMetaSchema> metaSchemas = new ConcurrentHashMap<String, JsonMetaSchema>();
60+
private final ConcurrentMap<String, JsonMetaSchema> metaSchemas = new ConcurrentHashMap<>();
6161
private SchemaLoaders.Builder schemaLoadersBuilder = null;
6262
private SchemaMappers.Builder schemaMappersBuilder = null;
6363
private boolean enableSchemaCache = true;
@@ -83,7 +83,7 @@ public Builder jsonNodeReader(JsonNodeReader jsonNodeReader) {
8383
* <p>
8484
* If the object reader is set this will not be used.
8585
* <p>
86-
* This is deprecated use a object reader instead.
86+
* This is deprecated use an object reader instead.
8787
*
8888
* @param jsonMapper the json mapper
8989
* @return the builder
@@ -99,7 +99,7 @@ public Builder jsonMapper(final ObjectMapper jsonMapper) {
9999
* <p>
100100
* If the object reader is set this will not be used.
101101
* <p>
102-
* This is deprecated use a object reader instead.
102+
* This is deprecated use an object reader instead.
103103
*
104104
* @param yamlMapper the yaml mapper
105105
* @return the builder
@@ -439,7 +439,7 @@ private JsonMetaSchema getMetaSchema(final JsonNode schemaNode, SchemaValidators
439439
private JsonMetaSchema getMetaSchemaOrDefault(final JsonNode schemaNode, SchemaValidatorsConfig config) {
440440
final JsonNode iriNode = schemaNode.get("$schema");
441441
if (iriNode != null && !iriNode.isNull() && !iriNode.isTextual()) {
442-
throw new JsonSchemaException("Unknown MetaSchema: " + iriNode.toString());
442+
throw new JsonSchemaException("Unknown MetaSchema: " + iriNode);
443443
}
444444
final String iri = iriNode == null || iriNode.isNull() ? defaultMetaSchemaIri : iriNode.textValue();
445445
return getMetaSchema(iri, config);
@@ -669,7 +669,7 @@ protected SchemaValidatorsConfig createSchemaValidatorsConfig() {
669669
protected JsonSchema getMappedSchema(final SchemaLocation schemaUri, SchemaValidatorsConfig config) {
670670
try (InputStream inputStream = this.schemaLoader.getSchema(schemaUri.getAbsoluteIri()).getInputStream()) {
671671
if (inputStream == null) {
672-
throw new IOException("Cannot load schema at " + schemaUri.toString());
672+
throw new IOException("Cannot load schema at " + schemaUri);
673673
}
674674
final JsonNode schemaNode;
675675
if (isYaml(schemaUri)) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ public interface JsonSchemaIdValidator {
3636
boolean validate(String id, boolean rootSchema, SchemaLocation schemaLocation,
3737
SchemaLocation resolvedSchemaLocation, ValidationContext validationContext);
3838

39-
public static final JsonSchemaIdValidator DEFAULT = new DefaultJsonSchemaIdValidator();
39+
JsonSchemaIdValidator DEFAULT = new DefaultJsonSchemaIdValidator();
4040

4141
/**
4242
* Implementation of {@link JsonSchemaIdValidator}.
4343
* <p>
4444
* Note that this does not strictly follow the specification.
4545
* <p>
46-
* This allows an $id that isn't an absolute-IRI on the root schema but it must
46+
* This allows an $id that isn't an absolute-IRI on the root schema, but it must
4747
* resolve to an absolute-IRI given a base-IRI.
4848
* <p>
4949
* This also allows non-empty fragments.
5050
*/
51-
public static class DefaultJsonSchemaIdValidator implements JsonSchemaIdValidator {
51+
class DefaultJsonSchemaIdValidator implements JsonSchemaIdValidator {
5252
@Override
5353
public boolean validate(String id, boolean rootSchema, SchemaLocation schemaLocation,
5454
SchemaLocation resolvedSchemaLocation, ValidationContext validationContext) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public enum JsonType {
3939
*
4040
* @param typeStr the type value
4141
*/
42-
private JsonType(String typeStr) {
42+
JsonType(String typeStr) {
4343
this.type = typeStr;
4444
}
4545

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ default Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode
7575
*
7676
* @return the schema location
7777
*/
78-
public SchemaLocation getSchemaLocation();
78+
SchemaLocation getSchemaLocation();
7979

8080
/**
8181
* The evaluation path is the set of keys, starting from the schema root,
@@ -84,12 +84,12 @@ default Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode
8484
*
8585
* @return the evaluation path
8686
*/
87-
public JsonNodePath getEvaluationPath();
87+
JsonNodePath getEvaluationPath();
8888

8989
/**
9090
* The keyword of the validator.
9191
*
9292
* @return the keyword
9393
*/
94-
public String getKeyword();
94+
String getKeyword();
9595
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public NonValidationKeyword(String keyword) {
6262

6363
@Override
6464
public JsonValidator newValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
65-
JsonSchema parentSchema, ValidationContext validationContext) throws JsonSchemaException, Exception {
65+
JsonSchema parentSchema, ValidationContext validationContext) {
6666
return new Validator(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, this);
6767
}
6868
}

0 commit comments

Comments
 (0)