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.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.HashSet;
15 import java.util.List;
17 import java.util.TreeSet;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.UsesNode;
27 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
33 import org.opendaylight.yangtools.yang.parser.util.Comparators;
34 import org.opendaylight.yangtools.yang.parser.util.CopyUtils;
35 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
36 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
38 public final class GroupingBuilderImpl extends AbstractDataNodeContainerBuilder implements GroupingBuilder {
39 private boolean isBuilt;
40 private final GroupingDefinitionImpl instance;
41 private SchemaPath schemaPath;
43 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
44 super(moduleName, line, qname);
46 instance = new GroupingDefinitionImpl(qname, path);
49 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path, final GroupingDefinition base) {
50 super(moduleName, line, base.getQName());
52 instance = new GroupingDefinitionImpl(qname, path);
54 instance.description = base.getDescription();
55 instance.reference = base.getReference();
56 instance.status = base.getStatus();
57 instance.addedByUses = base.isAddedByUses();
59 URI ns = qname.getNamespace();
60 Date rev = qname.getRevision();
61 String pref = qname.getPrefix();
62 addedChildNodes.addAll(ParserUtils.wrapChildNodes(moduleName, line, base.getChildNodes(), path, ns, rev, pref));
63 addedGroupings.addAll(ParserUtils.wrapGroupings(moduleName, line, base.getGroupings(), path, ns, rev, pref));
64 addedTypedefs.addAll(ParserUtils.wrapTypedefs(moduleName, line, base, path, ns, rev, pref));
65 addedUnknownNodes.addAll(ParserUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
68 instance.uses.addAll(base.getUses());
72 public GroupingDefinition build() {
75 for (DataSchemaNodeBuilder node : addedChildNodes) {
76 childNodes.add(node.build());
78 instance.addChildNodes(childNodes);
81 for (GroupingBuilder builder : addedGroupings) {
82 groupings.add(builder.build());
84 instance.addGroupings(groupings);
87 for (TypeDefinitionBuilder entry : addedTypedefs) {
88 typedefs.add(entry.build());
90 instance.addTypeDefinitions(typedefs);
93 for (UsesNodeBuilder builder : addedUsesNodes) {
94 usesNodes.add(builder.build());
96 instance.addUses(usesNodes);
99 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
100 unknownNodes.add(b.build());
102 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
103 instance.addUnknownSchemaNodes(unknownNodes);
112 public Set<DataSchemaNodeBuilder> instantiateChildNodes(Builder newParent) {
113 final Set<DataSchemaNodeBuilder> nodes = new HashSet<>();
114 for (DataSchemaNodeBuilder node : addedChildNodes) {
115 DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
116 ParserUtils.setNodeAddedByUses(copy);
117 if (newParent instanceof DataSchemaNodeBuilder) {
118 ParserUtils.setNodeConfig(copy, ((DataSchemaNodeBuilder)newParent).isConfiguration());
126 public Set<TypeDefinitionBuilder> instantiateTypedefs(Builder newParent) {
127 final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
128 for (TypeDefinitionBuilder node : addedTypedefs) {
129 TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
136 public Set<GroupingBuilder> instantiateGroupings(Builder newParent) {
137 final Set<GroupingBuilder> nodes = new HashSet<>();
138 for (GroupingBuilder node : addedGroupings) {
139 GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
140 copy.setAddedByUses(true);
141 for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
142 ParserUtils.setNodeAddedByUses(childNode);
150 public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(Builder newParent) {
151 final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
152 for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
153 UnknownSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
154 copy.setAddedByUses(true);
161 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
162 return addedTypedefs;
166 public void addTypedef(final TypeDefinitionBuilder type) {
167 String typeName = type.getQName().getLocalName();
168 for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
169 throw new YangParseException(moduleName, type.getLine(), "Can not add typedef '" + typeName
170 + "': typedef with same name already declared at line " + addedTypedef.getLine());
172 addedTypedefs.add(type);
176 public SchemaPath getPath() {
181 public String getDescription() {
182 return instance.description;
186 public void setDescription(final String description) {
187 instance.description = description;
191 public String getReference() {
192 return instance.reference;
196 public void setReference(final String reference) {
197 instance.reference = reference;
201 public Status getStatus() {
202 return instance.status;
206 public void setStatus(Status status) {
207 if (status != null) {
208 instance.status = status;
213 public boolean isAddedByUses() {
214 return instance.addedByUses;
218 public void setAddedByUses(final boolean addedByUses) {
219 instance.addedByUses = addedByUses;
223 public String toString() {
224 return "grouping " + qname.getLocalName();
228 public int hashCode() {
229 final int prime = 31;
231 result = prime * result + ((parentBuilder == null) ? 0 : parentBuilder.hashCode());
232 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
237 public boolean equals(Object obj) {
244 if (getClass() != obj.getClass()) {
247 if (!super.equals(obj)) {
250 final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
251 if (parentBuilder == null) {
252 if (other.parentBuilder != null) {
255 } else if (!parentBuilder.equals(other.parentBuilder)) {
258 if (schemaPath == null) {
259 if (other.schemaPath != null) {
262 } else if (!schemaPath.equals(other.schemaPath)) {
269 private static final class GroupingDefinitionImpl implements GroupingDefinition {
270 private final QName qname;
271 private final SchemaPath path;
272 private String description;
273 private String reference;
274 private Status status;
275 private boolean addedByUses;
276 private final Set<DataSchemaNode> childNodes = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
277 private final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
278 private final Set<TypeDefinition<?>> typeDefinitions = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
279 private final Set<UsesNode> uses = new HashSet<>();
280 private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
282 private GroupingDefinitionImpl(final QName qname, final SchemaPath path) {
288 public QName getQName() {
293 public SchemaPath getPath() {
298 public String getDescription() {
303 public String getReference() {
308 public Status getStatus() {
313 public boolean isAddedByUses() {
318 public Set<DataSchemaNode> getChildNodes() {
319 return Collections.unmodifiableSet(childNodes);
322 private void addChildNodes(Set<DataSchemaNode> childNodes) {
323 if (childNodes != null) {
324 this.childNodes.addAll(childNodes);
329 public Set<GroupingDefinition> getGroupings() {
330 return Collections.unmodifiableSet(groupings);
333 private void addGroupings(Set<GroupingDefinition> groupings) {
334 if (groupings != null) {
335 this.groupings.addAll(groupings);
340 public Set<UsesNode> getUses() {
341 return Collections.unmodifiableSet(uses);
344 private void addUses(Set<UsesNode> uses) {
346 this.uses.addAll(uses);
351 public Set<TypeDefinition<?>> getTypeDefinitions() {
352 return Collections.unmodifiableSet(typeDefinitions);
355 private void addTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
356 if (typeDefinitions != null) {
357 this.typeDefinitions.addAll(typeDefinitions);
362 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
363 return Collections.unmodifiableList(unknownNodes);
366 private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
367 if (unknownNodes != null) {
368 this.unknownNodes.addAll(unknownNodes);
373 public DataSchemaNode getDataChildByName(QName name) {
374 return getChildNode(childNodes, name);
378 public DataSchemaNode getDataChildByName(String name) {
379 return getChildNode(childNodes, name);
383 public int hashCode() {
384 final int prime = 31;
386 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
387 result = prime * result + ((path == null) ? 0 : path.hashCode());
392 public boolean equals(Object obj) {
399 if (getClass() != obj.getClass()) {
402 final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
404 if (other.qname != null) {
407 } else if (!qname.equals(other.qname)) {
411 if (other.path != null) {
414 } else if (!path.equals(other.path)) {
421 public String toString() {
422 StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
424 sb.append("qname=" + qname);
426 return sb.toString();