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;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import java.util.HashSet;
13 import java.util.List;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
20 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
25 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
27 public final class GroupingBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements GroupingBuilder {
28 private GroupingDefinitionImpl instance;
30 private SchemaPath schemaPath;
31 // DataSchemaNode args
32 private boolean addedByUses;
34 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
35 super(moduleName, line, qname);
36 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
39 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path,
40 final GroupingDefinition base) {
41 super(moduleName, line, base.getQName(), Preconditions.checkNotNull(path, "Schema Path must not be null"), base);
43 addedByUses = base.isAddedByUses();
44 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
49 public GroupingDefinition build() {
50 if (instance != null) {
54 instance = new GroupingDefinitionImpl(qname, schemaPath, this);
55 instance.addedByUses = addedByUses;
58 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
59 unknownNodes.add(b.build());
61 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
67 public Set<DataSchemaNodeBuilder> instantiateChildNodes(final Builder newParent) {
68 final Set<DataSchemaNodeBuilder> nodes = new HashSet<>();
69 for (DataSchemaNodeBuilder node : getChildNodeBuilders()) {
70 DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
71 BuilderUtils.setNodeAddedByUses(copy);
78 public Set<TypeDefinitionBuilder> instantiateTypedefs(final Builder newParent) {
79 final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
80 for (TypeDefinitionBuilder node : getTypeDefinitionBuilders()) {
81 TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
88 public Set<GroupingBuilder> instantiateGroupings(final Builder newParent) {
89 final Set<GroupingBuilder> nodes = new HashSet<>();
90 for (GroupingBuilder node : getGroupingBuilders()) {
91 GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
92 copy.setAddedByUses(true);
93 for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
94 BuilderUtils.setNodeAddedByUses(childNode);
102 public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(final Builder newParent) {
103 final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
104 for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
105 UnknownSchemaNodeBuilderImpl copy = CopyUtils.copy(node, newParent, true);
106 copy.setAddedByUses(true);
113 public SchemaPath getPath() {
118 public void setPath(final SchemaPath path) {
119 this.schemaPath = path;
123 public boolean isAddedByUses() {
128 public void setAddedByUses(final boolean addedByUses) {
129 this.addedByUses = addedByUses;
133 public String toString() {
134 return "grouping " + qname.getLocalName();
138 public int hashCode() {
139 final int prime = 31;
141 result = prime * result + ((getParent() == null) ? 0 : getParent().hashCode());
142 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
147 public boolean equals(final Object obj) {
154 if (getClass() != obj.getClass()) {
157 if (!super.equals(obj)) {
160 final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
161 if (getParent() == null) {
162 if (other.getParent() != null) {
165 } else if (!getParent().equals(other.getParent())) {
168 if (schemaPath == null) {
169 if (other.schemaPath != null) {
172 } else if (!schemaPath.equals(other.schemaPath)) {
179 protected String getStatementName() {
183 private static final class GroupingDefinitionImpl extends AbstractDocumentedDataNodeContainer implements
185 private final QName qname;
186 private final SchemaPath path;
188 private boolean addedByUses;
189 private ImmutableList<UnknownSchemaNode> unknownNodes;
191 private GroupingDefinitionImpl(final QName qname, final SchemaPath path, final GroupingBuilderImpl builder) {
198 public QName getQName() {
203 public SchemaPath getPath() {
208 public boolean isAddedByUses() {
213 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
218 public int hashCode() {
219 final int prime = 31;
221 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
222 result = prime * result + ((path == null) ? 0 : path.hashCode());
227 public boolean equals(final Object obj) {
234 if (getClass() != obj.getClass()) {
237 final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
239 if (other.qname != null) {
242 } else if (!qname.equals(other.qname)) {
246 if (other.path != null) {
249 } else if (!path.equals(other.path)) {
256 public String toString() {
257 StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
259 sb.append("qname=").append(qname);
261 return sb.toString();