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;
13 import java.util.Date;
14 import java.util.HashSet;
15 import java.util.List;
17 import org.opendaylight.yangtools.yang.common.QName;
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.UnknownSchemaNode;
22 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
28 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
30 public final class GroupingBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements GroupingBuilder {
31 private GroupingDefinitionImpl instance;
33 private SchemaPath schemaPath;
34 private String description;
35 private String reference;
36 private Status status = Status.CURRENT;
37 // DataSchemaNode args
38 private boolean addedByUses;
40 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
41 super(moduleName, line, qname);
42 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
45 public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path,
46 final GroupingDefinition base) {
47 super(moduleName, line, base.getQName(), path, base);
50 description = base.getDescription();
51 reference = base.getReference();
52 status = base.getStatus();
53 addedByUses = base.isAddedByUses();
55 URI ns = qname.getNamespace();
56 Date rev = qname.getRevision();
57 String pref = qname.getPrefix();
58 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
63 public GroupingDefinition build() {
64 if (instance != null) {
68 instance = new GroupingDefinitionImpl(qname, schemaPath, this);
69 instance.addedByUses = addedByUses;
72 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
73 unknownNodes.add(b.build());
75 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
81 public Set<DataSchemaNodeBuilder> instantiateChildNodes(final Builder newParent) {
82 final Set<DataSchemaNodeBuilder> nodes = new HashSet<>();
83 for (DataSchemaNodeBuilder node : getChildNodeBuilders()) {
84 DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
85 BuilderUtils.setNodeAddedByUses(copy);
92 public Set<TypeDefinitionBuilder> instantiateTypedefs(final Builder newParent) {
93 final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
94 for (TypeDefinitionBuilder node : getTypeDefinitionBuilders()) {
95 TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
102 public Set<GroupingBuilder> instantiateGroupings(final Builder newParent) {
103 final Set<GroupingBuilder> nodes = new HashSet<>();
104 for (GroupingBuilder node : getGroupingBuilders()) {
105 GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
106 copy.setAddedByUses(true);
107 for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
108 BuilderUtils.setNodeAddedByUses(childNode);
116 public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(final Builder newParent) {
117 final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
118 for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
119 UnknownSchemaNodeBuilderImpl copy = CopyUtils.copy(node, newParent, true);
120 copy.setAddedByUses(true);
127 public SchemaPath getPath() {
132 public void setPath(final SchemaPath path) {
133 this.schemaPath = path;
137 public boolean isAddedByUses() {
142 public void setAddedByUses(final boolean addedByUses) {
143 this.addedByUses = addedByUses;
147 public String toString() {
148 return "grouping " + qname.getLocalName();
152 public int hashCode() {
153 final int prime = 31;
155 result = prime * result + ((getParent() == null) ? 0 : getParent().hashCode());
156 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
161 public boolean equals(final Object obj) {
168 if (getClass() != obj.getClass()) {
171 if (!super.equals(obj)) {
174 final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
175 if (getParent() == null) {
176 if (other.getParent() != null) {
179 } else if (!getParent().equals(other.getParent())) {
182 if (schemaPath == null) {
183 if (other.schemaPath != null) {
186 } else if (!schemaPath.equals(other.schemaPath)) {
193 protected String getStatementName() {
197 private static final class GroupingDefinitionImpl extends AbstractDocumentedDataNodeContainer implements
199 private final QName qname;
200 private final SchemaPath path;
202 private boolean addedByUses;
203 private ImmutableList<UnknownSchemaNode> unknownNodes;
205 private GroupingDefinitionImpl(final QName qname, final SchemaPath path, final GroupingBuilderImpl builder) {
212 public QName getQName() {
217 public SchemaPath getPath() {
222 public boolean isAddedByUses() {
227 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
232 public int hashCode() {
233 final int prime = 31;
235 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
236 result = prime * result + ((path == null) ? 0 : path.hashCode());
241 public boolean equals(final Object obj) {
248 if (getClass() != obj.getClass()) {
251 final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
253 if (other.qname != null) {
256 } else if (!qname.equals(other.qname)) {
260 if (other.path != null) {
263 } else if (!path.equals(other.path)) {
270 public String toString() {
271 StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
273 sb.append("qname=").append(qname);
275 return sb.toString();