Merge "Bug 1036 - Allow using container in case stmt to preserve uniqueness."
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / CompositeNodeWrapper.java
index e000c7e29e21c0a39904088351940d6862e3f42d..96ad528a0dd3f9e0591b78b2be702e9b1d004c4c 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * Copyright (c) 2014 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.controller.sal.restconf.impl;
 
 import java.net.URI;
@@ -24,17 +31,29 @@ public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, C
 
     private String localName;
     private URI namespace;
+    private QName name;
     private List<NodeWrapper<?>> values = new ArrayList<>();
-    
-    public CompositeNodeWrapper(String localName) {
+
+    public CompositeNodeWrapper(final String localName) {
         this.localName = Preconditions.checkNotNull(localName);
     }
-    
-    public CompositeNodeWrapper(URI namespace, String localName) {
+
+    public CompositeNodeWrapper(final URI namespace, final String localName) {
         this(localName);
         this.namespace = namespace;
     }
 
+    @Override
+    public void setQname(final QName name) {
+        Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
+        this.name = name;
+    }
+
+    @Override
+    public QName getQname() {
+        return name;
+    }
+
     @Override
     public String getLocalName() {
         if (compositeNode != null) {
@@ -52,174 +71,186 @@ public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, C
     }
 
     @Override
-    public void setNamespace(URI namespace) {
+    public void setNamespace(final URI namespace) {
         Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
         this.namespace = namespace;
     }
 
-    public void addValue(NodeWrapper<?> value) {
+    public void addValue(final NodeWrapper<?> value) {
         Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
         values.add(value);
     }
 
-    public void removeValue(NodeWrapper<CompositeNode> value) {
+    public void removeValue(final NodeWrapper<CompositeNode> value) {
         Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
         values.remove(value);
     }
-    
+
     public List<NodeWrapper<?>> getValues() {
         Preconditions.checkState(compositeNode == null, "Data can be inconsistent.");
         return Collections.unmodifiableList(values);
     }
 
     @Override
-    public CompositeNode unwrap(CompositeNode parent) {
+    public boolean isChangeAllowed() {
+        return compositeNode == null ? true : false;
+    }
+
+    @Override
+    public CompositeNode unwrap() {
         if (compositeNode == null) {
-            Preconditions.checkNotNull(namespace);
-            compositeNode = NodeFactory.createMutableCompositeNode(new QName(namespace, localName), 
-                    parent, new ArrayList<Node<?>>(), ModifyAction.CREATE, null);
-            
+            if (name == null) {
+                Preconditions.checkNotNull(namespace);
+                name = new QName(namespace, localName);
+            }
+
             List<Node<?>> nodeValues = new ArrayList<>();
             for (NodeWrapper<?> nodeWrapper : values) {
-                nodeValues.add(nodeWrapper.unwrap(compositeNode));
+                nodeValues.add(nodeWrapper.unwrap());
             }
-            compositeNode.setValue(nodeValues);
-            
+            compositeNode = NodeFactory.createMutableCompositeNode(name, null, nodeValues, null, null);
+
             values = null;
             namespace = null;
             localName = null;
+            name = null;
         }
         return compositeNode;
     }
 
     @Override
     public QName getNodeType() {
-        return unwrap(null).getNodeType();
+        return unwrap().getNodeType();
     }
 
+    @Deprecated
     @Override
     public CompositeNode getParent() {
-        return unwrap(null).getParent();
+        return unwrap().getParent();
     }
 
     @Override
     public List<Node<?>> getValue() {
-        return unwrap(null).getValue();
+        return unwrap().getValue();
     }
 
     @Override
     public ModifyAction getModificationAction() {
-        return unwrap(null).getModificationAction();
+        return unwrap().getModificationAction();
     }
 
+    /**
+     * @deprecated Use {@link #getValue()} instead.
+     */
+    @Deprecated
     @Override
     public List<Node<?>> getChildren() {
-        return unwrap(null).getChildren();
+        return unwrap().getValue();
     }
 
     @Override
-    public List<CompositeNode> getCompositesByName(QName children) {
-        return unwrap(null).getCompositesByName(children);
+    public List<CompositeNode> getCompositesByName(final QName children) {
+        return unwrap().getCompositesByName(children);
     }
 
     @Override
-    public List<CompositeNode> getCompositesByName(String children) {
-        return unwrap(null).getCompositesByName(children);
+    public List<CompositeNode> getCompositesByName(final String children) {
+        return unwrap().getCompositesByName(children);
     }
 
     @Override
-    public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
-        return unwrap(null).getSimpleNodesByName(children);
+    public List<SimpleNode<?>> getSimpleNodesByName(final QName children) {
+        return unwrap().getSimpleNodesByName(children);
     }
 
     @Override
-    public List<SimpleNode<?>> getSimpleNodesByName(String children) {
-        return unwrap(null).getSimpleNodesByName(children);
+    public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
+        return unwrap().getSimpleNodesByName(children);
     }
 
     @Override
-    public CompositeNode getFirstCompositeByName(QName container) {
-        return unwrap(null).getFirstCompositeByName(container);
+    public CompositeNode getFirstCompositeByName(final QName container) {
+        return unwrap().getFirstCompositeByName(container);
     }
 
     @Override
-    public SimpleNode<?> getFirstSimpleByName(QName leaf) {
-        return unwrap(null).getFirstSimpleByName(leaf);
+    public SimpleNode<?> getFirstSimpleByName(final QName leaf) {
+        return unwrap().getFirstSimpleByName(leaf);
     }
 
     @Override
     public MutableCompositeNode asMutable() {
-        return unwrap(null).asMutable();
+        return unwrap().asMutable();
     }
 
     @Override
     public QName getKey() {
-        return unwrap(null).getKey();
+        return unwrap().getKey();
     }
 
     @Override
-    public List<Node<?>> setValue(List<Node<?>> value) {
-        return unwrap(null).setValue(value);
+    public List<Node<?>> setValue(final List<Node<?>> value) {
+        return unwrap().setValue(value);
     }
 
     @Override
     public int size() {
-        return unwrap(null).size();
+        return unwrap().size();
     }
 
     @Override
     public boolean isEmpty() {
-        return unwrap(null).isEmpty();
+        return unwrap().isEmpty();
     }
 
     @Override
-    public boolean containsKey(Object key) {
-        return unwrap(null).containsKey(key);
+    public boolean containsKey(final Object key) {
+        return unwrap().containsKey(key);
     }
 
     @Override
-    public boolean containsValue(Object value) {
-        return unwrap(null).containsValue(value);
+    public boolean containsValue(final Object value) {
+        return unwrap().containsValue(value);
     }
 
     @Override
-    public List<Node<?>> get(Object key) {
-        return unwrap(null).get(key);
+    public List<Node<?>> get(final Object key) {
+        return unwrap().get(key);
     }
 
     @Override
-    public List<Node<?>> put(QName key, List<Node<?>> value) {
-        return unwrap(null).put(key, value);
+    public List<Node<?>> put(final QName key, final List<Node<?>> value) {
+        return unwrap().put(key, value);
     }
 
     @Override
-    public List<Node<?>> remove(Object key) {
-        return unwrap(null).remove(key);
+    public List<Node<?>> remove(final Object key) {
+        return unwrap().remove(key);
     }
 
     @Override
-    public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
-        unwrap(null).putAll(m);
+    public void putAll(final Map<? extends QName, ? extends List<Node<?>>> m) {
+        unwrap().putAll(m);
     }
 
     @Override
     public void clear() {
-        unwrap(null).clear();
+        unwrap().clear();
     }
 
     @Override
     public Set<QName> keySet() {
-        return unwrap(null).keySet();
+        return unwrap().keySet();
     }
 
     @Override
     public Collection<List<Node<?>>> values() {
-        return unwrap(null).values();
+        return unwrap().values();
     }
 
     @Override
     public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
-        return unwrap(null).entrySet();
+        return unwrap().entrySet();
     }
 
 }