Address trivial eclipse warnings
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractDataTreeCandidateNode.java
index 64930c7e129b20b3adf3d3d4ca9f69827c8f1e71..082dfd4c870c05c9aa630651af781f8689c57182 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -7,14 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Collections2;
-import com.google.common.collect.Maps;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Map;
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -22,53 +20,73 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
 
 abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
-    private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_DELETED_NODES = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
-        @Override
-        public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
-            return AbstractRecursiveCandidateNode.deleteNode(input);
-        }
-    };    private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_WRITTEN_NODES = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
-        @Override
-        public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
-            return AbstractRecursiveCandidateNode.writeNode(input);
+    private static Optional<NormalizedNode<?, ?>> getChild(
+            final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> container,
+                    final PathArgument identifier) {
+        return container == null ? Optional.absent() : container.getChild(identifier);
+    }
+
+    static DataTreeCandidateNode deltaChild(
+            final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
+            final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData, final PathArgument identifier) {
+
+        final Optional<NormalizedNode<?, ?>> maybeNewChild = getChild(newData, identifier);
+        final Optional<NormalizedNode<?, ?>> maybeOldChild = getChild(oldData, identifier);
+        if (maybeOldChild.isPresent()) {
+            final NormalizedNode<?, ?> oldChild = maybeOldChild.get();
+            if (maybeNewChild.isPresent()) {
+                return AbstractRecursiveCandidateNode.replaceNode(oldChild, maybeNewChild.get());
+            }
+            return AbstractRecursiveCandidateNode.deleteNode(oldChild);
         }
-    };
 
-    static Collection<DataTreeCandidateNode> deltaChildren(@Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
+        return maybeNewChild.isPresent() ? AbstractRecursiveCandidateNode.writeNode(maybeNewChild.get()) : null;
+    }
+
+    static Collection<DataTreeCandidateNode> deltaChildren(
+            @Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
             @Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData) {
+        Preconditions.checkArgument(newData != null || oldData != null,
+                "No old or new data, modification type should be NONE and deltaChildren() mustn't be called.");
         if (newData == null) {
-            return Collections2.transform(oldData.getValue(), TO_DELETED_NODES);
+            return Collections2.transform(oldData.getValue(), AbstractRecursiveCandidateNode::deleteNode);
         }
         if (oldData == null) {
-            return Collections2.transform(newData.getValue(), TO_WRITTEN_NODES);
-        }
-
-        // Create index for fast cross-references
-        // FIXME: speed this up by exposing maps inside ImmutableMapNode and similar.
-        final Map<PathArgument, NormalizedNode<?, ?>> oldChildren = Maps.newHashMapWithExpectedSize(oldData.getValue().size());
-        for (NormalizedNode<?, ?> child : oldData.getValue()) {
-            oldChildren.put(child.getIdentifier(), child);
+            return Collections2.transform(newData.getValue(), AbstractRecursiveCandidateNode::writeNode);
         }
 
-        final Collection<DataTreeCandidateNode> ret = new ArrayList<>(Math.max(oldChildren.size(), newData.getValue().size()));
+        /*
+         * This is slightly inefficient, as it requires N*F(M)+M*F(N) lookup operations, where
+         * F is dependent on the implementation of NormalizedNodeContainer.getChild().
+         *
+         * We build the return collection by iterating over new data and looking each child up
+         * in old data. Based on that we construct replaced/written nodes. We then proceed to
+         * iterate over old data and looking up each child in new data.
+         */
+        final Collection<DataTreeCandidateNode> result = new ArrayList<>();
         for (NormalizedNode<?, ?> child : newData.getValue()) {
-            // Slight optimization iterator length
-            final NormalizedNode<?, ?> oldChild = oldChildren.remove(child.getIdentifier());
             final DataTreeCandidateNode node;
-            if (oldChild == null) {
-                node = AbstractRecursiveCandidateNode.writeNode(child);
+            final Optional<NormalizedNode<?, ?>> maybeOldChild = oldData.getChild(child.getIdentifier());
+
+            if (maybeOldChild.isPresent()) {
+                // This does not find children which have not in fact been modified, as doing that
+                // reliably would require us running a full equals() on the two nodes.
+                node = AbstractRecursiveCandidateNode.replaceNode(maybeOldChild.get(), child);
             } else {
-                node = AbstractRecursiveCandidateNode.replaceNode(oldChild, child);
+                node = AbstractRecursiveCandidateNode.writeNode(child);
             }
 
-            ret.add(node);
+            result.add(node);
         }
 
-        for (NormalizedNode<?, ?> child : oldChildren.values()) {
-            ret.add(AbstractRecursiveCandidateNode.deleteNode(child));
+        // Process removals next, looking into new data to see if we processed it
+        for (NormalizedNode<?, ?> child : oldData.getValue()) {
+            if (!newData.getChild(child.getIdentifier()).isPresent()) {
+                result.add(AbstractRecursiveCandidateNode.deleteNode(child));
+            }
         }
 
-        return ret;
+        return result;
     }
 
     private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?,?>> data;
@@ -78,10 +96,11 @@ abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
     }
 
     protected final Optional<NormalizedNode<?, ?>> dataOptional() {
-        return Optional.<NormalizedNode<?, ?>>of(data);
+        return Optional.of(data);
     }
 
     @Override
+    @Nonnull
     public final PathArgument getIdentifier() {
         return data.getIdentifier();
     }
@@ -89,4 +108,9 @@ abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
     protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
         return data;
     }
+
+    @Override
+    public String toString() {
+        return this.getClass().getSimpleName() + "{data = " + this.data + "}";
+    }
 }