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.yangtools.yang.parser.builder.impl;
11 import java.util.Date;
12 import java.util.HashSet;
13 import java.util.List;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
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.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.UsesNode;
24 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDataNodeContainerBuilder;
31 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
33 import com.google.common.base.Preconditions;
34 import com.google.common.collect.ImmutableList;
35 import com.google.common.collect.ImmutableSet;
37 public final class GroupingBuilderImpl extends AbstractDataNodeContainerBuilder implements GroupingBuilder {
38 private GroupingDefinitionImpl instance;
40 private SchemaPath schemaPath;
41 private String description;
42 private String reference;
43 private Status status = Status.CURRENT;
44 // DataSchemaNode args
45 private boolean addedByUses;
47 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
48 super(moduleName, line, qname);
49 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
52 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path,
53 final GroupingDefinition base) {
54 this(moduleName, line, base.getQName(),path);
56 description = base.getDescription();
57 reference = base.getReference();
58 status = base.getStatus();
59 addedByUses = base.isAddedByUses();
61 URI ns = qname.getNamespace();
62 Date rev = qname.getRevision();
63 String pref = qname.getPrefix();
64 addedChildNodes.addAll(BuilderUtils.wrapChildNodes(moduleName, line, base.getChildNodes(), path, ns, rev, pref));
65 addedGroupings.addAll(BuilderUtils.wrapGroupings(moduleName, line, base.getGroupings(), path, ns, rev, pref));
66 addedTypedefs.addAll(BuilderUtils.wrapTypedefs(moduleName, line, base, path, ns, rev, pref));
67 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
70 usesNodes.addAll(base.getUses());
74 public GroupingDefinition build() {
75 if (instance != null) {
79 instance = new GroupingDefinitionImpl(qname, schemaPath);
81 instance.description = description;
82 instance.reference = reference;
83 instance.status = status;
84 instance.addedByUses = addedByUses;
87 for (DataSchemaNodeBuilder node : addedChildNodes) {
88 childNodes.put(node.getQName(), node.build());
90 instance.childNodes = ImmutableSet.copyOf(childNodes.values());
93 for (GroupingBuilder builder : addedGroupings) {
94 groupings.add(builder.build());
96 instance.groupings = ImmutableSet.copyOf(groupings);
99 for (TypeDefinitionBuilder entry : addedTypedefs) {
100 typedefs.add(entry.build());
102 instance.typeDefinitions = ImmutableSet.copyOf(typedefs);
105 for (UsesNodeBuilder builder : addedUsesNodes) {
106 usesNodes.add(builder.build());
108 instance.uses = ImmutableSet.copyOf(usesNodes);
111 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
112 unknownNodes.add(b.build());
114 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
120 public Set<DataSchemaNodeBuilder> instantiateChildNodes(final Builder newParent) {
121 final Set<DataSchemaNodeBuilder> nodes = new HashSet<>();
122 for (DataSchemaNodeBuilder node : addedChildNodes) {
123 DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
124 BuilderUtils.setNodeAddedByUses(copy);
131 public Set<TypeDefinitionBuilder> instantiateTypedefs(final Builder newParent) {
132 final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
133 for (TypeDefinitionBuilder node : addedTypedefs) {
134 TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
141 public Set<GroupingBuilder> instantiateGroupings(final Builder newParent) {
142 final Set<GroupingBuilder> nodes = new HashSet<>();
143 for (GroupingBuilder node : addedGroupings) {
144 GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
145 copy.setAddedByUses(true);
146 for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
147 BuilderUtils.setNodeAddedByUses(childNode);
155 public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(final Builder newParent) {
156 final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
157 for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
158 UnknownSchemaNodeBuilderImpl copy = CopyUtils.copy(node, newParent, true);
159 copy.setAddedByUses(true);
166 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
167 return addedTypedefs;
171 public void addTypedef(final TypeDefinitionBuilder type) {
172 String typeName = type.getQName().getLocalName();
173 for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
174 throw new YangParseException(getModuleName(), type.getLine(), "Can not add typedef '" + typeName
175 + "': typedef with same name already declared at line " + addedTypedef.getLine());
177 addedTypedefs.add(type);
181 public SchemaPath getPath() {
186 public void setPath(final SchemaPath path) {
187 this.schemaPath = path;
191 public String getDescription() {
196 public void setDescription(final String description) {
197 this.description = description;
201 public String getReference() {
206 public void setReference(final String reference) {
207 this.reference = reference;
211 public Status getStatus() {
216 public void setStatus(final Status status) {
217 this.status = Preconditions.checkNotNull(status, "status cannot be null");
221 public boolean isAddedByUses() {
226 public void setAddedByUses(final boolean addedByUses) {
227 this.addedByUses = addedByUses;
231 public String toString() {
232 return "grouping " + qname.getLocalName();
236 public int hashCode() {
237 final int prime = 31;
239 result = prime * result + ((getParent() == null) ? 0 : getParent().hashCode());
240 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
245 public boolean equals(final Object obj) {
252 if (getClass() != obj.getClass()) {
255 if (!super.equals(obj)) {
258 final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
259 if (getParent() == null) {
260 if (other.getParent() != null) {
263 } else if (!getParent().equals(other.getParent())) {
266 if (schemaPath == null) {
267 if (other.schemaPath != null) {
270 } else if (!schemaPath.equals(other.schemaPath)) {
276 private static final class GroupingDefinitionImpl implements GroupingDefinition {
277 private final QName qname;
278 private final SchemaPath path;
279 private String description;
280 private String reference;
281 private Status status;
282 private boolean addedByUses;
283 private ImmutableSet<DataSchemaNode> childNodes;
284 private ImmutableSet<GroupingDefinition> groupings;
285 private ImmutableSet<TypeDefinition<?>> typeDefinitions;
286 private ImmutableSet<UsesNode> uses;
287 private ImmutableList<UnknownSchemaNode> unknownNodes;
289 private GroupingDefinitionImpl(final QName qname, final SchemaPath path) {
295 public QName getQName() {
300 public SchemaPath getPath() {
305 public String getDescription() {
310 public String getReference() {
315 public Status getStatus() {
320 public boolean isAddedByUses() {
325 public Set<DataSchemaNode> getChildNodes() {
330 public Set<GroupingDefinition> getGroupings() {
335 public Set<UsesNode> getUses() {
340 public Set<TypeDefinition<?>> getTypeDefinitions() {
341 return typeDefinitions;
345 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
350 public DataSchemaNode getDataChildByName(final QName name) {
351 return getChildNode(childNodes, name);
355 public DataSchemaNode getDataChildByName(final String name) {
356 return getChildNode(childNodes, name);
360 public int hashCode() {
361 final int prime = 31;
363 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
364 result = prime * result + ((path == null) ? 0 : path.hashCode());
369 public boolean equals(final Object obj) {
376 if (getClass() != obj.getClass()) {
379 final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
381 if (other.qname != null) {
384 } else if (!qname.equals(other.qname)) {
388 if (other.path != null) {
391 } else if (!path.equals(other.path)) {
398 public String toString() {
399 StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
401 sb.append("qname=" + qname);
403 return sb.toString();