Merge "Added XmlTreeBuilder into yang-data-impl"
[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 java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.TreeMap;
17 import java.util.TreeSet;
18
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.UsesNode;
27 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
32 import org.opendaylight.yangtools.yang.parser.util.Comparators;
33 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
34
35 public final class GroupingBuilderImpl extends AbstractDataNodeContainerBuilder implements GroupingBuilder {
36     private boolean isBuilt;
37     private final GroupingDefinitionImpl instance;
38     private SchemaPath schemaPath;
39     private String description;
40     private String reference;
41     private Status status = Status.CURRENT;
42     private boolean addedByUses;
43
44     private Set<TypeDefinition<?>> typedefs;
45     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
46
47     private Set<UsesNode> usesNodes;
48     private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
49
50     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname) {
51         super(moduleName, line, qname);
52         instance = new GroupingDefinitionImpl(qname);
53     }
54
55     @Override
56     public GroupingDefinition build() {
57         if (!isBuilt) {
58             instance.setPath(schemaPath);
59             instance.setDescription(description);
60             instance.setReference(reference);
61             instance.setStatus(status);
62             instance.setAddedByUses(addedByUses);
63
64             // CHILD NODES
65             final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
66             if (childNodes == null || childNodes.isEmpty()) {
67                 for (DataSchemaNodeBuilder node : addedChildNodes) {
68                     childs.put(node.getQName(), node.build());
69                 }
70             } else {
71                 for (DataSchemaNode node : childNodes) {
72                     childs.put(node.getQName(), node);
73                 }
74             }
75             instance.setChildNodes(childs);
76
77             // GROUPINGS
78             if (groupings == null) {
79                 groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
80                 for (GroupingBuilder builder : addedGroupings) {
81                     groupings.add(builder.build());
82                 }
83             }
84             instance.setGroupings(groupings);
85
86             // TYPEDEFS
87             if (typedefs == null) {
88                 typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
89                 for (TypeDefinitionBuilder entry : addedTypedefs) {
90                     typedefs.add(entry.build());
91                 }
92             }
93             instance.setTypeDefinitions(typedefs);
94
95             // USES
96             if (usesNodes == null) {
97                 usesNodes = new HashSet<UsesNode>();
98                 for (UsesNodeBuilder builder : addedUsesNodes) {
99                     usesNodes.add(builder.build());
100                 }
101             }
102             instance.setUses(usesNodes);
103
104             // UNKNOWN NODES
105             if (unknownNodes == null) {
106                 unknownNodes = new ArrayList<UnknownSchemaNode>();
107                 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
108                     unknownNodes.add(b.build());
109                 }
110                 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
111             }
112             instance.setUnknownSchemaNodes(unknownNodes);
113
114             isBuilt = true;
115         }
116
117         return instance;
118     }
119
120
121     @Override
122     public void setQName(QName qname) {
123         this.qname = qname;
124     }
125
126     @Override
127     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
128         return addedTypedefs;
129     }
130
131     @Override
132     public void addTypedef(final TypeDefinitionBuilder type) {
133         String typeName = type.getQName().getLocalName();
134         for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
135             throw new YangParseException(moduleName, type.getLine(), "Can not add typedef '" + typeName
136                     + "': typedef with same name already declared at line " + addedTypedef.getLine());
137         }
138         addedTypedefs.add(type);
139     }
140
141     public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
142         this.typedefs = typedefs;
143     }
144
145     @Override
146     public SchemaPath getPath() {
147         return schemaPath;
148     }
149
150     @Override
151     public void setPath(SchemaPath schemaPath) {
152         this.schemaPath = schemaPath;
153     }
154
155     @Override
156     public String getDescription() {
157         return description;
158     }
159
160     @Override
161     public void setDescription(final String description) {
162         this.description = description;
163     }
164
165     @Override
166     public String getReference() {
167         return reference;
168     }
169
170     @Override
171     public void setReference(final String reference) {
172         this.reference = reference;
173     }
174
175     @Override
176     public Status getStatus() {
177         return status;
178     }
179
180     @Override
181     public void setStatus(final Status status) {
182         this.status = status;
183     }
184
185     @Override
186     public boolean isAddedByUses() {
187         return addedByUses;
188     }
189
190     @Override
191     public void setAddedByUses(final boolean addedByUses) {
192         this.addedByUses = addedByUses;
193     }
194
195     @Override
196     public Set<UsesNodeBuilder> getUsesNodes() {
197         return addedUsesNodes;
198     }
199
200     @Override
201     public void addUsesNode(final UsesNodeBuilder usesBuilder) {
202         addedUsesNodes.add(usesBuilder);
203     }
204
205     public void setUsesnodes(final Set<UsesNode> usesNodes) {
206         this.usesNodes = usesNodes;
207     }
208
209     @Override
210     public String toString() {
211         return "grouping " + qname.getLocalName();
212     }
213
214     @Override
215     public int hashCode() {
216         final int prime = 31;
217         int result = 1;
218         result = prime * result + ((parent == null) ? 0 : parent.hashCode());
219         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
220         return result;
221     }
222
223     @Override
224     public boolean equals(Object obj) {
225         if (this == obj) {
226             return true;
227         }
228         if (obj == null) {
229             return false;
230         }
231         if (getClass() != obj.getClass()) {
232             return false;
233         }
234         if (!super.equals(obj)) {
235             return false;
236         }
237         final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
238         if (parent == null) {
239             if (other.parent != null) {
240                 return false;
241             }
242         } else if (!parent.equals(other.parent)) {
243             return false;
244         }
245         if (schemaPath == null) {
246             if (other.schemaPath != null) {
247                 return false;
248             }
249         } else if (!schemaPath.equals(other.schemaPath)) {
250             return false;
251         }
252         return true;
253     }
254
255
256     private final class GroupingDefinitionImpl implements GroupingDefinition {
257         private final QName qname;
258         private SchemaPath path;
259         private String description;
260         private String reference;
261         private Status status;
262         private boolean addedByUses;
263         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
264         private Set<GroupingDefinition> groupings = Collections.emptySet();
265         private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
266         private Set<UsesNode> uses = Collections.emptySet();
267         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
268
269         private GroupingDefinitionImpl(final QName qname) {
270             this.qname = qname;
271         }
272
273         @Override
274         public QName getQName() {
275             return qname;
276         }
277
278         @Override
279         public SchemaPath getPath() {
280             return path;
281         }
282
283         private void setPath(SchemaPath path) {
284             this.path = path;
285         }
286
287         @Override
288         public String getDescription() {
289             return description;
290         }
291
292         private void setDescription(String description) {
293             this.description = description;
294         }
295
296         @Override
297         public String getReference() {
298             return reference;
299         }
300
301         private void setReference(String reference) {
302             this.reference = reference;
303         }
304
305         @Override
306         public Status getStatus() {
307             return status;
308         }
309
310         private void setStatus(Status status) {
311             this.status = status;
312         }
313
314         @Override
315         public boolean isAddedByUses() {
316             return addedByUses;
317         }
318
319         private void setAddedByUses(final boolean addedByUses) {
320             this.addedByUses = addedByUses;
321         }
322
323         @Override
324         public Set<DataSchemaNode> getChildNodes() {
325             final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
326             result.addAll(childNodes.values());
327             return result;
328         }
329
330         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
331             this.childNodes = childNodes;
332         }
333
334         @Override
335         public Set<GroupingDefinition> getGroupings() {
336             return groupings;
337         }
338
339         private void setGroupings(Set<GroupingDefinition> groupings) {
340             this.groupings = groupings;
341         }
342
343         @Override
344         public Set<UsesNode> getUses() {
345             return uses;
346         }
347
348         private void setUses(Set<UsesNode> uses) {
349             this.uses = uses;
350         }
351
352         @Override
353         public Set<TypeDefinition<?>> getTypeDefinitions() {
354             return typeDefinitions;
355         }
356
357         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
358             this.typeDefinitions = typeDefinitions;
359         }
360
361         @Override
362         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
363             return unknownNodes;
364         }
365
366         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
367             if (unknownNodes != null) {
368                 this.unknownNodes = unknownNodes;
369             }
370         }
371
372         @Override
373         public DataSchemaNode getDataChildByName(QName name) {
374             return childNodes.get(name);
375         }
376
377         @Override
378         public DataSchemaNode getDataChildByName(String name) {
379             DataSchemaNode result = null;
380             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
381                 if (entry.getKey().getLocalName().equals(name)) {
382                     result = entry.getValue();
383                     break;
384                 }
385             }
386             return result;
387         }
388
389         @Override
390         public int hashCode() {
391             final int prime = 31;
392             int result = 1;
393             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
394             result = prime * result + ((path == null) ? 0 : path.hashCode());
395             return result;
396         }
397
398         @Override
399         public boolean equals(Object obj) {
400             if (this == obj) {
401                 return true;
402             }
403             if (obj == null) {
404                 return false;
405             }
406             if (getClass() != obj.getClass()) {
407                 return false;
408             }
409             final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
410             if (qname == null) {
411                 if (other.qname != null) {
412                     return false;
413                 }
414             } else if (!qname.equals(other.qname)) {
415                 return false;
416             }
417             if (path == null) {
418                 if (other.path != null) {
419                     return false;
420                 }
421             } else if (!path.equals(other.path)) {
422                 return false;
423             }
424             return true;
425         }
426
427         @Override
428         public String toString() {
429             StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
430             sb.append("[");
431             sb.append("qname=" + qname);
432             sb.append("]");
433             return sb.toString();
434         }
435     }
436
437 }