Merge "BUG-1460: make sure DCL works"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / CompositeNodeTOImpl.java
index 5ba7ed8b3c2853c3fc2f4786a8faa2ea7e58c25f..9fc19d70f6a73b73e995ff95e9e6fcf65155b246 100644 (file)
@@ -7,9 +7,17 @@
  */
 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;
 
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
@@ -20,11 +28,13 @@ import org.opendaylight.yangtools.yang.data.api.SimpleNode;
 
 /**
  * @author michal.rehak
- * 
+ *
  */
-public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implements CompositeNode {
+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
@@ -32,11 +42,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();
     }
 
@@ -47,25 +54,28 @@ 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;
     }
 
     @Override
     public List<Node<?>> getChildren() {
-        return getValue();
+        return Collections.unmodifiableList(getValue() == null ? new ArrayList<Node<?>>() : getValue());
     }
 
     @Override
-    public SimpleNode<?> getFirstSimpleByName(QName leafQName) {
+    public SimpleNode<?> getFirstSimpleByName(final QName leafQName) {
         List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
         if (list.isEmpty()) {
             return null;
@@ -74,8 +84,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) {
@@ -86,19 +99,23 @@ 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) {
-            if (node instanceof SimpleNode<?>)
+            if (node instanceof SimpleNode<?>) {
                 list.add((SimpleNode<?>) node);
+            }
         }
         return list;
     }
 
     @Override
-    public CompositeNode getFirstCompositeByName(QName container) {
+    public CompositeNode getFirstCompositeByName(final QName container) {
         List<CompositeNode> list = getCompositesByName(container);
         if (list.isEmpty()) {
             return null;
@@ -110,7 +127,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;
@@ -119,22 +136,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));
-    }
-
-    /**
-     * @param value
-     */
-    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,4 +155,85 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
         return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
     }
 
+    @Override
+    public void clear() {
+        nodeMap.clear();
+    }
+
+    @Override
+    public boolean containsKey(final Object key) {
+        return nodeMap.containsKey(key);
+    }
+
+    @Override
+    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 int size() {
+        return nodeMap.size();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return nodeMap.isEmpty();
+    }
+
+    @Override
+    public List<Node<?>> get(final Object key) {
+        return nodeMap.get(key);
+    }
+
+    @Override
+    public List<Node<?>> put(final QName key, final List<Node<?>> value) {
+        return nodeMap.put(key, value);
+    }
+
+    @Override
+    public List<Node<?>> remove(final Object key) {
+        return nodeMap.remove(key);
+    }
+
+    @Override
+    public void putAll(final Map<? extends QName, ? extends List<Node<?>>> m) {
+        nodeMap.putAll(m);
+    }
+
+    @Override
+    public Set<QName> keySet() {
+        return nodeMap.keySet();
+    }
+
+    @Override
+    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());
+    }
 }