Bug 1131 - yang-parser-impl cleanup
[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.net.URI;
13 import java.util.Date;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
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.UnknownSchemaNode;
22 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
28 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
29
30 public final class GroupingBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements GroupingBuilder {
31     private GroupingDefinitionImpl instance;
32     // SchemaNode args
33     private SchemaPath schemaPath;
34     private String description;
35     private String reference;
36     private Status status = Status.CURRENT;
37     // DataSchemaNode args
38     private boolean addedByUses;
39
40     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
41         super(moduleName, line, qname);
42         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
43     }
44
45     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path,
46             final GroupingDefinition base) {
47         super(moduleName, line, base.getQName(), path, base);
48         schemaPath = path;
49
50         description = base.getDescription();
51         reference = base.getReference();
52         status = base.getStatus();
53         addedByUses = base.isAddedByUses();
54
55         URI ns = qname.getNamespace();
56         Date rev = qname.getRevision();
57         String pref = qname.getPrefix();
58         addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
59                 ns, rev, pref));
60     }
61
62     @Override
63     public GroupingDefinition build() {
64         if (instance != null) {
65             return instance;
66         }
67         buildChildren();
68         instance = new GroupingDefinitionImpl(qname, schemaPath, this);
69         instance.addedByUses = addedByUses;
70
71         // UNKNOWN NODES
72         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
73             unknownNodes.add(b.build());
74         }
75         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
76
77         return instance;
78     }
79
80     @Override
81     public Set<DataSchemaNodeBuilder> instantiateChildNodes(final Builder newParent) {
82         final Set<DataSchemaNodeBuilder> nodes = new HashSet<>();
83         for (DataSchemaNodeBuilder node : getChildNodeBuilders()) {
84             DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
85             BuilderUtils.setNodeAddedByUses(copy);
86             nodes.add(copy);
87         }
88         return nodes;
89     }
90
91     @Override
92     public Set<TypeDefinitionBuilder> instantiateTypedefs(final Builder newParent) {
93         final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
94         for (TypeDefinitionBuilder node : getTypeDefinitionBuilders()) {
95             TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
96             nodes.add(copy);
97         }
98         return nodes;
99     }
100
101     @Override
102     public Set<GroupingBuilder> instantiateGroupings(final Builder newParent) {
103         final Set<GroupingBuilder> nodes = new HashSet<>();
104         for (GroupingBuilder node : getGroupingBuilders()) {
105             GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
106             copy.setAddedByUses(true);
107             for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
108                 BuilderUtils.setNodeAddedByUses(childNode);
109             }
110             nodes.add(copy);
111         }
112         return nodes;
113     }
114
115     @Override
116     public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(final Builder newParent) {
117         final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
118         for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
119             UnknownSchemaNodeBuilderImpl copy = CopyUtils.copy(node, newParent, true);
120             copy.setAddedByUses(true);
121             nodes.add(copy);
122         }
123         return nodes;
124     }
125
126     @Override
127     public SchemaPath getPath() {
128         return schemaPath;
129     }
130
131     @Override
132     public void setPath(final SchemaPath path) {
133         this.schemaPath = path;
134     }
135
136     @Override
137     public boolean isAddedByUses() {
138         return addedByUses;
139     }
140
141     @Override
142     public void setAddedByUses(final boolean addedByUses) {
143         this.addedByUses = addedByUses;
144     }
145
146     @Override
147     public String toString() {
148         return "grouping " + qname.getLocalName();
149     }
150
151     @Override
152     public int hashCode() {
153         final int prime = 31;
154         int result = 1;
155         result = prime * result + ((getParent() == null) ? 0 : getParent().hashCode());
156         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
157         return result;
158     }
159
160     @Override
161     public boolean equals(final Object obj) {
162         if (this == obj) {
163             return true;
164         }
165         if (obj == null) {
166             return false;
167         }
168         if (getClass() != obj.getClass()) {
169             return false;
170         }
171         if (!super.equals(obj)) {
172             return false;
173         }
174         final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
175         if (getParent() == null) {
176             if (other.getParent() != null) {
177                 return false;
178             }
179         } else if (!getParent().equals(other.getParent())) {
180             return false;
181         }
182         if (schemaPath == null) {
183             if (other.schemaPath != null) {
184                 return false;
185             }
186         } else if (!schemaPath.equals(other.schemaPath)) {
187             return false;
188         }
189         return true;
190     }
191
192     @Override
193     protected String getStatementName() {
194         return "grouping";
195     }
196
197     private static final class GroupingDefinitionImpl extends AbstractDocumentedDataNodeContainer implements
198             GroupingDefinition {
199         private final QName qname;
200         private final SchemaPath path;
201
202         private boolean addedByUses;
203         private ImmutableList<UnknownSchemaNode> unknownNodes;
204
205         private GroupingDefinitionImpl(final QName qname, final SchemaPath path, final GroupingBuilderImpl builder) {
206             super(builder);
207             this.qname = qname;
208             this.path = path;
209         }
210
211         @Override
212         public QName getQName() {
213             return qname;
214         }
215
216         @Override
217         public SchemaPath getPath() {
218             return path;
219         }
220
221         @Override
222         public boolean isAddedByUses() {
223             return addedByUses;
224         }
225
226         @Override
227         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
228             return unknownNodes;
229         }
230
231         @Override
232         public int hashCode() {
233             final int prime = 31;
234             int result = 1;
235             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
236             result = prime * result + ((path == null) ? 0 : path.hashCode());
237             return result;
238         }
239
240         @Override
241         public boolean equals(final Object obj) {
242             if (this == obj) {
243                 return true;
244             }
245             if (obj == null) {
246                 return false;
247             }
248             if (getClass() != obj.getClass()) {
249                 return false;
250             }
251             final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
252             if (qname == null) {
253                 if (other.qname != null) {
254                     return false;
255                 }
256             } else if (!qname.equals(other.qname)) {
257                 return false;
258             }
259             if (path == null) {
260                 if (other.path != null) {
261                     return false;
262                 }
263             } else if (!path.equals(other.path)) {
264                 return false;
265             }
266             return true;
267         }
268
269         @Override
270         public String toString() {
271             StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
272             sb.append("[");
273             sb.append("qname=").append(qname);
274             sb.append("]");
275             return sb.toString();
276         }
277     }
278
279 }