Skip to content

Commit

Permalink
replace clone().setParent() chain with relink()
Browse files Browse the repository at this point in the history
  • Loading branch information
redmitry committed Aug 29, 2024
1 parent 7c88d57 commit ce5c2fd
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <T> 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 extends AbstractJsonSchemaElement> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public BooleanJsonSchemaImpl(AbstractJsonSchemaElement parent,

@Override
public Stream<JsonSchemaElement> getChildren() {
return Stream.of();
return Stream.empty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public Stream<AbstractJsonSchemaElement> 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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,8 +56,8 @@ public JsonDependentPropertiesImpl(AbstractJsonSchema parent,
}

@Override
public Stream<AbstractJsonSchemaElement> getChildren() {
return Stream.empty(); // TODO
public <T extends JsonSchemaElement> Stream<T> getChildren() {
return Stream.empty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public Stream<AbstractJsonSchemaElement> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public JsonNotImpl(AbstractJsonSchema parent,

@Override
public Stream<AbstractJsonSchemaElement> getChildren() {
return schema.clone(schema).setParent(this).getChildren();
return schema.relink(this).getChildren();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidationError> errors) {

if (minimum != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public Stream<AbstractJsonSchemaElement> getChildren() {
final Stream<AbstractJsonSchemaElement> 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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,7 +56,7 @@ public JsonPropertiesImpl(AbstractJsonSchema parent,
public Stream<AbstractJsonSchemaElement> getChildren() {
// clone properties and set their parent to 'this'
final Stream<AbstractJsonSchemaElement> 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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ public Stream<AbstractJsonSchemaElement> 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
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public Stream<AbstractJsonSchemaElement> getChildren() {
final Stream<AbstractJsonSchemaElement> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public SchemaArrayImpl(AbstractJsonSchemaElement parent,
public Stream<AbstractJsonSchemaElement> getChildren() {
// clone array schemas and set their parent to 'this'
final Stream<AbstractJsonSchemaElement> 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()));
}
Expand Down

0 comments on commit ce5c2fd

Please sign in to comment.