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