2 * Copyright (c) 2014 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.Date;
13 import java.util.List;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
19 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.Status;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
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.util.AbstractDocumentedDataNodeContainer;
30 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
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 ChoiceCaseBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DataSchemaNodeBuilder,
38 AugmentationTargetBuilder {
39 private ChoiceCaseNodeImpl instance;
41 private SchemaPath schemaPath;
42 private String description;
43 private String reference;
44 private Status status = Status.CURRENT;
45 // DataSchemaNode args
46 private boolean augmenting;
47 private boolean addedByUses;
48 private final ConstraintsBuilder constraints;
49 // AugmentationTarget args
50 private final List<AugmentationSchema> augmentations = new ArrayList<>();
51 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
53 public ChoiceCaseBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
54 super(moduleName, line, qname);
55 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
56 constraints = new ConstraintsBuilderImpl(moduleName, line);
59 public ChoiceCaseBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
60 final ChoiceCaseNode base) {
62 super(moduleName, line, qname);
63 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
64 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
66 description = base.getDescription();
67 reference = base.getReference();
68 status = base.getStatus();
69 augmenting = base.isAugmenting();
70 addedByUses = base.isAddedByUses();
72 URI ns = qname.getNamespace();
73 Date rev = qname.getRevision();
74 String pref = qname.getPrefix();
75 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
77 augmentations.addAll(base.getAvailableAugmentations());
81 public ChoiceCaseNode build() {
82 if (instance != null) {
86 instance = new ChoiceCaseNodeImpl(qname, schemaPath,this);
88 instance.augmenting = augmenting;
89 instance.addedByUses = addedByUses;
91 instance.constraints = constraints.toInstance();
94 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
95 unknownNodes.add(b.build());
97 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
100 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
101 augmentations.add(builder.build());
103 instance.augmentations = ImmutableSet.copyOf(augmentations);
109 public SchemaPath getPath() {
114 public void setPath(final SchemaPath path) {
115 this.schemaPath = path;
118 public boolean isAugmenting() {
123 public void setAugmenting(final boolean augmenting) {
124 this.augmenting = augmenting;
128 public boolean isAddedByUses() {
133 public void setAddedByUses(final boolean addedByUses) {
134 this.addedByUses = addedByUses;
138 public void addTypedef(final TypeDefinitionBuilder typedefBuilder) {
139 throw new YangParseException(getModuleName(), typedefBuilder.getLine(), "Can not add type definition to choice case.");
143 public boolean isConfiguration() {
148 public void setConfiguration(final boolean configuration) {
149 throw new YangParseException(getModuleName(), getLine(), "Can not add config statement to choice case.");
153 public ConstraintsBuilder getConstraints() {
158 public void addAugmentation(final AugmentationSchemaBuilder augment) {
159 augmentationBuilders.add(augment);
163 public int hashCode() {
164 final int prime = 31;
166 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
171 public boolean equals(final Object obj) {
178 if (getClass() != obj.getClass()) {
181 ChoiceCaseBuilder other = (ChoiceCaseBuilder) obj;
182 if (schemaPath == null) {
183 if (other.schemaPath != null) {
186 } else if (!schemaPath.equals(other.schemaPath)) {
189 if (getParent() == null) {
190 if (other.getParent() != null) {
193 } else if (!getParent().equals(other.getParent())) {
200 public String toString() {
201 return "case " + getQName().getLocalName();
205 protected String getStatementName() {
209 private static final class ChoiceCaseNodeImpl extends AbstractDocumentedDataNodeContainer implements ChoiceCaseNode {
210 private final QName qname;
211 private final SchemaPath path;
212 private boolean augmenting;
213 private boolean addedByUses;
214 private ConstraintDefinition constraints;
215 private ImmutableSet<AugmentationSchema> augmentations;
216 private ImmutableList<UnknownSchemaNode> unknownNodes;
218 private ChoiceCaseNodeImpl(final QName qname, final SchemaPath path,final ChoiceCaseBuilder builder) {
225 public QName getQName() {
230 public SchemaPath getPath() {
235 public boolean isConfiguration() {
240 public ConstraintDefinition getConstraints() {
245 public boolean isAugmenting() {
250 public boolean isAddedByUses() {
255 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
260 public Set<AugmentationSchema> getAvailableAugmentations() {
261 return augmentations;
265 public int hashCode() {
266 final int prime = 31;
268 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
269 result = prime * result + ((path == null) ? 0 : path.hashCode());
274 public boolean equals(final Object obj) {
281 if (getClass() != obj.getClass()) {
284 ChoiceCaseNodeImpl other = (ChoiceCaseNodeImpl) obj;
286 if (other.qname != null) {
289 } else if (!qname.equals(other.qname)) {
293 if (other.path != null) {
296 } else if (!path.equals(other.path)) {
303 public String toString() {
304 StringBuilder sb = new StringBuilder(ChoiceCaseNodeImpl.class.getSimpleName());
309 return sb.toString();