Merge "Fix checkstyle if-statements must use braces in binding-generator"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / CompositeNodeTOImpl.java
index 33779221e8d35f7208f148c87ffdbee13cd6eb7f..3e59659d2e106eb397ef5fefa429df967ba7baa4 100644 (file)
@@ -7,6 +7,10 @@
  */
 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;
@@ -21,12 +25,17 @@ 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 = new HashMap<>();
 
@@ -36,7 +45,7 @@ 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);
         init();
     }
@@ -48,11 +57,11 @@ 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();
     }
-    
+
     protected void init() {
         if (getValue() != null) {
             nodeMap = NodeUtils.buildNodeMap(getValue());
@@ -69,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;
@@ -78,7 +87,7 @@ 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();
@@ -93,7 +102,7 @@ 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();
@@ -109,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;
@@ -121,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;
@@ -130,13 +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));
+    public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
+        return getSimpleNodesByName(QName.create(getNodeType(), children));
     }
 
     @Override
@@ -146,33 +155,28 @@ public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implement
 
     @Override
     public String toString() {
-        return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
+        return super.toString() + ", children.size = " + (!getChildren().isEmpty() ? 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() {
@@ -185,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);
     }
 
@@ -213,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());
+    }
 }