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