-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.yangtools.yang.data.impl;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.Stack;\r
-\r
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;\r
-import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;\r
-import org.opendaylight.yangtools.yang.data.api.MutableNode;\r
-import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;\r
-import org.opendaylight.yangtools.yang.data.api.Node;\r
-import org.opendaylight.yangtools.yang.data.api.NodeModification;\r
-import org.opendaylight.yangtools.yang.data.api.SimpleNode;\r
-\r
-/**\r
- * @author michal.rehak\r
- *\r
- */\r
-public class LazyNodeToNodeMap {\r
-\r
- private Map<Node<?>, Node<?>> node2node = new HashMap<>();\r
- private CompositeNode originalRoot;\r
- private MutableCompositeNode mutableRoot;\r
-\r
- /**\r
- * @param originalNode\r
- * @return mutable twin\r
- */\r
- public Node<?> getMutableEquivalent(Node<?> originalNode) {\r
- Node<?> mutableNode = node2node.get(originalNode);\r
- if (mutableNode == null) {\r
- addPathMembers(originalNode);\r
- mutableNode = node2node.get(originalNode);\r
- }\r
-\r
- return mutableNode;\r
- }\r
-\r
- /**\r
- * @param originalNode\r
- */\r
- private void addPathMembers(Node<?> originalNode) {\r
- Stack<Node<?>> jobQueue = new Stack<>();\r
- jobQueue.push(originalNode);\r
- while (!jobQueue.isEmpty()) {\r
- Node<?> node2add = jobQueue.pop();\r
- boolean fixChildrenRefOnly = false;\r
- if (node2node.containsKey(node2add)) {\r
- if (node2add instanceof SimpleNode<?>) {\r
- continue;\r
- }\r
- fixChildrenRefOnly = true;\r
- }\r
-\r
- CompositeNode nextParent = node2add.getParent();\r
- MutableNode<?> mutableEquivalent = null;\r
-\r
- if (node2add instanceof SimpleNode<?>) {\r
- SimpleNode<?> node2addSimple = (SimpleNode<?>) node2add;\r
- MutableSimpleNode<?> nodeMutant = NodeFactory.createMutableSimpleNode(\r
- node2add.getNodeType(), null, node2addSimple.getValue(),\r
- node2addSimple.getModificationAction(), node2addSimple);\r
- mutableEquivalent = nodeMutant;\r
- } else if (node2add instanceof CompositeNode) {\r
- MutableCompositeNode nodeMutant = null;\r
- if (fixChildrenRefOnly) {\r
- nodeMutant = (MutableCompositeNode) node2node.get(node2add);\r
- } else {\r
- CompositeNode node2addComposite = (CompositeNode) node2add;\r
- nodeMutant = NodeFactory.createMutableCompositeNode(node2add.getNodeType(),\r
- null, null,\r
- ((NodeModification) node2add).getModificationAction(), node2addComposite);\r
- }\r
-\r
- mutableEquivalent = nodeMutant;\r
-\r
- // tidy up children\r
- if (nodeMutant.getChildren() == null) {\r
- nodeMutant.setValue(new ArrayList<Node<?>>());\r
- }\r
- for (Node<?> originalChildNode : ((CompositeNode) node2add).getChildren()) {\r
- MutableNode<?> mutableChild = (MutableNode<?>) node2node.get(originalChildNode);\r
- fixChildrenRef(nodeMutant, mutableChild);\r
- }\r
-\r
- if (nodeMutant.getChildren() != null && !nodeMutant.getChildren().isEmpty()) {\r
- nodeMutant.init();\r
- }\r
-\r
- // store tree root, if occured\r
- if (nextParent == null) {\r
- if (originalRoot == null) {\r
- originalRoot = (CompositeNode) node2add;\r
- mutableRoot = nodeMutant;\r
- } else {\r
- if (!originalRoot.equals(node2add)) {\r
- throw new IllegalStateException("Different tree root node obtained - " +\r
- "perhaps nodes of different trees are getting mixed up.");\r
- }\r
- }\r
- }\r
- }\r
-\r
- // feed jobQueue\r
- node2node.put(node2add, mutableEquivalent);\r
- if (nextParent != null) {\r
- jobQueue.push(nextParent);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * @param nodeMutant\r
- * @param mutableChild\r
- */\r
- private static void fixChildrenRef(MutableCompositeNode nodeMutant,\r
- MutableNode<?> mutableChild) {\r
- if (mutableChild != null) {\r
- if (!nodeMutant.getChildren().contains(mutableChild)) {\r
- nodeMutant.getChildren().add(mutableChild);\r
- }\r
- CompositeNode parentOfChild = mutableChild.getParent();\r
- if (parentOfChild == null) {\r
- mutableChild.setParent(nodeMutant);\r
- } else {\r
- if (!parentOfChild.equals(nodeMutant)) {\r
- throw new IllegalStateException("Different parent node obtained - " +\r
- "perhaps nodes of different trees are getting mixed up.");\r
- }\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * @return the mutableRoot\r
- */\r
- public MutableCompositeNode getMutableRoot() {\r
- return mutableRoot;\r
- }\r
-\r
- /**\r
- * @return set of original nodes, registered in map as keys\r
- */\r
- public Set<Node<?>> getKeyNodes() {\r
- return node2node.keySet();\r
- }\r
-}\r
+/*
+ * Copyright (c) 2013 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,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl;
+
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
+import org.opendaylight.yangtools.yang.data.api.MutableNode;
+import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.NodeModification;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+
+/**
+ * @author michal.rehak
+ *
+ */
+public class LazyNodeToNodeMap {
+
+ private final Map<Node<?>, Node<?>> node2node = new HashMap<>();
+ private CompositeNode originalRoot;
+ private MutableCompositeNode mutableRoot;
+
+ /**
+ * @param originalNode
+ * @return mutable twin
+ */
+ public Node<?> getMutableEquivalent(final Node<?> originalNode) {
+ Node<?> mutableNode = node2node.get(originalNode);
+ if (mutableNode == null) {
+ addPathMembers(originalNode);
+ mutableNode = node2node.get(originalNode);
+ }
+
+ return mutableNode;
+ }
+
+ /**
+ * @param originalNode
+ */
+ private void addPathMembers(final Node<?> originalNode) {
+ final Deque<Node<?>> jobQueue = new ArrayDeque<>();
+ jobQueue.push(originalNode);
+ while (!jobQueue.isEmpty()) {
+ Node<?> node2add = jobQueue.pop();
+ boolean fixChildrenRefOnly = false;
+ if (node2node.containsKey(node2add)) {
+ if (node2add instanceof SimpleNode<?>) {
+ continue;
+ }
+ fixChildrenRefOnly = true;
+ }
+
+ CompositeNode nextParent = node2add.getParent();
+ MutableNode<?> mutableEquivalent = null;
+
+ if (node2add instanceof SimpleNode<?>) {
+ SimpleNode<?> node2addSimple = (SimpleNode<?>) node2add;
+ MutableSimpleNode<?> nodeMutant = NodeFactory.createMutableSimpleNode(
+ node2add.getNodeType(), null, node2addSimple.getValue(),
+ node2addSimple.getModificationAction(), node2addSimple);
+ mutableEquivalent = nodeMutant;
+ } else if (node2add instanceof CompositeNode) {
+ MutableCompositeNode nodeMutant = null;
+ if (fixChildrenRefOnly) {
+ nodeMutant = (MutableCompositeNode) node2node.get(node2add);
+ } else {
+ CompositeNode node2addComposite = (CompositeNode) node2add;
+ nodeMutant = NodeFactory.createMutableCompositeNode(node2add.getNodeType(),
+ null, null,
+ ((NodeModification) node2add).getModificationAction(), node2addComposite);
+ }
+
+ mutableEquivalent = nodeMutant;
+
+ // tidy up children
+ if (nodeMutant.getValue() == null) {
+ nodeMutant.setValue(new ArrayList<Node<?>>());
+ }
+ for (Node<?> originalChildNode : ((CompositeNode) node2add).getValue()) {
+ MutableNode<?> mutableChild = (MutableNode<?>) node2node.get(originalChildNode);
+ fixChildrenRef(nodeMutant, mutableChild);
+ }
+
+ if (nodeMutant.getValue() != null && !nodeMutant.getValue().isEmpty()) {
+ nodeMutant.init();
+ }
+
+ // store tree root, if occured
+ if (nextParent == null) {
+ if (originalRoot == null) {
+ originalRoot = (CompositeNode) node2add;
+ mutableRoot = nodeMutant;
+ } else {
+ if (!originalRoot.equals(node2add)) {
+ throw new IllegalStateException("Different tree root node obtained - " +
+ "perhaps nodes of different trees are getting mixed up.");
+ }
+ }
+ }
+ }
+
+ // feed jobQueue
+ node2node.put(node2add, mutableEquivalent);
+ if (nextParent != null) {
+ jobQueue.push(nextParent);
+ }
+ }
+ }
+
+ /**
+ * @param nodeMutant
+ * @param mutableChild
+ */
+ private static void fixChildrenRef(final MutableCompositeNode nodeMutant,
+ final MutableNode<?> mutableChild) {
+ if (mutableChild != null) {
+ if (!nodeMutant.getValue().contains(mutableChild)) {
+ nodeMutant.getValue().add(mutableChild);
+ }
+ CompositeNode parentOfChild = mutableChild.getParent();
+ if (parentOfChild == null) {
+ mutableChild.setParent(nodeMutant);
+ } else {
+ if (!parentOfChild.equals(nodeMutant)) {
+ throw new IllegalStateException("Different parent node obtained - " +
+ "perhaps nodes of different trees are getting mixed up.");
+ }
+ }
+ }
+ }
+
+ /**
+ * @return the mutableRoot
+ */
+ public MutableCompositeNode getMutableRoot() {
+ return mutableRoot;
+ }
+
+ /**
+ * @return set of original nodes, registered in map as keys
+ */
+ public Set<Node<?>> getKeyNodes() {
+ return node2node.keySet();
+ }
+}