Merge "Fixed bug in GeneratedPropertyBuilderImpl."
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / yang / model / util / DataNodeIterator.java
index ded4a75179113327683a084bf15425b7b3f91d8d..d55f84923c4fd6dae5cf8bf58b08c18ba9f433f0 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * 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.controller.yang.model.util;
 
 import java.util.ArrayList;
@@ -5,62 +12,53 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
-import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.controller.yang.model.api.DataNodeContainer;
-import org.opendaylight.controller.yang.model.api.DataSchemaNode;
-import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
-import org.opendaylight.controller.yang.model.api.ListSchemaNode;
+import org.opendaylight.controller.yang.model.api.*;
 
 public class DataNodeIterator implements Iterator<DataSchemaNode> {
-    
+
     private final DataNodeContainer container;
     private final List<ListSchemaNode> allLists;
     private final List<ContainerSchemaNode> allContainers;
-    private final List<LeafSchemaNode> allLeafs;
-    private final List<LeafListSchemaNode> allLeafLists;
+    private final List<ChoiceNode> allChoices;
     private final List<DataSchemaNode> allChilds;
-    
+
     public DataNodeIterator(final DataNodeContainer container) {
         if (container == null) {
-            throw new IllegalArgumentException("Data Node Container MUST be specified!");
+            throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");
         }
-        
-        this.allContainers = new ArrayList<ContainerSchemaNode>();
-        this.allLists = new ArrayList<ListSchemaNode>();
-        this.allLeafs = new ArrayList<LeafSchemaNode>();
-        this.allLeafLists = new ArrayList<LeafListSchemaNode>();
-        this.allChilds = new ArrayList<DataSchemaNode>();
-        
+
+        this.allContainers = new ArrayList<>();
+        this.allLists = new ArrayList<>();
+        this.allChilds = new ArrayList<>();
+        this.allChoices = new ArrayList<>();
+
         this.container = container;
-        
         traverse(this.container);
     }
-    
+
     public List<ContainerSchemaNode> allContainers() {
         return allContainers;
     }
-    
+
     public List<ListSchemaNode> allLists() {
         return allLists;
     }
-    
-    public List<LeafSchemaNode> allLeafs() {
-        return allLeafs;
-    }
-    
-    public List<LeafListSchemaNode> allLeafLists() {
-        return allLeafLists;
+
+    public List<ChoiceNode> allChoices() {
+        return allChoices;
     }
-    
+
     private void traverse(final DataNodeContainer dataNode) {
-        if (!containChildDataNodeContainer(dataNode)) {
+        if (dataNode == null) {
             return;
         }
 
-        final Set<DataSchemaNode> childs = dataNode.getChildNodes();
-        if (childs != null) {
-            for (DataSchemaNode childNode : childs) {
+        final Set<DataSchemaNode> childNodes = dataNode.getChildNodes();
+        if (childNodes != null) {
+            for (DataSchemaNode childNode : childNodes) {
+                if (childNode.isAugmenting()) {
+                    continue;
+                }
                 allChilds.add(childNode);
                 if (childNode instanceof ContainerSchemaNode) {
                     final ContainerSchemaNode container = (ContainerSchemaNode) childNode;
@@ -70,49 +68,34 @@ public class DataNodeIterator implements Iterator<DataSchemaNode> {
                     final ListSchemaNode list = (ListSchemaNode) childNode;
                     allLists.add(list);
                     traverse(list);
-                } else if (childNode instanceof LeafSchemaNode) {
-                    final LeafSchemaNode leaf = (LeafSchemaNode) childNode;
-                    allLeafs.add(leaf);
-                } else if (childNode instanceof LeafListSchemaNode) {
-                    final LeafListSchemaNode leafList = (LeafListSchemaNode) childNode;
-                    allLeafLists.add(leafList);
+                } else if (childNode instanceof ChoiceNode) {
+                    final ChoiceNode choiceNode = (ChoiceNode) childNode;
+                    allChoices.add(choiceNode);
+                    final Set<ChoiceCaseNode> cases = choiceNode.getCases();
+                    if (cases != null) {
+                        for (final ChoiceCaseNode caseNode : cases) {
+                            traverse(caseNode);
+                        }
+                    }
                 }
             }
         }
-    }
 
-    /**
-     * Returns <code>true</code> if and only if the child node contain at least
-     * one child container schema node or child list schema node, otherwise will
-     * always returns <code>false</code>
-     * 
-     * @param container
-     * @return <code>true</code> if and only if the child node contain at least
-     *         one child container schema node or child list schema node,
-     *         otherwise will always returns <code>false</code>
-     */
-    private boolean containChildDataNodeContainer(
-            final DataNodeContainer container) {
-        if (container != null) {
-            final Set<DataSchemaNode> childs = container.getChildNodes();
-            if ((childs != null) && (childs.size() > 0)) {
-                for (final DataSchemaNode childNode : childs) {
-                    if (childNode instanceof DataNodeContainer) {
-                        return true;
-                    }
-                }
+        final Set<GroupingDefinition> groupings = dataNode.getGroupings();
+        if (groupings != null) {
+            for (GroupingDefinition grouping : groupings) {
+                traverse(grouping);
             }
         }
-        return false;
     }
-    
+
     @Override
     public boolean hasNext() {
         if (container.getChildNodes() != null) {
-            Set<DataSchemaNode> childs = container.getChildNodes();
-            
-            if ((childs != null) && !childs.isEmpty()) {
-                return childs.iterator().hasNext();
+            final Set<DataSchemaNode> childNodes = container.getChildNodes();
+
+            if ((childNodes != null) && !childNodes.isEmpty()) {
+                return childNodes.iterator().hasNext();
             }
         }
         return false;