Add NormalizedNodeContainer.size()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / MinMaxElementsValidation.java
index 8da73070418f99b124faf18161629e4f3f8a9852..d1a390b7c240cd76bd1611d9bfc96f87aecb01cb 100644 (file)
@@ -25,37 +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 ModificationApplyOperation {
     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());
-    }
-
-    @Override
-    ModificationApplyOperation delegate() {
-        return delegate;
+        return new MinMaxElementsValidation<>(delegate, constraint.getMinElements(), constraint.getMaxElements());
     }
 
     @Override
@@ -107,6 +100,31 @@ final class MinMaxElementsValidation extends DelegatingModificationApplyOperatio
         checkChildren(modification);
     }
 
+    @Override
+    public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
+        return delegate.getChild(child);
+    }
+
+    @Override
+    ChildTrackingPolicy getChildPolicy() {
+        return delegate.getChildPolicy();
+    }
+
+    @Override
+    void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
+        delegate.mergeIntoModifiedNode(node, value, version);
+    }
+
+    @Override
+    void quickVerifyStructure(final NormalizedNode<?, ?> modification) {
+        delegate.quickVerifyStructure(modification);
+    }
+
+    @Override
+    void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
+        delegate.recursivelyVerifyStructure(value);
+    }
+
     private void validateMinMaxElements(final ModificationPath path, final NormalizedNode<?, ?> value)
             throws DataValidationFailedException {
         final PathArgument id = value.getIdentifier();
@@ -132,7 +150,7 @@ final class MinMaxElementsValidation extends DelegatingModificationApplyOperatio
 
     private static int numOfChildrenFromValue(final NormalizedNode<?, ?> value) {
         if (value instanceof NormalizedNodeContainer) {
-            return ((NormalizedNodeContainer<?, ?, ?>) value).getValue().size();
+            return ((NormalizedNodeContainer<?, ?, ?>) value).size();
         } else if (value instanceof UnkeyedListNode) {
             return ((UnkeyedListNode) value).getSize();
         }