diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/AbstractJsonSchemaElement.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/AbstractJsonSchemaElement.java index e1b272d..55b6375 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/AbstractJsonSchemaElement.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/AbstractJsonSchemaElement.java @@ -84,6 +84,7 @@ public String getJsonPointer() { public AbstractJsonSchemaElement getParent() { return parent; } + /** * This is a marker whether this element is in the dynamic scope and @@ -127,33 +128,22 @@ public Object clone() throws CloneNotSupportedException { } /** - * Predicate to clone provided element. - * Method just omits possible (impossible) CloneNotSupportedException. + * Predicate to create a clone of this element linked another parent. * - * @param returned type inferred from caller - * @param element the element to be cloned + * @param parent - the parent to be assigned to the cloned element * - * @return the clone of provided element + * @return the clone of this element */ - protected T clone(T element) { - if (element != null) { + protected AbstractJsonSchemaElement relink(AbstractJsonSchemaElement parent) { + if (this.parent != parent) { try { - return (T)element.clone(); - } catch (CloneNotSupportedException ex) {} + AbstractJsonSchemaElement e = (AbstractJsonSchemaElement)this.clone(); + e.parent = parent; + return e; + } catch (CloneNotSupportedException ex) { + return null; + } } - return null; - } - - /** - * Predicate used in streams to replace parent for this element - * - * @param parent new parent for this element - * - * @return affected element (this) - */ - protected AbstractJsonSchemaElement setParent(AbstractJsonSchemaElement parent) { - this.parent = parent; return this; } - } diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/BooleanJsonSchemaImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/BooleanJsonSchemaImpl.java index a905adc..74c08bf 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/BooleanJsonSchemaImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/BooleanJsonSchemaImpl.java @@ -57,7 +57,7 @@ public BooleanJsonSchemaImpl(AbstractJsonSchemaElement parent, @Override public Stream getChildren() { - return Stream.of(); + return Stream.empty(); } @Override diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonArraySchemaImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonArraySchemaImpl.java index 48c4327..75ab910 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonArraySchemaImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonArraySchemaImpl.java @@ -84,8 +84,7 @@ public Stream getChildren() { Optional.ofNullable(items).map(Collection::stream).orElseGet(Stream::empty), Stream.of(additionalItemsSchema, unevaluatedItemsSchema, contains) .filter(Objects::nonNull)) - .map(this::clone) - .map(c -> c.setParent(this)); + .map(c -> c.relink(this)); return Stream.concat( super.getChildren(), diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonDependentPropertiesImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonDependentPropertiesImpl.java index 03862b3..6a04a84 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonDependentPropertiesImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonDependentPropertiesImpl.java @@ -31,6 +31,7 @@ import es.elixir.bsc.json.schema.ParsingError; import es.elixir.bsc.json.schema.ParsingMessage; import es.elixir.bsc.json.schema.model.JsonDependentProperties; +import es.elixir.bsc.json.schema.model.JsonSchemaElement; import es.elixir.bsc.json.schema.model.StringArray; import javax.json.JsonObject; import javax.json.JsonValue; @@ -55,8 +56,8 @@ public JsonDependentPropertiesImpl(AbstractJsonSchema parent, } @Override - public Stream getChildren() { - return Stream.empty(); // TODO + public Stream getChildren() { + return Stream.empty(); } @Override diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonDynamicReferenceImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonDynamicReferenceImpl.java index a4f939d..109740b 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonDynamicReferenceImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonDynamicReferenceImpl.java @@ -102,7 +102,7 @@ private AbstractJsonSchemaElement getSchema(AbstractJsonSchemaElement e, URI uri if (value instanceof JsonObject jsubschema) { final String anchor = jsubschema.getString(DYNAMIC_ANCHOR, null); if (fragment.equals(anchor)) { - return parser.parse(l, getParent(), e.getJsonPointer(), jsubschema, null); + return parser.parse(l, this, e.getJsonPointer(), jsubschema, null); } } return null; diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonMultitypeSchemaWrapper.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonMultitypeSchemaWrapper.java index 60463b0..16f7ac1 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonMultitypeSchemaWrapper.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonMultitypeSchemaWrapper.java @@ -68,8 +68,7 @@ public Stream getChildren() { return StreamSupport.stream( Spliterators.spliteratorUnknownSize(iterator(), Spliterator.ORDERED),false) .filter(s -> s instanceof JsonObjectSchema || s instanceof JsonArraySchema) - .map(this::clone) - .map(c -> c.setParent(this)) + .map(c -> c.relink(this)) .flatMap(JsonSchemaElement::getChildren); } diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonNotImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonNotImpl.java index 3a78e1e..8685bae 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonNotImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonNotImpl.java @@ -53,7 +53,7 @@ public JsonNotImpl(AbstractJsonSchema parent, @Override public Stream getChildren() { - return schema.clone(schema).setParent(this).getChildren(); + return schema.relink(this).getChildren(); } @Override diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonNumberSchemaImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonNumberSchemaImpl.java index 7af4152..d780f13 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonNumberSchemaImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonNumberSchemaImpl.java @@ -92,7 +92,7 @@ public boolean validate(String jsonPointer, JsonValue value, JsonValue parent, return nerrors == errors.size(); } - + private void validate(String jsonPointer, BigDecimal dec, List errors) { if (minimum != null) { diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonObjectSchemaImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonObjectSchemaImpl.java index cafc2e1..964dee7 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonObjectSchemaImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonObjectSchemaImpl.java @@ -86,8 +86,8 @@ public Stream getChildren() { final Stream children = Stream.of( properties, propertyNames, patternProperties, unevaluatedPropertiesSchema, dependentSchemas) - .filter(Objects::nonNull).map(this::clone) - .map(c -> c.setParent(this)); + .filter(Objects::nonNull) + .map(c -> c.relink(this)); return Stream.concat( super.getChildren(), diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonPropertiesImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonPropertiesImpl.java index a498406..5837445 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonPropertiesImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonPropertiesImpl.java @@ -29,6 +29,7 @@ import es.elixir.bsc.json.schema.JsonSchemaLocator; import es.elixir.bsc.json.schema.impl.JsonSubschemaParser; import es.elixir.bsc.json.schema.model.JsonProperties; +import es.elixir.bsc.json.schema.model.JsonSchemaElement; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; @@ -55,7 +56,7 @@ public JsonPropertiesImpl(AbstractJsonSchema parent, public Stream getChildren() { // clone properties and set their parent to 'this' final Stream children = - properties.values().stream().map(this::clone).map(c -> c.setParent(this)); + properties.values().stream().map(c -> c.relink(this)); return children.flatMap(p -> Stream.concat(Stream.of(p), p.getChildren())); } diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonRecursiveReferenceImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonRecursiveReferenceImpl.java index 6f0160d..239919b 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonRecursiveReferenceImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonRecursiveReferenceImpl.java @@ -48,7 +48,7 @@ public class JsonRecursiveReferenceImpl extends AbstractJsonReferenceImpl public JsonRecursiveReferenceImpl(AbstractJsonSchemaElement parent, JsonSchemaLocator scope, JsonSchemaLocator locator, String jsonPointer) { super(parent, scope, locator, jsonPointer); - + AbstractJsonSchemaElement e = this; do { e.setDynamicScope(true); diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonReferenceImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonReferenceImpl.java index d576c73..018a095 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonReferenceImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/JsonReferenceImpl.java @@ -55,18 +55,18 @@ public Stream getChildren() { while (s != null) { if (ref_locator.uri.equals(s.getId()) && ref_pointer.equals(s.getJsonPointer())) { - return Stream.empty(); // cyclic ref + return Stream.of(); // cyclic ref } s = s.getParent(); } try { schema = getSchema(); } catch(JsonSchemaException ex) { - return Stream.empty(); // unresolvable ref + return Stream.of(); // unresolvable ref } } - return schema.clone(schema).setParent(this).getChildren(); + return schema.relink(this).getChildren(); } @Override @@ -79,7 +79,7 @@ public AbstractJsonSchemaElement getSchema() throws JsonSchemaException { new ParsingError(ParsingMessage.UNRESOLVABLE_REFERENCE, ref)); } - schema = parser.parse(ref_locator, getParent(), ref_pointer, jsubschema, null); + schema = parser.parse(ref_locator, this, ref_pointer, jsubschema, null); } catch(IOException | JsonException | IllegalArgumentException ex) { throw new JsonSchemaException( new ParsingError(ParsingMessage.INVALID_REFERENCE, ref)); diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/PrimitiveSchemaImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/PrimitiveSchemaImpl.java index 124f73b..8a03669 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/PrimitiveSchemaImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/PrimitiveSchemaImpl.java @@ -93,8 +93,7 @@ public Stream getChildren() { final Stream children = Stream.of(allOf, anyOf, oneOf, not, _if, _then, _else, ref) .filter(Objects::nonNull) - .map(this::clone) - .map(c -> c.setParent(this)); + .map(c -> c.relink(this)); return children.flatMap(AbstractJsonSchemaElement::getChildren); } diff --git a/src/main/java/es/elixir/bsc/json/schema/model/impl/SchemaArrayImpl.java b/src/main/java/es/elixir/bsc/json/schema/model/impl/SchemaArrayImpl.java index 1dc467c..4c0d178 100644 --- a/src/main/java/es/elixir/bsc/json/schema/model/impl/SchemaArrayImpl.java +++ b/src/main/java/es/elixir/bsc/json/schema/model/impl/SchemaArrayImpl.java @@ -59,7 +59,7 @@ public SchemaArrayImpl(AbstractJsonSchemaElement parent, public Stream getChildren() { // clone array schemas and set their parent to 'this' final Stream children = - schemas.stream().map(this::clone).map(c -> c.setParent(this)); + schemas.stream().map(c -> c.relink(this)); return children.flatMap(e -> Stream.concat(Stream.of(e), e.getChildren())); }