Unify ModificationApplyOperation.toString()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / MinMaxElementsValidation.java
index 8da73070418f99b124faf18161629e4f3f8a9852..8e1e91c5633060d19c12e420622351199ea91dc2 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.base.MoreObjects.ToStringHelper;
 import java.util.Optional;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -25,43 +26,36 @@ 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
-    Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
+    Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta,
             final Version version) {
-        Optional<TreeNode> ret = modification.getValidatedNode(this, storeMeta);
+        Optional<? extends TreeNode> ret = modification.getValidatedNode(this, storeMeta);
         if (ret == null) {
             // Deal with the result moving on us
             ret = delegate.apply(modification, storeMeta, version);
@@ -75,7 +69,7 @@ final class MinMaxElementsValidation extends DelegatingModificationApplyOperatio
 
     @Override
     void checkApplicable(final ModificationPath path, final NodeModification modification,
-            final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
+            final Optional<? extends TreeNode> current, final Version version) throws DataValidationFailedException {
         delegate.checkApplicable(path, modification, current, version);
 
         if (!(modification instanceof ModifiedNode)) {
@@ -86,7 +80,7 @@ final class MinMaxElementsValidation extends DelegatingModificationApplyOperatio
 
         // We need to actually perform the operation to deal with merge in a sane manner. We know the modification
         // is immutable, so the result of validation will probably not change. Note we should not be checking number
-        final Optional<TreeNode> maybeApplied = delegate.apply(modified, current, version);
+        final Optional<? extends TreeNode> maybeApplied = delegate.apply(modified, current, version);
         if (maybeApplied.isPresent()) {
             // We only enforce min/max on present data and rely on MandatoryLeafEnforcer to take care of the empty case
             validateMinMaxElements(path, maybeApplied.get().getData());
@@ -107,6 +101,36 @@ 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);
+    }
+
+    @Override
+    ToStringHelper addToStringAttributes(final ToStringHelper helper) {
+        return helper.add("min", minElements).add("max", maxElements).add("delegate", delegate);
+    }
+
     private void validateMinMaxElements(final ModificationPath path, final NormalizedNode<?, ?> value)
             throws DataValidationFailedException {
         final PathArgument id = value.getIdentifier();
@@ -132,7 +156,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();
         }