Skip to content

Commit

Permalink
switch HashMap for LinkedHashMap to perserve order of things everit-o…
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshMcCullough committed Aug 18, 2022
1 parent 440c9ef commit 7a11607
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 103 deletions.
23 changes: 12 additions & 11 deletions core/src/main/java/org/everit/json/schema/ObjectSchema.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package org.everit.json.schema;

import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toMap;
import static org.everit.json.schema.JSONPointer.unescape;
import org.everit.json.schema.regexp.JavaUtilRegexpFactory;
import org.everit.json.schema.regexp.Regexp;
import org.everit.json.schema.regexp.RegexpFactory;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;

import org.everit.json.schema.regexp.JavaUtilRegexpFactory;
import org.everit.json.schema.regexp.Regexp;
import org.everit.json.schema.regexp.RegexpFactory;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toMap;
import static org.everit.json.schema.JSONPointer.unescape;

/**
* Object schema validator.
Expand All @@ -35,11 +36,11 @@ private static final Regexp toRegexp(String pattern) {
return DEFAULT_REGEXP_FACTORY.createHandler(pattern);
}

private final Map<Regexp, Schema> patternProperties = new HashMap<>();
private final Map<Regexp, Schema> patternProperties = new LinkedHashMap<>();

private boolean requiresObject = true;

private final Map<String, Schema> propertySchemas = new HashMap<>();
private final Map<String, Schema> propertySchemas = new LinkedHashMap<>();

private boolean additionalProperties = true;

Expand All @@ -51,9 +52,9 @@ private static final Regexp toRegexp(String pattern) {

private Integer maxProperties;

private final Map<String, Set<String>> propertyDependencies = new HashMap<>();
private final Map<String, Set<String>> propertyDependencies = new LinkedHashMap<>();

private final Map<String, Schema> schemaDependencies = new HashMap<>();
private final Map<String, Schema> schemaDependencies = new LinkedHashMap<>();

private Schema propertyNameSchema;

Expand Down Expand Up @@ -167,7 +168,7 @@ public static Builder builder() {
}

private static <K, V> Map<K, V> copyMap(Map<K, V> original) {
return Collections.unmodifiableMap(new HashMap<>(original));
return Collections.unmodifiableMap(new LinkedHashMap<>(original));
}

private final Map<String, Schema> propertySchemas;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.everit.json.schema;

import static java.util.Objects.requireNonNull;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import static java.util.Objects.requireNonNull;

/**
* This class is used by {@link org.everit.json.schema.loader.SchemaLoader} to resolve JSON pointers
* during the construction of the schema. This class has been made mutable to permit the loading of
Expand Down Expand Up @@ -44,7 +44,7 @@ public Builder refValue(String refValue) {

@Override public ReferenceSchema.Builder unprocessedProperties(Map<String, Object> unprocessedProperties) {
if (retval != null) {
retval.unprocessedProperties = new HashMap<>(unprocessedProperties);
retval.unprocessedProperties = new LinkedHashMap<>(unprocessedProperties);
}
super.unprocessedProperties(unprocessedProperties);
return this;
Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/org/everit/json/schema/Schema.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.everit.json.schema;

import static java.util.Collections.unmodifiableMap;
import org.everit.json.schema.internal.JSONPrinter;
import org.json.JSONWriter;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import org.everit.json.schema.internal.JSONPrinter;
import org.json.JSONWriter;
import static java.util.Collections.unmodifiableMap;

/**
* Superclass of all other schema validator classes of this package.
Expand Down Expand Up @@ -41,7 +42,7 @@ public abstract static class Builder<S extends Schema> {

private Boolean writeOnly = null;

public Map<String, Object> unprocessedProperties = new HashMap<>(0);
public Map<String, Object> unprocessedProperties = new LinkedHashMap<>(0);

public Builder<S> title(String title) {
this.title = title;
Expand Down Expand Up @@ -137,7 +138,7 @@ protected Schema(Builder<?> builder) {
this.nullable = builder.nullable;
this.readOnly = builder.readOnly;
this.writeOnly = builder.writeOnly;
this.unprocessedProperties = new HashMap<>(builder.unprocessedProperties);
this.unprocessedProperties = new LinkedHashMap<>(builder.unprocessedProperties);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/org/everit/json/schema/loader/JsonObject.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.everit.json.schema.loader;

import static java.lang.String.format;
import static java.util.Collections.unmodifiableMap;
import static java.util.Collections.unmodifiableSet;
import org.everit.json.schema.SchemaException;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

import org.everit.json.schema.SchemaException;
import static java.lang.String.format;
import static java.util.Collections.unmodifiableMap;
import static java.util.Collections.unmodifiableSet;

/**
* @author erosb
Expand Down Expand Up @@ -97,7 +97,7 @@ private void iterateOnEntry(Map.Entry<String, Object> entry, JsonObjectIterator
}

@Override protected Object unwrap() {
return new HashMap<>(storage);
return new LinkedHashMap<>(storage);
}

Map<String, Object> toMap() {
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/java/org/everit/json/schema/loader/JsonValue.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package org.everit.json.schema.loader;

import static org.everit.json.schema.loader.OrgJsonUtil.toList;
import static org.everit.json.schema.loader.OrgJsonUtil.toMap;
import static org.everit.json.schema.loader.SpecificationVersion.DRAFT_4;
import org.everit.json.schema.SchemaException;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

import org.everit.json.schema.SchemaException;
import org.json.JSONArray;
import org.json.JSONObject;
import static org.everit.json.schema.loader.OrgJsonUtil.toList;
import static org.everit.json.schema.loader.OrgJsonUtil.toMap;
import static org.everit.json.schema.loader.SpecificationVersion.DRAFT_4;

/**
* @author erosb
Expand All @@ -24,7 +24,7 @@ class JsonValue {

class Multiplexer<R> {

protected Map<Class<?>, Function<?, R>> actions = new HashMap<>();
protected Map<Class<?>, Function<?, R>> actions = new LinkedHashMap<>();

Multiplexer(Class<?> expectedType, Function<?, R> mapper) {
actions.put(expectedType, mapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -19,7 +20,7 @@ public class OrgJsonUtil {
* Used as a replacement of {@code JSONObject#toMap()} (which doesn't exist in the android version of org.json).
*/
public static Map<String, Object> toMap(JSONObject obj) {
Map<String, Object> rval = new HashMap<>(obj.length());
Map<String, Object> rval = new LinkedHashMap<>(obj.length());
Iterator<String> keyIt = obj.keys();
while (keyIt.hasNext()) {
String key = keyIt.next();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.everit.json.schema.loader;

import static java.lang.String.format;
import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

import static java.lang.String.format;
import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;

class ProjectedJsonObject extends JsonObject {

private final JsonObject original;
Expand Down Expand Up @@ -89,13 +89,13 @@ private void throwExceptionIfNotVisible(String key) {
}

@Override protected Object unwrap() {
Map<String, Object> storage = new HashMap<>(original.storage);
Map<String, Object> storage = new LinkedHashMap<>(original.storage);
removeHiddenKeysFrom(storage);
return storage;
}

@Override Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>(original.toMap());
Map<String, Object> map = new LinkedHashMap<>(original.toMap());
removeHiddenKeysFrom(map);
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -61,9 +62,9 @@ static Map<String, Object> extend(Map<String, Object> additional, Map<String, Ob
if (original.keySet().isEmpty()) {
return additional;
}
Map<String, Object> rawObj = new HashMap<>();
original.forEach(rawObj::put);
additional.forEach(rawObj::put);
Map<String, Object> rawObj = new LinkedHashMap<>();
rawObj.putAll(original);
rawObj.putAll(additional);
return rawObj;
}

Expand Down Expand Up @@ -111,7 +112,7 @@ private Map<String, Object> doExtend(Map<String, Object> additional, Map<String,
}

Map<String, Object> withoutRef(JsonObject original) {
Map<String, Object> rawObj = new HashMap<>();
Map<String, Object> rawObj = new LinkedHashMap<>();
original.keySet().stream()
.filter(name -> !"$ref".equals(name))
.forEach(name -> rawObj.put(name, original.get(name)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.everit.json.schema;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import java.util.LinkedHashMap;
import java.util.Map;

public class DynamicReferenceSchemaExample {

public static void main(String[] args) {
Expand All @@ -14,7 +14,7 @@ public static void main(String[] args) {
.build();
rootSchema.addPropertySchema("myProperty", referenceSchema);

Map<String, Object> unprocessed = new HashMap<>();
Map<String, Object> unprocessed = new LinkedHashMap<>();
JSONObject defs = new JSONObject();
StringSchema referredSchema = StringSchema.builder()
.minLength(2).maxLength(5)
Expand Down
36 changes: 18 additions & 18 deletions core/src/test/java/org/everit/json/schema/ObjectSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@
*/
package org.everit.json.schema;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toMap;
import static org.everit.json.schema.JSONMatcher.sameJsonAs;
import static org.everit.json.schema.TestSupport.buildWithLocation;
import static org.everit.json.schema.TestSupport.loadAsV6;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import com.google.re2j.Pattern;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONPointer;
import org.junit.jupiter.api.Test;

import com.google.re2j.Pattern;
import java.util.AbstractMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toMap;
import static org.everit.json.schema.JSONMatcher.sameJsonAs;
import static org.everit.json.schema.TestSupport.buildWithLocation;
import static org.everit.json.schema.TestSupport.loadAsV6;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

public class ObjectSchemaTest {

Expand Down Expand Up @@ -259,7 +259,7 @@ public void patternPropertyTranslation() {
ObjectSchema subject = ObjectSchema.builder()
.patternProperty(java.util.regex.Pattern.compile("b_.*"), BooleanSchema.INSTANCE)
.build();
Map<java.util.regex.Pattern, Schema> expected = new HashMap<>();
Map<java.util.regex.Pattern, Schema> expected = new LinkedHashMap<>();
expected.put(java.util.regex.Pattern.compile("b_.*"), BooleanSchema.INSTANCE);
assertEquals(toStringToSchemaMap(expected), toStringToSchemaMap(subject.getPatternProperties()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package org.everit.json.schema.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.StringWriter;
import java.util.HashMap;

import org.everit.json.schema.NullSchema;
import org.everit.json.schema.Schema;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class JSONPrinterTest {

private StringWriter buffer;
Expand Down Expand Up @@ -127,10 +128,10 @@ public void arraySupport() {

@Test
public void printSchemaMap() {
HashMap<Number, Schema> input = new HashMap<Number, Schema>();
Map<Number, Schema> input = new LinkedHashMap<>();
input.put(2, NullSchema.INSTANCE);
subject().printSchemaMap(input);
assertEquals("{\"2\":" + NullSchema.INSTANCE.toString() + "}", buffer.toString());
assertEquals("{\"2\":" + NullSchema.INSTANCE + "}", buffer.toString());
}

}
Loading

0 comments on commit 7a11607

Please sign in to comment.