Refactored base yang-java types.
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / DataNodeIterator.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.yangtools.yang.model.util;\r
9 \r
10 import java.util.ArrayList;\r
11 import java.util.Iterator;\r
12 import java.util.List;\r
13 import java.util.Set;\r
14 \r
15 import org.opendaylight.yangtools.yang.model.api.*;\r
16 \r
17 public class DataNodeIterator implements Iterator<DataSchemaNode> {\r
18 \r
19     private final DataNodeContainer container;\r
20     private final List<ListSchemaNode> allLists;\r
21     private final List<ContainerSchemaNode> allContainers;\r
22     private final List<ChoiceNode> allChoices;\r
23     private final List<DataSchemaNode> allChilds;\r
24     \r
25     public DataNodeIterator(final DataNodeContainer container) {\r
26         if (container == null) {\r
27             throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");\r
28         }\r
29 \r
30         this.allContainers = new ArrayList<>();\r
31         this.allLists = new ArrayList<>();\r
32         this.allChilds = new ArrayList<>();\r
33         this.allChoices = new ArrayList<>();\r
34 \r
35         this.container = container;\r
36         traverse(this.container);\r
37     }\r
38 \r
39     public List<ContainerSchemaNode> allContainers() {\r
40         return allContainers;\r
41     }\r
42 \r
43     public List<ListSchemaNode> allLists() {\r
44         return allLists;\r
45     }\r
46 \r
47     public List<ChoiceNode> allChoices() {\r
48         return allChoices;\r
49     }\r
50 \r
51     private void traverse(final DataNodeContainer dataNode) {\r
52         if (dataNode == null) {\r
53             return;\r
54         }\r
55 \r
56         final Set<DataSchemaNode> childNodes = dataNode.getChildNodes();\r
57         if (childNodes != null) {\r
58             for (DataSchemaNode childNode : childNodes) {\r
59                 if (childNode.isAugmenting()) {\r
60                     continue;\r
61                 }\r
62                 allChilds.add(childNode);\r
63                 if (childNode instanceof ContainerSchemaNode) {\r
64                     final ContainerSchemaNode container = (ContainerSchemaNode) childNode;\r
65                     allContainers.add(container);\r
66                     traverse(container);\r
67                 } else if (childNode instanceof ListSchemaNode) {\r
68                     final ListSchemaNode list = (ListSchemaNode) childNode;\r
69                     allLists.add(list);\r
70                     traverse(list);\r
71                 } else if (childNode instanceof ChoiceNode) {\r
72                     final ChoiceNode choiceNode = (ChoiceNode) childNode;\r
73                     allChoices.add(choiceNode);\r
74                     final Set<ChoiceCaseNode> cases = choiceNode.getCases();\r
75                     if (cases != null) {\r
76                         for (final ChoiceCaseNode caseNode : cases) {\r
77                             traverse(caseNode);\r
78                         }\r
79                     }\r
80                 }\r
81             }\r
82         }\r
83 \r
84         if(dataNode instanceof Module) {\r
85             traverseModule((Module)dataNode);\r
86         }\r
87         \r
88         final Set<GroupingDefinition> groupings = dataNode.getGroupings();\r
89         if (groupings != null) {\r
90             for (GroupingDefinition grouping : groupings) {\r
91                 traverse(grouping);\r
92             }\r
93         }\r
94     }\r
95     \r
96 \r
97     private void traverseModule(Module module) {\r
98         final Set<NotificationDefinition> notifications = module.getNotifications();\r
99         for (NotificationDefinition notificationDefinition : notifications) {\r
100             traverse(notificationDefinition);\r
101         }\r
102         final Set<RpcDefinition> rpcs = module.getRpcs();\r
103         for (RpcDefinition rpcDefinition : rpcs) {\r
104             ContainerSchemaNode input = rpcDefinition.getInput();\r
105             if(input != null) {\r
106                 traverse(input);\r
107             }\r
108             ContainerSchemaNode output = rpcDefinition.getInput();\r
109             if(input != null) {\r
110                 traverse(output);\r
111             }\r
112         }\r
113     }\r
114     \r
115     @Override\r
116     public boolean hasNext() {\r
117         if (container.getChildNodes() != null) {\r
118             final Set<DataSchemaNode> childNodes = container.getChildNodes();\r
119 \r
120             if ((childNodes != null) && !childNodes.isEmpty()) {\r
121                 return childNodes.iterator().hasNext();\r
122             }\r
123         }\r
124         return false;\r
125     }\r
126 \r
127     @Override\r
128     public DataSchemaNode next() {\r
129         return allChilds.iterator().next();\r
130     }\r
131 \r
132     @Override\r
133     public void remove() {\r
134         throw new UnsupportedOperationException();\r
135     }\r
136 }\r