2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang.model.parser.builder.impl;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
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;
30 public class GroupingBuilderImpl implements GroupingBuilder {
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>();
38 GroupingBuilderImpl(QName qname) {
39 this.instance = new GroupingDefinitionImpl(qname);
43 public GroupingDefinition build() {
45 Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
46 for (DataSchemaNodeBuilder node : childNodes) {
47 childs.put(node.getQName(), node.build());
49 instance.setChildNodes(childs);
52 Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
53 for (GroupingBuilder builder : groupings) {
54 groupingDefinitions.add(builder.build());
56 instance.setGroupings(groupingDefinitions);
59 Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
60 for (TypeDefinitionBuilder entry : addedTypedefs) {
61 typedefs.add(entry.build());
63 instance.setTypeDefinitions(typedefs);
66 Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
67 for (UsesNodeBuilder builder : usesNodes) {
68 usesNodeDefinitions.add(builder.build());
70 instance.setUses(usesNodeDefinitions);
76 * Always returns null.
79 public QName getQName() {
84 public void addTypedef(TypeDefinitionBuilder type) {
85 addedTypedefs.add(type);
89 public void setPath(SchemaPath schemaPath) {
90 instance.setPath(schemaPath);
94 public void setDescription(String description) {
95 instance.setDescription(description);
99 public void setReference(String reference) {
100 instance.setReference(reference);
104 public void setStatus(Status status) {
105 instance.setStatus(status);
109 public void addChildNode(DataSchemaNodeBuilder childNode) {
110 childNodes.add(childNode);
114 public void addGrouping(GroupingBuilder grouping) {
115 groupings.add(grouping);
119 public void addUsesNode(UsesNodeBuilder usesBuilder) {
120 usesNodes.add(usesBuilder);
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;
134 private GroupingDefinitionImpl(QName qname) {
139 public QName getQName() {
144 public SchemaPath getPath() {
148 private void setPath(SchemaPath path) {
153 public String getDescription() {
157 private void setDescription(String description) {
158 this.description = description;
162 public String getReference() {
166 private void setReference(String reference) {
167 this.reference = reference;
171 public Status getStatus() {
175 private void setStatus(Status status) {
176 this.status = status;
180 public Set<DataSchemaNode> getChildNodes() {
181 return new HashSet<DataSchemaNode>(childNodes.values());
184 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
185 this.childNodes = childNodes;
189 public Set<GroupingDefinition> getGroupings() {
193 private void setGroupings(Set<GroupingDefinition> groupings) {
194 this.groupings = groupings;
198 public Set<UsesNode> getUses() {
202 private void setUses(Set<UsesNode> uses) {
207 public Set<TypeDefinition<?>> getTypeDefinitions() {
208 return typeDefinitions;
211 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
212 this.typeDefinitions = typeDefinitions;
216 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
217 return Collections.emptyList();
221 public DataSchemaNode getDataChildByName(QName name) {
222 return childNodes.get(name);
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();
238 public int hashCode() {
239 final int prime = 31;
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());
255 + ((typeDefinitions == null) ? 0 : typeDefinitions
257 result = prime * result + ((uses == null) ? 0 : uses.hashCode());
262 public boolean equals(Object obj) {
269 if (getClass() != obj.getClass()) {
272 GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
274 if (other.qname != null) {
277 } else if (!qname.equals(other.qname)) {
281 if (other.path != null) {
284 } else if (!path.equals(other.path)) {
287 if (description == null) {
288 if (other.description != null) {
291 } else if (!description.equals(other.description)) {
294 if (reference == null) {
295 if (other.reference != null) {
298 } else if (!reference.equals(other.reference)) {
301 if (status == null) {
302 if (other.status != null) {
305 } else if (!status.equals(other.status)) {
308 if (childNodes == null) {
309 if (other.childNodes != null) {
312 } else if (!childNodes.equals(other.childNodes)) {
315 if (groupings == null) {
316 if (other.groupings != null) {
319 } else if (!groupings.equals(other.groupings)) {
322 if (typeDefinitions == null) {
323 if (other.typeDefinitions != null) {
326 } else if (!typeDefinitions.equals(other.typeDefinitions)) {
330 if (other.uses != null) {
333 } else if (!uses.equals(other.uses)) {
340 public String toString() {
341 StringBuilder sb = new StringBuilder(
342 GroupingDefinitionImpl.class.getSimpleName());
344 sb.append("qname=" + qname);
346 return sb.toString();