From: Robert Varga Date: Wed, 13 Feb 2019 09:56:23 +0000 (+0100) Subject: Make MinMaxElementsValidation type-safe X-Git-Tag: v2.0.17~19 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=3c02bfbc4a1849409d32dc9b5116088878b7c489;p=yangtools.git Make MinMaxElementsValidation type-safe Since SchemaAwareApplyOperation exposes underlying schema, we can make MinMaxElementsValidation require a specific schema type, making the code a bit simpler. JIRA: YANGTOOLS-955 Change-Id: I00700d2b45f4163072d8c4d4c364bd8047e5b430 Signed-off-by: Robert Varga --- diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/MinMaxElementsValidation.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/MinMaxElementsValidation.java index 37414e0e86..687d87a40f 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/MinMaxElementsValidation.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/MinMaxElementsValidation.java @@ -25,32 +25,30 @@ import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -final class MinMaxElementsValidation extends DelegatingModificationApplyOperation { +final class MinMaxElementsValidation + extends DelegatingModificationApplyOperation { private static final Logger LOG = LoggerFactory.getLogger(MinMaxElementsValidation.class); - private final SchemaAwareApplyOperation delegate; + private final SchemaAwareApplyOperation delegate; private final int minElements; private final int maxElements; - private MinMaxElementsValidation(final SchemaAwareApplyOperation delegate, final Integer minElements, + private MinMaxElementsValidation(final SchemaAwareApplyOperation delegate, final Integer minElements, final Integer maxElements) { this.delegate = requireNonNull(delegate); this.minElements = minElements != null ? minElements : 0; this.maxElements = maxElements != null ? maxElements : Integer.MAX_VALUE; } - static ModificationApplyOperation from(final SchemaAwareApplyOperation delegate, final DataSchemaNode schema) { - if (!(schema instanceof ElementCountConstraintAware)) { - return delegate; - } - final Optional optConstraint = ((ElementCountConstraintAware) schema) - .getElementCountConstraint(); + static ModificationApplyOperation from( + final SchemaAwareApplyOperation delegate) { + final Optional optConstraint = delegate.getSchema().getElementCountConstraint(); if (!optConstraint.isPresent()) { return delegate; } final ElementCountConstraint constraint = optConstraint.get(); - return new MinMaxElementsValidation(delegate, constraint.getMinElements(), constraint.getMaxElements()); + return new MinMaxElementsValidation<>(delegate, constraint.getMinElements(), constraint.getMaxElements()); } @Override diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/SchemaAwareApplyOperation.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/SchemaAwareApplyOperation.java index de206202da..e78c444fe6 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/SchemaAwareApplyOperation.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/SchemaAwareApplyOperation.java @@ -58,7 +58,8 @@ abstract class SchemaAwareApplyOperation extends Modificat } else if (schemaNode instanceof ChoiceSchemaNode) { return new ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode, treeConfig); } else if (schemaNode instanceof LeafListSchemaNode) { - return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode, treeConfig); + return MinMaxElementsValidation.from(new LeafSetModificationStrategy((LeafListSchemaNode) schemaNode, + treeConfig)); } else if (schemaNode instanceof LeafSchemaNode) { return new ValueNodeModificationStrategy<>(LeafNode.class, (LeafSchemaNode) schemaNode); } @@ -95,18 +96,13 @@ abstract class SchemaAwareApplyOperation extends Modificat private static ModificationApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode, final DataTreeConfiguration treeConfig) { final List keyDefinition = schemaNode.getKeyDefinition(); - final SchemaAwareApplyOperation op; + final SchemaAwareApplyOperation op; if (keyDefinition == null || keyDefinition.isEmpty()) { op = new UnkeyedListModificationStrategy(schemaNode, treeConfig); } else { op = MapModificationStrategy.of(schemaNode, treeConfig); } - return MinMaxElementsValidation.from(op, schemaNode); - } - - private static ModificationApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode, - final DataTreeConfiguration treeConfig) { - return MinMaxElementsValidation.from(new LeafSetModificationStrategy(schemaNode, treeConfig), schemaNode); + return MinMaxElementsValidation.from(op); } protected static void checkNotConflicting(final ModificationPath path, final TreeNode original,