Rework NormalizedNode type hierarchy
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ModificationApplyOperation.java
index 92fda9d19bd402a31270d421b87a9c8132aa821e..a9a944801cc0e416662b16a66b6b40956c56ead2 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
+import com.google.common.base.MoreObjects;
+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;
@@ -16,26 +18,29 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
 
 /**
- * Operation responsible for applying {@link ModifiedNode} on tree.
- *
- * <p>
- * Operation is composite - operation on top level node consists of
- * suboperations on child nodes. This allows to walk operation hierarchy and
+ * An operation responsible for applying {@link ModifiedNode} on tree. The operation is a hierachical composite -
+ * the operation on top level node consists of suboperations on child nodes. This allows to walk operation hierarchy and
  * invoke suboperations independently.
  *
  * <p>
  * <b>Implementation notes</b>
  * <ul>
- * <li>
- * Implementations MUST expose all nested suboperations which operates on child
- * nodes expose via {@link #getChild(PathArgument)} method.
- * <li>Same suboperations SHOULD be used when invoked via
- * {@link #apply(ModifiedNode, Optional, Version)} if applicable.
+ *   <li>Implementations MUST expose all nested suboperations which operates on child nodes expose via
+ *       {@link #findChildByArg(PathArgument)} method.</li>
+ *   <li>Same suboperations SHOULD be used when invoked via {@link #apply(ModifiedNode, Optional, Version)},
+ *       if applicable.</li>
+ *   <li>There are exactly two base implementations:
+ *     <ul>
+ *       <li>{@link SchemaAwareApplyOperation}, which serves as the base class for stateful mutators -- directly
+ *           impacting the layout and transitions of the {@link TreeNode} hierarchy.
+ *       <li>{@link AbstractValidation}, which serves as the base class for stateless checks, which work purely on top
+ *           of the {@link TreeNode} hierarchy. These are always overlaid on top of some other
+ *           {@link ModificationApplyOperation}, ultimately leading to a {@link SchemaAwareApplyOperation}.
+ *     </ul>
+ *     This allows baseline invocations from {@link OperationWithModification} to be bimorphic in the first line of
+ *     dispatch.
+ *   </li>
  * </ul>
- *
- * <p>
- * Hierarchical composite operation which is responsible for applying
- * modification on particular subtree and creating updated subtree
  */
 abstract class ModificationApplyOperation implements StoreTreeNode<ModificationApplyOperation> {
     /**
@@ -54,7 +59,8 @@ abstract class ModificationApplyOperation implements StoreTreeNode<ModificationA
      *             If it is not possible to apply Operation on provided Metadata
      *             node
      */
-    abstract Optional<TreeNode> apply(ModifiedNode modification, Optional<TreeNode> storeMeta, Version version);
+    abstract Optional<? extends TreeNode> apply(ModifiedNode modification, Optional<? extends TreeNode> storeMeta,
+            Version version);
 
     /**
      * Checks if provided node modification could be applied to current metadata node.
@@ -66,18 +72,28 @@ abstract class ModificationApplyOperation implements StoreTreeNode<ModificationA
      * @throws DataValidationFailedException if the modification is not applicable
      */
     abstract void checkApplicable(ModificationPath path, NodeModification modification,
-            Optional<TreeNode> current, Version version) throws DataValidationFailedException;
+            Optional<? extends TreeNode> current, Version version) throws DataValidationFailedException;
+
+    /**
+     * Performs a quick structural verification of NodeModification, such as written values / types uses right
+     * structural elements.
+     *
+     * @param modification data to be verified.
+     * @throws IllegalArgumentException If provided NodeModification does not adhere to the
+     *         structure.
+     */
+    abstract void quickVerifyStructure(NormalizedNode modification);
 
     /**
-     * Performs structural verification of NodeModification, such as written values / types uses
-     * right structural elements.
+     * Performs a full structural verification of NodeModification, such as written values / types uses right
+     * structural elements. Unlike {@link #quickVerifyStructure(NormalizedNode)} this includes recursively checking
+     * children, too.
      *
      * @param modification data to be verified.
-     * @param verifyChildren true if structure verification should be run against children.
      * @throws IllegalArgumentException If provided NodeModification does not adhere to the
      *         structure.
      */
-    abstract void verifyStructure(NormalizedNode<?, ?> modification, boolean verifyChildren);
+    abstract void fullVerifyStructure(NormalizedNode modification);
 
     /**
      * Return the tracking policy for this node's children.
@@ -95,16 +111,23 @@ abstract class ModificationApplyOperation implements StoreTreeNode<ModificationA
      * @param value Value which should be merge into the modification
      * @param version Data version as carried in the containing {@link InMemoryDataTreeModification}
      */
-    abstract void mergeIntoModifiedNode(ModifiedNode modification, NormalizedNode<?, ?> value, Version version);
+    abstract void mergeIntoModifiedNode(ModifiedNode modification, NormalizedNode value, Version version);
 
     /**
-     * Returns a suboperation for specified tree node.
+     * {@inheritDoc}
      *
-     * @return Reference to suboperation for specified tree node, {@link Optional#empty()}
-     *         if suboperation is not supported for specified tree node.
+     * @return Reference to suboperation for specified tree node, {@code null} if suboperation is not supported for
+     *         specified tree node.
      */
     @Override
-    public abstract Optional<ModificationApplyOperation> getChild(PathArgument child);
+    public abstract ModificationApplyOperation childByArg(PathArgument arg);
 
-    abstract void recursivelyVerifyStructure(NormalizedNode<?, ?> value);
+    abstract void recursivelyVerifyStructure(NormalizedNode value);
+
+    abstract ToStringHelper addToStringAttributes(ToStringHelper helper);
+
+    @Override
+    public final String toString() {
+        return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
+    }
 }