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