Refactor ModificationApplyOperation
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / AutomaticLifecycleMixin.java
index 89467acdbb69ae6e5ff9fe5f83994f9749584912..fa16bbd8e9d8da1aa474ebc12f9959134d568ac6 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.yangtools.yang.data.tree.impl;
 import static com.google.common.base.Preconditions.checkState;
 
 import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
@@ -22,52 +24,51 @@ import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
  */
 final class AutomaticLifecycleMixin {
     /**
-     * This is a capture of {@link ModificationApplyOperation#apply(ModifiedNode, Optional, Version)}.
+     * This is a capture of {@link ModificationApplyOperation#apply(ModifiedNode, TreeNode, Version)}.
      */
     @FunctionalInterface
     interface Apply {
-        Optional<? extends TreeNode> apply(ModifiedNode modification, Optional<? extends TreeNode> storeMeta,
-                Version version);
+        Optional<? extends TreeNode> apply(ModifiedNode modification, @Nullable TreeNode currentMeta, Version version);
     }
 
     /**
      * This is a capture of
-     * {@link SchemaAwareApplyOperation#applyWrite(ModifiedNode, NormalizedNode, Optional, Version)}.
+     * {@link SchemaAwareApplyOperation#applyWrite(ModifiedNode, NormalizedNode, TreeNode, Version)}.
      */
     @FunctionalInterface
     interface ApplyWrite {
-        TreeNode applyWrite(ModifiedNode modification, NormalizedNode newValue,
-                Optional<? extends TreeNode> storeMeta, Version version);
+        TreeNode applyWrite(ModifiedNode modification, NormalizedNode newValue, @Nullable TreeNode currentMeta,
+            Version version);
     }
 
     private AutomaticLifecycleMixin() {
-
+        // Hidden on purpose
     }
 
     static Optional<? extends TreeNode> apply(final Apply delegate, final ApplyWrite writeDelegate,
-            final NormalizedNode emptyNode, final ModifiedNode modification,
-            final Optional<? extends TreeNode> storeMeta, final Version version) {
+            final NormalizedNode emptyNode, final ModifiedNode modification, final @Nullable TreeNode currentMeta,
+            final Version version) {
         final Optional<? extends TreeNode> ret;
         if (modification.getOperation() == LogicalOperation.DELETE) {
             if (modification.isEmpty()) {
-                return delegate.apply(modification, storeMeta, version);
+                return delegate.apply(modification, currentMeta, version);
             }
             // Delete with children, implies it really is an empty write
-            ret = Optional.of(writeDelegate.applyWrite(modification, emptyNode, storeMeta, version));
-        } else if (modification.getOperation() == LogicalOperation.TOUCH && storeMeta.isEmpty()) {
-            ret = applyTouch(delegate, emptyNode, modification, storeMeta, version);
+            ret = Optional.of(writeDelegate.applyWrite(modification, emptyNode, currentMeta, version));
+        } else if (modification.getOperation() == LogicalOperation.TOUCH && currentMeta == null) {
+            ret = applyTouch(delegate, emptyNode, modification, null, version);
         } else {
             // No special handling required here, run normal apply operation
-            ret = delegate.apply(modification, storeMeta, version);
+            ret = delegate.apply(modification, currentMeta, version);
         }
 
-        return ret.isPresent() ? disappearResult(modification, ret.orElseThrow(), storeMeta) : ret;
+        return ret.isPresent() ? disappearResult(modification, ret.orElseThrow(), currentMeta) : ret;
     }
 
     private static Optional<? extends TreeNode> applyTouch(final Apply delegate, final NormalizedNode emptyNode,
-            final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta, final Version version) {
+            final ModifiedNode modification, final @Nullable TreeNode currentMeta, final Version version) {
         // Container is not present, let's take care of the 'magically appear' part of our job
-        final Optional<? extends TreeNode> ret = delegate.apply(modification, fakeMeta(emptyNode, version), version);
+        final var ret = delegate.apply(modification, fakeMeta(emptyNode, version), version);
 
         // If the delegate indicated SUBTREE_MODIFIED, account for the fake and report APPEARED
         if (modification.getModificationType() == ModificationType.SUBTREE_MODIFIED) {
@@ -77,7 +78,7 @@ final class AutomaticLifecycleMixin {
     }
 
     private static Optional<? extends TreeNode> disappearResult(final ModifiedNode modification, final TreeNode result,
-            final Optional<? extends TreeNode> storeMeta) {
+            final @Nullable TreeNode currentMeta) {
         // Check if the result is in fact empty before pulling any tricks
         if (!isEmpty(result)) {
             return Optional.of(result);
@@ -85,7 +86,7 @@ final class AutomaticLifecycleMixin {
 
         // We are pulling the 'disappear' trick, but what we report can be three different things
         final ModificationType finalType;
-        if (storeMeta.isEmpty()) {
+        if (currentMeta == null) {
             // ... there was nothing in the datastore, no change
             finalType = ModificationType.UNMODIFIED;
         } else if (modification.getModificationType() == ModificationType.WRITE) {
@@ -99,8 +100,8 @@ final class AutomaticLifecycleMixin {
         return Optional.empty();
     }
 
-    private static Optional<TreeNode> fakeMeta(final NormalizedNode emptyNode, final Version version) {
-        return Optional.of(TreeNode.of(emptyNode, version));
+    private static @NonNull TreeNode fakeMeta(final NormalizedNode emptyNode, final Version version) {
+        return TreeNode.of(emptyNode, version);
     }
 
     private static boolean isEmpty(final TreeNode treeNode) {