Fix AugmentationSchemaProxy.getDataChildByName()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / CompositeNodeTOImpl.java
index 48d25523f3cebc3825ba2a8a0ce1c731ff855507..7d5b8bb330e8cd46966d4213ab62e1f0b58dbfc4 100644 (file)
@@ -7,9 +7,14 @@
  */
 package org.opendaylight.yangtools.yang.data.impl;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -20,14 +25,19 @@ import org.opendaylight.yangtools.yang.data.api.ModifyAction;
 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
 import org.opendaylight.yangtools.yang.data.api.Node;
 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 
 /**
  * @author michal.rehak
- * 
+ *
+ * @deprecated Use one of the {@link NormalizedNodeContainer} implementations.
  */
-public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implements CompositeNode {
+@Deprecated
+public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implements CompositeNode, Serializable {
+
+    private static final long serialVersionUID = 100L;
 
-    private Map<QName, List<Node<?>>> nodeMap;
+    private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
 
     /**
      * @param qname
@@ -35,11 +45,8 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
      *            use null to create top composite node (without parent)
      * @param value
      */
-    public CompositeNodeTOImpl(QName qname, CompositeNode parent, List<Node<?>> value) {
+    public CompositeNodeTOImpl(final QName qname, final CompositeNode parent, final List<Node<?>> value) {
         super(qname, parent, value);
-        if (value != null) {
-            nodeMap = NodeUtils.buildNodeMap(getValue());
-        }
         init();
     }
 
@@ -50,14 +57,17 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
      * @param value
      * @param modifyAction
      */
-    public CompositeNodeTOImpl(QName qname, CompositeNode parent, List<Node<?>> value, ModifyAction modifyAction) {
+    public CompositeNodeTOImpl(final QName qname, final CompositeNode parent, final List<Node<?>> value, final ModifyAction modifyAction) {
         super(qname, parent, value, modifyAction);
         init();
     }
 
-    /**
-     * @return the nodeMap
-     */
+    protected void init() {
+        if (getValue() != null) {
+            nodeMap = NodeUtils.buildNodeMap(getValue());
+        }
+    }
+
     protected Map<QName, List<Node<?>>> getNodeMap() {
         return nodeMap;
     }
@@ -68,7 +78,7 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
     }
 
     @Override
-    public SimpleNode<?> getFirstSimpleByName(QName leafQName) {
+    public SimpleNode<?> getFirstSimpleByName(final QName leafQName) {
         List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
         if (list.isEmpty()) {
             return null;
@@ -77,8 +87,11 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
     }
 
     @Override
-    public List<CompositeNode> getCompositesByName(QName children) {
+    public List<CompositeNode> getCompositesByName(final QName children) {
         List<Node<?>> toFilter = getNodeMap().get(children);
+        if(toFilter == null) {
+            return Collections.emptyList();
+        }
         List<CompositeNode> list = new ArrayList<CompositeNode>();
         for (Node<?> node : toFilter) {
             if (node instanceof CompositeNode) {
@@ -89,8 +102,11 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
     }
 
     @Override
-    public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
+    public List<SimpleNode<?>> getSimpleNodesByName(final QName children) {
         List<Node<?>> toFilter = getNodeMap().get(children);
+        if(toFilter == null) {
+            return Collections.emptyList();
+        }
         List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
 
         for (Node<?> node : toFilter) {
@@ -102,7 +118,7 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
     }
 
     @Override
-    public CompositeNode getFirstCompositeByName(QName container) {
+    public CompositeNode getFirstCompositeByName(final QName container) {
         List<CompositeNode> list = getCompositesByName(container);
         if (list.isEmpty()) {
             return null;
@@ -114,7 +130,7 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
      * @param leaf
      * @return TODO:: do we need this method?
      */
-    public SimpleNode<?> getFirstLeafByName(QName leaf) {
+    public SimpleNode<?> getFirstLeafByName(final QName leaf) {
         List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
         if (list.isEmpty()) {
             return null;
@@ -123,19 +139,13 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
     }
 
     @Override
-    public List<CompositeNode> getCompositesByName(String children) {
-        return getCompositesByName(new QName(getNodeType(), children));
+    public List<CompositeNode> getCompositesByName(final String children) {
+        return getCompositesByName(QName.create(getNodeType(), children));
     }
 
     @Override
-    public List<SimpleNode<?>> getSimpleNodesByName(String children) {
-        return getSimpleNodesByName(new QName(getNodeType(), children));
-    }
-
-    protected void init() {
-        if (getValue() != null) {
-            nodeMap = NodeUtils.buildNodeMap(getValue());
-        }
+    public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
+        return getSimpleNodesByName(QName.create(getNodeType(), children));
     }
 
     @Override
@@ -147,31 +157,26 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
     public String toString() {
         return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
     }
-    
+
     @Override
     public void clear() {
         nodeMap.clear();
     }
-    
+
     @Override
-    public boolean containsKey(Object key) {
+    public boolean containsKey(final Object key) {
         return nodeMap.containsKey(key);
     }
-    
+
     @Override
-    public boolean containsValue(Object value) {
+    public boolean containsValue(final Object value) {
         return nodeMap.containsValue(value);
     }
-    
+
     @Override
     public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
         return nodeMap.entrySet();
     }
-    
-    @Override
-    public boolean equals(Object obj) {
-        return super.equals(obj);
-    }
 
     @Override
     public int size() {
@@ -184,22 +189,22 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
     }
 
     @Override
-    public List<Node<?>> get(Object key) {
+    public List<Node<?>> get(final Object key) {
         return nodeMap.get(key);
     }
 
     @Override
-    public List<Node<?>> put(QName key, List<Node<?>> value) {
+    public List<Node<?>> put(final QName key, final List<Node<?>> value) {
         return nodeMap.put(key, value);
     }
 
     @Override
-    public List<Node<?>> remove(Object key) {
+    public List<Node<?>> remove(final Object key) {
         return nodeMap.remove(key);
     }
 
     @Override
-    public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
+    public void putAll(final Map<? extends QName, ? extends List<Node<?>>> m) {
         nodeMap.putAll(m);
     }
 
@@ -212,4 +217,26 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
     public Collection<List<Node<?>>> values() {
         return nodeMap.values();
     }
+
+    // Serialization related
+
+    private void readObject(final ObjectInputStream aStream) throws IOException, ClassNotFoundException {
+        aStream.defaultReadObject();
+        QName qName = (QName)aStream.readObject();
+        CompositeNode parent = (CompositeNode) aStream.readObject();
+        @SuppressWarnings("unchecked")
+        List<Node<?>> value = (List<Node<?>>) aStream.readObject();
+        ModifyAction modifyAction = (ModifyAction) aStream.readObject();
+
+        init(qName, parent, value, modifyAction);
+    }
+
+    private void writeObject(final ObjectOutputStream aStream) throws IOException {
+        aStream.defaultWriteObject();
+        //manually serialize superclass
+        aStream.writeObject(getQName());
+        aStream.writeObject(getParent());
+        aStream.writeObject(getValue());
+        aStream.writeObject(getModificationAction());
+    }
 }