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;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
21 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
33 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
34 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
36 public final class ChoiceCaseBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DataSchemaNodeBuilder,
37 AugmentationTargetBuilder {
38 private ChoiceCaseNodeImpl instance;
40 private SchemaPath schemaPath;
41 // DataSchemaNode args
42 private boolean augmenting;
43 private ChoiceCaseNode originalNode;
44 private ChoiceCaseBuilder originalBuilder;
45 private boolean addedByUses;
46 private final ConstraintsBuilder constraints;
47 // AugmentationTarget args
48 private final List<AugmentationSchema> augmentations = new ArrayList<>();
49 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
51 public ChoiceCaseBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
52 super(moduleName, line, qname);
53 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
54 constraints = new ConstraintsBuilderImpl(moduleName, line);
57 public ChoiceCaseBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
58 final ChoiceCaseNode base) {
59 super(moduleName, line, qname, Preconditions.checkNotNull(path, "Schema Path must not be null"), base);
60 this.schemaPath = path;
61 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
63 augmenting = base.isAugmenting();
64 addedByUses = base.isAddedByUses();
66 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
68 augmentations.addAll(base.getAvailableAugmentations());
72 public ChoiceCaseNode build() {
73 if (instance != null) {
77 instance = new ChoiceCaseNodeImpl(qname, schemaPath,this);
79 instance.augmenting = augmenting;
80 instance.addedByUses = addedByUses;
82 instance.constraints = constraints.toInstance();
85 if (originalNode == null && originalBuilder != null) {
86 originalNode = originalBuilder.build();
88 instance.original = originalNode;
91 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
92 unknownNodes.add(b.build());
94 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
97 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
98 augmentations.add(builder.build());
100 instance.augmentations = ImmutableSet.copyOf(augmentations);
106 public SchemaPath getPath() {
111 public void setPath(final SchemaPath path) {
112 this.schemaPath = path;
115 public boolean isAugmenting() {
120 public void setAugmenting(final boolean augmenting) {
121 this.augmenting = augmenting;
125 public boolean isAddedByUses() {
130 public void setAddedByUses(final boolean addedByUses) {
131 this.addedByUses = addedByUses;
135 public ChoiceCaseBuilder getOriginal() {
136 return originalBuilder;
140 public void setOriginal(final SchemaNodeBuilder builder) {
141 Preconditions.checkArgument(builder instanceof ChoiceCaseBuilder, "Original of case cannot be " + builder);
142 this.originalBuilder = (ChoiceCaseBuilder) builder;
146 public void addTypedef(final TypeDefinitionBuilder typedefBuilder) {
147 throw new YangParseException(getModuleName(), typedefBuilder.getLine(), "Can not add type definition to choice case.");
151 public void addGrouping(GroupingBuilder groupingBuilder) {
152 throw new YangParseException(getModuleName(), groupingBuilder.getLine(), "Can not add grouping to choice case.");
156 public boolean isConfiguration() {
161 public void setConfiguration(final boolean configuration) {
162 throw new YangParseException(getModuleName(), getLine(), "Can not add config statement to choice case.");
166 public ConstraintsBuilder getConstraints() {
171 public void addAugmentation(final AugmentationSchemaBuilder augment) {
172 augmentationBuilders.add(augment);
176 public int hashCode() {
177 final int prime = 31;
179 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
184 public boolean equals(final Object obj) {
191 if (getClass() != obj.getClass()) {
194 ChoiceCaseBuilder other = (ChoiceCaseBuilder) obj;
195 if (schemaPath == null) {
196 if (other.schemaPath != null) {
199 } else if (!schemaPath.equals(other.schemaPath)) {
202 if (getParent() == null) {
203 if (other.getParent() != null) {
206 } else if (!getParent().equals(other.getParent())) {
213 public String toString() {
214 return "case " + getQName().getLocalName();
218 protected String getStatementName() {
222 private static final class ChoiceCaseNodeImpl extends AbstractDocumentedDataNodeContainer implements ChoiceCaseNode, DerivableSchemaNode {
223 private final QName qname;
224 private final SchemaPath path;
225 private boolean augmenting;
226 private boolean addedByUses;
227 private ChoiceCaseNode original;
228 private ConstraintDefinition constraints;
229 private ImmutableSet<AugmentationSchema> augmentations;
230 private ImmutableList<UnknownSchemaNode> unknownNodes;
232 private ChoiceCaseNodeImpl(final QName qname, final SchemaPath path,final ChoiceCaseBuilder builder) {
239 public QName getQName() {
244 public SchemaPath getPath() {
249 public boolean isConfiguration() {
254 public ConstraintDefinition getConstraints() {
259 public boolean isAugmenting() {
264 public boolean isAddedByUses() {
269 public Optional<ChoiceCaseNode> getOriginal() {
270 return Optional.fromNullable(original);
274 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
279 public Set<AugmentationSchema> getAvailableAugmentations() {
280 return augmentations;
284 public int hashCode() {
285 final int prime = 31;
287 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
288 result = prime * result + ((path == null) ? 0 : path.hashCode());
293 public boolean equals(final Object obj) {
300 if (getClass() != obj.getClass()) {
303 ChoiceCaseNodeImpl other = (ChoiceCaseNodeImpl) obj;
305 if (other.qname != null) {
308 } else if (!qname.equals(other.qname)) {
312 if (other.path != null) {
315 } else if (!path.equals(other.path)) {
322 public String toString() {
323 StringBuilder sb = new StringBuilder(ChoiceCaseNodeImpl.class.getSimpleName());
328 return sb.toString();