Merge from development repository.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / 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.controller.yang.model.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.opendaylight.controller.yang.common.QName;
18 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
19 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
20 import org.opendaylight.controller.yang.model.api.SchemaPath;
21 import org.opendaylight.controller.yang.model.api.Status;
22 import org.opendaylight.controller.yang.model.api.TypeDefinition;
23 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
24 import org.opendaylight.controller.yang.model.api.UsesNode;
25 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
29
30 public class GroupingBuilderImpl implements GroupingBuilder {
31
32     private final GroupingDefinitionImpl instance;
33     private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
34     private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
35     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
36     private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
37
38     GroupingBuilderImpl(QName qname) {
39         this.instance = new GroupingDefinitionImpl(qname);
40     }
41
42     @Override
43     public GroupingDefinition build() {
44         // CHILD NODES
45         Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
46         for (DataSchemaNodeBuilder node : childNodes) {
47             childs.put(node.getQName(), node.build());
48         }
49         instance.setChildNodes(childs);
50
51         // GROUPINGS
52         Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
53         for (GroupingBuilder builder : groupings) {
54             groupingDefinitions.add(builder.build());
55         }
56         instance.setGroupings(groupingDefinitions);
57
58         // TYPEDEFS
59         Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
60         for (TypeDefinitionBuilder entry : addedTypedefs) {
61             typedefs.add(entry.build());
62         }
63         instance.setTypeDefinitions(typedefs);
64
65         // USES
66         Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
67         for (UsesNodeBuilder builder : usesNodes) {
68             usesNodeDefinitions.add(builder.build());
69         }
70         instance.setUses(usesNodeDefinitions);
71
72         return instance;
73     }
74
75     /**
76      * Always returns null.
77      */
78     @Override
79     public QName getQName() {
80         return null;
81     }
82
83     @Override
84     public void addTypedef(TypeDefinitionBuilder type) {
85         addedTypedefs.add(type);
86     }
87
88     @Override
89     public void setPath(SchemaPath schemaPath) {
90         instance.setPath(schemaPath);
91     }
92
93     @Override
94     public void setDescription(String description) {
95         instance.setDescription(description);
96     }
97
98     @Override
99     public void setReference(String reference) {
100         instance.setReference(reference);
101     }
102
103     @Override
104     public void setStatus(Status status) {
105         instance.setStatus(status);
106     }
107
108     @Override
109     public void addChildNode(DataSchemaNodeBuilder childNode) {
110         childNodes.add(childNode);
111     }
112
113     @Override
114     public void addGrouping(GroupingBuilder grouping) {
115         groupings.add(grouping);
116     }
117
118     @Override
119     public void addUsesNode(UsesNodeBuilder usesBuilder) {
120         usesNodes.add(usesBuilder);
121     }
122
123     private static class GroupingDefinitionImpl implements GroupingDefinition {
124         private final QName qname;
125         private SchemaPath path;
126         private String description;
127         private String reference;
128         private Status status;
129         private Map<QName, DataSchemaNode> childNodes;
130         private Set<GroupingDefinition> groupings;
131         private Set<TypeDefinition<?>> typeDefinitions;
132         private Set<UsesNode> uses;
133
134         private GroupingDefinitionImpl(QName qname) {
135             this.qname = qname;
136         }
137
138         @Override
139         public QName getQName() {
140             return qname;
141         }
142
143         @Override
144         public SchemaPath getPath() {
145             return path;
146         }
147
148         private void setPath(SchemaPath path) {
149             this.path = path;
150         }
151
152         @Override
153         public String getDescription() {
154             return description;
155         }
156
157         private void setDescription(String description) {
158             this.description = description;
159         }
160
161         @Override
162         public String getReference() {
163             return reference;
164         }
165
166         private void setReference(String reference) {
167             this.reference = reference;
168         }
169
170         @Override
171         public Status getStatus() {
172             return status;
173         }
174
175         private void setStatus(Status status) {
176             this.status = status;
177         }
178
179         @Override
180         public Set<DataSchemaNode> getChildNodes() {
181             return new HashSet<DataSchemaNode>(childNodes.values());
182         }
183
184         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
185             this.childNodes = childNodes;
186         }
187
188         @Override
189         public Set<GroupingDefinition> getGroupings() {
190             return groupings;
191         }
192
193         private void setGroupings(Set<GroupingDefinition> groupings) {
194             this.groupings = groupings;
195         }
196
197         @Override
198         public Set<UsesNode> getUses() {
199             return uses;
200         }
201
202         private void setUses(Set<UsesNode> uses) {
203             this.uses = uses;
204         }
205
206         @Override
207         public Set<TypeDefinition<?>> getTypeDefinitions() {
208             return typeDefinitions;
209         }
210
211         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
212             this.typeDefinitions = typeDefinitions;
213         }
214
215         @Override
216         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
217             return Collections.emptyList();
218         }
219
220         @Override
221         public DataSchemaNode getDataChildByName(QName name) {
222             return childNodes.get(name);
223         }
224
225         @Override
226         public DataSchemaNode getDataChildByName(String name) {
227             DataSchemaNode result = null;
228             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
229                 if (entry.getKey().getLocalName().equals(name)) {
230                     result = entry.getValue();
231                     break;
232                 }
233             }
234             return result;
235         }
236
237         @Override
238         public int hashCode() {
239             final int prime = 31;
240             int result = 1;
241             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
242             result = prime * result + ((path == null) ? 0 : path.hashCode());
243             result = prime * result
244                     + ((description == null) ? 0 : description.hashCode());
245             result = prime * result
246                     + ((reference == null) ? 0 : reference.hashCode());
247             result = prime * result
248                     + ((status == null) ? 0 : status.hashCode());
249             result = prime * result
250                     + ((childNodes == null) ? 0 : childNodes.hashCode());
251             result = prime * result
252                     + ((groupings == null) ? 0 : groupings.hashCode());
253             result = prime
254                     * result
255                     + ((typeDefinitions == null) ? 0 : typeDefinitions
256                             .hashCode());
257             result = prime * result + ((uses == null) ? 0 : uses.hashCode());
258             return result;
259         }
260
261         @Override
262         public boolean equals(Object obj) {
263             if (this == obj) {
264                 return true;
265             }
266             if (obj == null) {
267                 return false;
268             }
269             if (getClass() != obj.getClass()) {
270                 return false;
271             }
272             GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
273             if (qname == null) {
274                 if (other.qname != null) {
275                     return false;
276                 }
277             } else if (!qname.equals(other.qname)) {
278                 return false;
279             }
280             if (path == null) {
281                 if (other.path != null) {
282                     return false;
283                 }
284             } else if (!path.equals(other.path)) {
285                 return false;
286             }
287             if (description == null) {
288                 if (other.description != null) {
289                     return false;
290                 }
291             } else if (!description.equals(other.description)) {
292                 return false;
293             }
294             if (reference == null) {
295                 if (other.reference != null) {
296                     return false;
297                 }
298             } else if (!reference.equals(other.reference)) {
299                 return false;
300             }
301             if (status == null) {
302                 if (other.status != null) {
303                     return false;
304                 }
305             } else if (!status.equals(other.status)) {
306                 return false;
307             }
308             if (childNodes == null) {
309                 if (other.childNodes != null) {
310                     return false;
311                 }
312             } else if (!childNodes.equals(other.childNodes)) {
313                 return false;
314             }
315             if (groupings == null) {
316                 if (other.groupings != null) {
317                     return false;
318                 }
319             } else if (!groupings.equals(other.groupings)) {
320                 return false;
321             }
322             if (typeDefinitions == null) {
323                 if (other.typeDefinitions != null) {
324                     return false;
325                 }
326             } else if (!typeDefinitions.equals(other.typeDefinitions)) {
327                 return false;
328             }
329             if (uses == null) {
330                 if (other.uses != null) {
331                     return false;
332                 }
333             } else if (!uses.equals(other.uses)) {
334                 return false;
335             }
336             return true;
337         }
338
339         @Override
340         public String toString() {
341             StringBuilder sb = new StringBuilder(
342                     GroupingDefinitionImpl.class.getSimpleName());
343             sb.append("[");
344             sb.append("qname=" + qname);
345             sb.append("]");
346             return sb.toString();
347         }
348     }
349
350 }