Merge "BUG-979: reworked data schema nodes ordering"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / GroupingBuilderImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
26 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
27
28 public final class GroupingBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements GroupingBuilder {
29     private GroupingDefinitionImpl instance;
30     // SchemaNode args
31     private SchemaPath schemaPath;
32     // DataSchemaNode args
33     private boolean addedByUses;
34
35     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
36         super(moduleName, line, qname);
37         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
38     }
39
40     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path,
41             final GroupingDefinition base) {
42         super(moduleName, line, base.getQName(), Preconditions.checkNotNull(path, "Schema Path must not be null"), base);
43         schemaPath = path;
44         addedByUses = base.isAddedByUses();
45         addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
46                 qname));
47     }
48
49     @Override
50     public GroupingDefinition build() {
51         if (instance != null) {
52             return instance;
53         }
54         buildChildren();
55         instance = new GroupingDefinitionImpl(qname, schemaPath, this);
56         instance.addedByUses = addedByUses;
57
58         // UNKNOWN NODES
59         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
60             unknownNodes.add(b.build());
61         }
62         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
63
64         return instance;
65     }
66
67     @Override
68     public List<DataSchemaNodeBuilder> instantiateChildNodes(final Builder newParent) {
69         final List<DataSchemaNodeBuilder> nodes = new ArrayList<>();
70         for (DataSchemaNodeBuilder node : getChildNodeBuilders()) {
71             DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
72             BuilderUtils.setNodeAddedByUses(copy);
73             nodes.add(copy);
74         }
75         return nodes;
76     }
77
78     @Override
79     public Set<TypeDefinitionBuilder> instantiateTypedefs(final Builder newParent) {
80         final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
81         for (TypeDefinitionBuilder node : getTypeDefinitionBuilders()) {
82             TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
83             nodes.add(copy);
84         }
85         return nodes;
86     }
87
88     @Override
89     public Set<GroupingBuilder> instantiateGroupings(final Builder newParent) {
90         final Set<GroupingBuilder> nodes = new HashSet<>();
91         for (GroupingBuilder node : getGroupingBuilders()) {
92             GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
93             copy.setAddedByUses(true);
94             for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
95                 BuilderUtils.setNodeAddedByUses(childNode);
96             }
97             nodes.add(copy);
98         }
99         return nodes;
100     }
101
102     @Override
103     public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(final Builder newParent) {
104         final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
105         for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
106             UnknownSchemaNodeBuilderImpl copy = CopyUtils.copy(node, newParent, true);
107             copy.setAddedByUses(true);
108             nodes.add(copy);
109         }
110         return nodes;
111     }
112
113     @Override
114     public SchemaPath getPath() {
115         return schemaPath;
116     }
117
118     @Override
119     public void setPath(final SchemaPath path) {
120         this.schemaPath = path;
121     }
122
123     @Override
124     public boolean isAddedByUses() {
125         return addedByUses;
126     }
127
128     @Override
129     public void setAddedByUses(final boolean addedByUses) {
130         this.addedByUses = addedByUses;
131     }
132
133     @Override
134     public String toString() {
135         return "grouping " + qname.getLocalName();
136     }
137
138     @Override
139     public int hashCode() {
140         final int prime = 31;
141         int result = 1;
142         result = prime * result + ((getParent() == null) ? 0 : getParent().hashCode());
143         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
144         return result;
145     }
146
147     @Override
148     public boolean equals(final Object obj) {
149         if (this == obj) {
150             return true;
151         }
152         if (obj == null) {
153             return false;
154         }
155         if (getClass() != obj.getClass()) {
156             return false;
157         }
158         if (!super.equals(obj)) {
159             return false;
160         }
161         final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
162         if (getParent() == null) {
163             if (other.getParent() != null) {
164                 return false;
165             }
166         } else if (!getParent().equals(other.getParent())) {
167             return false;
168         }
169         if (schemaPath == null) {
170             if (other.schemaPath != null) {
171                 return false;
172             }
173         } else if (!schemaPath.equals(other.schemaPath)) {
174             return false;
175         }
176         return true;
177     }
178
179     @Override
180     protected String getStatementName() {
181         return "grouping";
182     }
183
184     private static final class GroupingDefinitionImpl extends AbstractDocumentedDataNodeContainer implements
185     GroupingDefinition {
186         private final QName qname;
187         private final SchemaPath path;
188
189         private boolean addedByUses;
190         private ImmutableList<UnknownSchemaNode> unknownNodes;
191
192         private GroupingDefinitionImpl(final QName qname, final SchemaPath path, final GroupingBuilderImpl builder) {
193             super(builder);
194             this.qname = qname;
195             this.path = path;
196         }
197
198         @Override
199         public QName getQName() {
200             return qname;
201         }
202
203         @Override
204         public SchemaPath getPath() {
205             return path;
206         }
207
208         @Override
209         public boolean isAddedByUses() {
210             return addedByUses;
211         }
212
213         @Override
214         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
215             return unknownNodes;
216         }
217
218         @Override
219         public int hashCode() {
220             final int prime = 31;
221             int result = 1;
222             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
223             result = prime * result + ((path == null) ? 0 : path.hashCode());
224             return result;
225         }
226
227         @Override
228         public boolean equals(final Object obj) {
229             if (this == obj) {
230                 return true;
231             }
232             if (obj == null) {
233                 return false;
234             }
235             if (getClass() != obj.getClass()) {
236                 return false;
237             }
238             final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
239             if (qname == null) {
240                 if (other.qname != null) {
241                     return false;
242                 }
243             } else if (!qname.equals(other.qname)) {
244                 return false;
245             }
246             if (path == null) {
247                 if (other.path != null) {
248                     return false;
249                 }
250             } else if (!path.equals(other.path)) {
251                 return false;
252             }
253             return true;
254         }
255
256         @Override
257         public String toString() {
258             StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
259             sb.append("[");
260             sb.append("qname=").append(qname);
261             sb.append("]");
262             return sb.toString();
263         }
264     }
265
266 }