Rework NormalizedNode type hierarchy
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ValueNodeModificationStrategy.java
index 6287ed6721165d31a0aa32ddcd14e3dfc86df7c8..a45c95984674fa6ed586550b80b6e5dc0b08af5f 100644 (file)
@@ -10,10 +10,11 @@ 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.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.ValueNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
@@ -21,19 +22,24 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 
-final class ValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation {
-    private final Class<? extends ValueNode> nodeClass;
-    private final T schema;
+final class ValueNodeModificationStrategy<T extends DataSchemaNode, V extends NormalizedNode>
+        extends SchemaAwareApplyOperation<T> {
+    private final @NonNull Class<V> nodeClass;
+    private final @NonNull T schema;
 
-    ValueNodeModificationStrategy(final Class<? extends ValueNode> nodeClass, final T schema) {
+    ValueNodeModificationStrategy(final Class<V> nodeClass, final T schema) {
         this.nodeClass = requireNonNull(nodeClass);
         this.schema = requireNonNull(schema);
     }
 
     @Override
-    public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
-        throw new UnsupportedOperationException("Node " + schema.getPath()
-                + " is leaf type node. Child nodes not allowed");
+    T getSchema() {
+        return schema;
+    }
+
+    @Override
+    public ModificationApplyOperation childByArg(final PathArgument arg) {
+        throw new UnsupportedOperationException("Node " + schema + " is leaf type node. Child nodes not allowed");
     }
 
     @Override
@@ -44,34 +50,34 @@ final class ValueNodeModificationStrategy<T extends DataSchemaNode> extends Sche
     @Override
     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
             final Version version) {
-        throw new UnsupportedOperationException("Node " + schema.getPath()
-                + " is leaf type node. Subtree change is not allowed.");
+        throw new UnsupportedOperationException("Node " + schema + " is leaf type node. "
+            + " Subtree change is not allowed.");
     }
 
     @Override
     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
             final Version version) {
         // Just overwrite whatever was there, but be sure to run validation
-        final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
+        final NormalizedNode newValue = modification.getWrittenValue();
         verifyWrittenValue(newValue);
         modification.resolveModificationType(ModificationType.WRITE);
         return applyWrite(modification, newValue, null, version);
     }
 
     @Override
-    protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
-            final Optional<TreeNode> currentMeta, final Version version) {
+    protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
+            final Optional<? extends TreeNode> currentMeta, final Version version) {
         return TreeNodeFactory.createTreeNode(newValue, version);
     }
 
     @Override
     protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
-            final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
+            final Optional<? extends TreeNode> current, final Version version) throws IncorrectDataStructureException {
         throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
     }
 
     @Override
-    void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
+    void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode value, final Version version) {
         switch (node.getOperation()) {
             // Delete performs a data dependency check on existence of the node. Performing a merge
             // on DELETE means we
@@ -86,16 +92,21 @@ final class ValueNodeModificationStrategy<T extends DataSchemaNode> extends Sche
     }
 
     @Override
-    void verifyValue(final NormalizedNode<?, ?> writtenValue) {
+    void verifyValue(final NormalizedNode writtenValue) {
         verifyWrittenValue(writtenValue);
     }
 
     @Override
-    void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
+    void recursivelyVerifyStructure(final NormalizedNode value) {
         verifyWrittenValue(value);
     }
 
-    private void verifyWrittenValue(final NormalizedNode<?, ?> value) {
+    @Override
+    ToStringHelper addToStringAttributes(final ToStringHelper helper) {
+        return helper.add("value", nodeClass.getSimpleName());
+    }
+
+    private void verifyWrittenValue(final NormalizedNode value) {
         checkArgument(nodeClass.isInstance(value), "Expected an instance of %s, have %s", nodeClass, value);
     }
 }