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.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
26 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
28 public final class GroupingBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements GroupingBuilder {
29 private GroupingDefinitionImpl instance;
31 private SchemaPath schemaPath;
32 // DataSchemaNode args
33 private boolean addedByUses;
35 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
36 super(moduleName, line, qname);
37 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
40 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path,
41 final GroupingDefinition base) {
42 super(moduleName, line, base.getQName(), Preconditions.checkNotNull(path, "Schema Path must not be null"), base);
44 addedByUses = base.isAddedByUses();
45 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
50 public GroupingDefinition build() {
51 if (instance != null) {
55 instance = new GroupingDefinitionImpl(qname, schemaPath, this);
56 instance.addedByUses = addedByUses;
59 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
60 unknownNodes.add(b.build());
62 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
68 public List<DataSchemaNodeBuilder> instantiateChildNodes(final Builder newParent) {
69 final List<DataSchemaNodeBuilder> nodes = new ArrayList<>();
70 for (DataSchemaNodeBuilder node : getChildNodeBuilders()) {
71 DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
72 BuilderUtils.setNodeAddedByUses(copy);
79 public Set<TypeDefinitionBuilder> instantiateTypedefs(final Builder newParent) {
80 final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
81 for (TypeDefinitionBuilder node : getTypeDefinitionBuilders()) {
82 TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
89 public Set<GroupingBuilder> instantiateGroupings(final Builder newParent) {
90 final Set<GroupingBuilder> nodes = new HashSet<>();
91 for (GroupingBuilder node : getGroupingBuilders()) {
92 GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
93 copy.setAddedByUses(true);
94 for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
95 BuilderUtils.setNodeAddedByUses(childNode);
103 public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(final Builder newParent) {
104 final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
105 for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
106 UnknownSchemaNodeBuilderImpl copy = CopyUtils.copy(node, newParent, true);
107 copy.setAddedByUses(true);
114 public SchemaPath getPath() {
119 public void setPath(final SchemaPath path) {
120 this.schemaPath = path;
124 public boolean isAddedByUses() {
129 public void setAddedByUses(final boolean addedByUses) {
130 this.addedByUses = addedByUses;
134 public String toString() {
135 return "grouping " + qname.getLocalName();
139 public int hashCode() {
140 final int prime = 31;
142 result = prime * result + ((getParent() == null) ? 0 : getParent().hashCode());
143 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
148 public boolean equals(final Object obj) {
155 if (getClass() != obj.getClass()) {
158 if (!super.equals(obj)) {
161 final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
162 if (getParent() == null) {
163 if (other.getParent() != null) {
166 } else if (!getParent().equals(other.getParent())) {
169 if (schemaPath == null) {
170 if (other.schemaPath != null) {
173 } else if (!schemaPath.equals(other.schemaPath)) {
180 protected String getStatementName() {
184 private static final class GroupingDefinitionImpl extends AbstractDocumentedDataNodeContainer implements
186 private final QName qname;
187 private final SchemaPath path;
189 private boolean addedByUses;
190 private ImmutableList<UnknownSchemaNode> unknownNodes;
192 private GroupingDefinitionImpl(final QName qname, final SchemaPath path, final GroupingBuilderImpl builder) {
199 public QName getQName() {
204 public SchemaPath getPath() {
209 public boolean isAddedByUses() {
214 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
219 public int hashCode() {
220 final int prime = 31;
222 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
223 result = prime * result + ((path == null) ? 0 : path.hashCode());
228 public boolean equals(final Object obj) {
235 if (getClass() != obj.getClass()) {
238 final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
240 if (other.qname != null) {
243 } else if (!qname.equals(other.qname)) {
247 if (other.path != null) {
250 } else if (!path.equals(other.path)) {
257 public String toString() {
258 StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
260 sb.append("qname=").append(qname);
262 return sb.toString();