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