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.NotificationDefinition;
21 import org.opendaylight.controller.yang.model.api.SchemaNode;
22 import org.opendaylight.controller.yang.model.api.SchemaPath;
23 import org.opendaylight.controller.yang.model.api.Status;
24 import org.opendaylight.controller.yang.model.api.TypeDefinition;
25 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.controller.yang.model.api.UsesNode;
27 import org.opendaylight.controller.yang.model.parser.builder.api.AbstractChildNodeBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
30 import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder;
31 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionAwareBuilder;
32 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
33 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
35 public class NotificationBuilder extends AbstractChildNodeBuilder implements
36 TypeDefinitionAwareBuilder, SchemaNodeBuilder {
38 private final NotificationDefinitionImpl instance;
39 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
40 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
42 NotificationBuilder(QName qname) {
44 instance = new NotificationDefinitionImpl(qname);
48 public SchemaNode build() {
50 Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
51 for (DataSchemaNodeBuilder node : childNodes) {
52 childs.put(node.getQName(), node.build());
54 instance.setChildNodes(childs);
57 Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
58 for (GroupingBuilder builder : groupings) {
59 groupingDefinitions.add(builder.build());
61 instance.setGroupings(groupingDefinitions);
64 Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
65 for (TypeDefinitionBuilder entry : addedTypedefs) {
66 typedefs.add(entry.build());
68 instance.setTypeDefinitions(typedefs);
71 Set<UsesNode> uses = new HashSet<UsesNode>();
72 for (UsesNodeBuilder builder : addedUsesNodes) {
73 uses.add(builder.build());
75 instance.setUses(uses);
81 public void addTypedef(TypeDefinitionBuilder type) {
82 addedTypedefs.add(type);
86 public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
87 addedUsesNodes.add(usesNodeBuilder);
91 public void setPath(SchemaPath schemaPath) {
92 instance.setPath(schemaPath);
96 public void setDescription(String description) {
97 instance.setDescription(description);
101 public void setReference(String reference) {
102 instance.setReference(reference);
106 public void setStatus(Status status) {
107 instance.setStatus(status);
110 private class NotificationDefinitionImpl implements NotificationDefinition {
112 private final QName qname;
113 private SchemaPath path;
114 private String description;
115 private String reference;
116 private Status status;
117 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
118 private Set<GroupingDefinition> groupings = Collections.emptySet();
119 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
120 private Set<UsesNode> uses = Collections.emptySet();
121 private List<UnknownSchemaNode> unknownSchemaNodes = Collections.emptyList();
123 private NotificationDefinitionImpl(QName qname) {
128 public QName getQName() {
133 public SchemaPath getPath() {
137 private void setPath(SchemaPath path) {
142 public String getDescription() {
146 private void setDescription(String description) {
147 this.description = description;
151 public String getReference() {
155 private void setReference(String reference) {
156 this.reference = reference;
160 public Status getStatus() {
164 private void setStatus(Status status) {
165 this.status = status;
169 public Set<DataSchemaNode> getChildNodes() {
170 return new HashSet<DataSchemaNode>(childNodes.values());
173 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
174 if(childNodes != null) {
175 this.childNodes = childNodes;
180 public Set<GroupingDefinition> getGroupings() {
184 private void setGroupings(Set<GroupingDefinition> groupings) {
185 if(groupings != null) {
186 this.groupings = groupings;
191 public Set<UsesNode> getUses() {
195 private void setUses(Set<UsesNode> uses) {
202 public Set<TypeDefinition<?>> getTypeDefinitions() {
203 return typeDefinitions;
206 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
207 if(typeDefinitions != null) {
208 this.typeDefinitions = typeDefinitions;
213 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
214 return unknownSchemaNodes;
218 public DataSchemaNode getDataChildByName(QName name) {
219 return childNodes.get(name);
223 public DataSchemaNode getDataChildByName(String name) {
224 DataSchemaNode result = null;
225 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
226 if (entry.getKey().getLocalName().equals(name)) {
227 result = entry.getValue();
235 public int hashCode() {
236 final int prime = 31;
238 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
239 result = prime * result + ((path == null) ? 0 : path.hashCode());
240 result = prime * result
241 + ((description == null) ? 0 : description.hashCode());
242 result = prime * result
243 + ((reference == null) ? 0 : reference.hashCode());
244 result = prime * result
245 + ((status == null) ? 0 : status.hashCode());
246 result = prime * result
247 + ((childNodes == null) ? 0 : childNodes.hashCode());
248 result = prime * result
249 + ((groupings == null) ? 0 : groupings.hashCode());
250 result = prime * result
251 + ((typeDefinitions == null) ? 0 : typeDefinitions.hashCode());
252 result = prime * result + ((uses == null) ? 0 : uses.hashCode());
257 public boolean equals(Object obj) {
264 if (getClass() != obj.getClass()) {
267 NotificationDefinitionImpl other = (NotificationDefinitionImpl) obj;
269 if (other.qname != null) {
272 } else if (!qname.equals(other.qname)) {
276 if (other.path != null) {
279 } else if (!path.equals(other.path)) {
282 if (description == null) {
283 if (other.description != null) {
286 } else if (!description.equals(other.description)) {
289 if (reference == null) {
290 if (other.reference != null) {
293 } else if (!reference.equals(other.reference)) {
296 if (status == null) {
297 if (other.status != null) {
300 } else if (!status.equals(other.status)) {
303 if (childNodes == null) {
304 if (other.childNodes != null) {
307 } else if (!childNodes.equals(other.childNodes)) {
310 if (groupings == null) {
311 if (other.groupings != null) {
314 } else if (!groupings.equals(other.groupings)) {
317 if (typeDefinitions == null) {
318 if (other.typeDefinitions != null) {
321 } else if (!typeDefinitions.equals(other.typeDefinitions)) {
325 if (other.uses != null) {
328 } else if (!uses.equals(other.uses)) {
335 public String toString() {
336 StringBuilder sb = new StringBuilder(
337 NotificationDefinitionImpl.class.getSimpleName());
338 sb.append("[qname=" + qname + "]");
339 return sb.toString();