Make MinMaxElementsValidation type-safe 04/80304/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 13 Feb 2019 09:56:23 +0000 (10:56 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 13 Feb 2019 09:56:23 +0000 (10:56 +0100)
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 <robert.varga@pantheon.tech>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/MinMaxElementsValidation.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/SchemaAwareApplyOperation.java

index 37414e0e8663ba17211e9b6b736b828e4c2aa874..687d87a40f282c44bb18a9b65c8e0fc1807365bb 100644 (file)
@@ -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<T extends DataSchemaNode & ElementCountConstraintAware>
+        extends DelegatingModificationApplyOperation {
     private static final Logger LOG = LoggerFactory.getLogger(MinMaxElementsValidation.class);
 
-    private final SchemaAwareApplyOperation<?> delegate;
+    private final SchemaAwareApplyOperation<T> delegate;
     private final int minElements;
     private final int maxElements;
 
-    private MinMaxElementsValidation(final SchemaAwareApplyOperation<?> delegate, final Integer minElements,
+    private MinMaxElementsValidation(final SchemaAwareApplyOperation<T> 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<ElementCountConstraint> optConstraint = ((ElementCountConstraintAware) schema)
-                .getElementCountConstraint();
+    static <T extends DataSchemaNode & ElementCountConstraintAware> ModificationApplyOperation from(
+            final SchemaAwareApplyOperation<T> delegate) {
+        final Optional<ElementCountConstraint> 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
index de206202dae67b0e8fa162d647ea9a0f07ada930..e78c444fe60479f082e9e4563b0162e16e89883a 100644 (file)
@@ -58,7 +58,8 @@ abstract class SchemaAwareApplyOperation<T extends WithStatus> 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<T extends WithStatus> extends Modificat
     private static ModificationApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode,
             final DataTreeConfiguration treeConfig) {
         final List<QName> keyDefinition = schemaNode.getKeyDefinition();
-        final SchemaAwareApplyOperation<?> op;
+        final SchemaAwareApplyOperation<ListSchemaNode> 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,