Clarify the correctness of Optional.get() 91/19691/3
authorRobert Varga <rovarga@cisco.com>
Wed, 6 May 2015 09:37:58 +0000 (11:37 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 13 May 2015 09:27:22 +0000 (09:27 +0000)
The check of Optional.isPresent() exists in all codepaths, but it is not
immediately obvious. Make it more explicit.

Change-Id: I73b6931299ee8cd8e40d0331acc6d6e753db8612
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractNodeContainerModificationStrategy.java

index b323e6b397fbda9e4b9c5ada6e664de5a5d447eb..8a8afb046a859c2787280efdcc9a86b738dab662 100644 (file)
@@ -14,6 +14,7 @@ import java.util.Collection;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException;
@@ -181,15 +182,24 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl
             throw new ModifiedNodeDoesNotExistException(path, String.format("Node %s does not exist. Cannot apply modification to its children.", path));
         }
 
-        SchemaAwareApplyOperation.checkConflicting(path, current.isPresent(), "Node was deleted by other transaction.");
-        checkChildPreconditions(path, modification, current);
+        if (!current.isPresent()) {
+            throw new ConflictingModificationAppliedException(path, "Node was deleted by other transaction.");
+        }
+
+        checkChildPreconditions(path, modification, current.get());
     }
 
-    private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
-        final TreeNode currentMeta = current.get();
+    /**
+     * Recursively check child preconditions.
+     *
+     * @param path current node path
+     * @param modification current modification
+     * @param current Current data tree node.
+     */
+    private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final TreeNode current) throws DataValidationFailedException {
         for (NodeModification childMod : modification.getChildren()) {
             final YangInstanceIdentifier.PathArgument childId = childMod.getIdentifier();
-            final Optional<TreeNode> childMeta = currentMeta.getChild(childId);
+            final Optional<TreeNode> childMeta = current.getChild(childId);
 
             YangInstanceIdentifier childPath = path.node(childId);
             resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta);
@@ -199,8 +209,8 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl
     @Override
     protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
             final Optional<TreeNode> current) throws DataValidationFailedException {
-        if(current.isPresent()) {
-            checkChildPreconditions(path, modification,current);
+        if (current.isPresent()) {
+            checkChildPreconditions(path, modification, current.get());
         }
     }