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 java.util.ArrayList;
11 import java.util.List;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
16 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
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 import com.google.common.base.Optional;
31 import com.google.common.base.Preconditions;
32 import com.google.common.collect.ImmutableList;
33 import com.google.common.collect.ImmutableSet;
35 public final class ContainerSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
36 AugmentationTargetBuilder, DataSchemaNodeBuilder {
37 private ContainerSchemaNodeImpl instance;
38 private boolean presence;
40 private SchemaPath path;
41 // DataSchemaNode args
42 private boolean augmenting;
43 private boolean addedByUses;
44 private boolean configuration;
45 private ContainerSchemaNode originalNode;
46 private ContainerSchemaNodeBuilder originalBuilder;
47 private final ConstraintsBuilder constraints;
48 // AugmentationTarget args
49 private final List<AugmentationSchema> augmentations = new ArrayList<>();
50 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
52 public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
53 super(moduleName, line, qname);
54 this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
55 this.constraints = new ConstraintsBuilderImpl(moduleName, line);
58 // constructor for uses
59 public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname,
60 final SchemaPath path, final ContainerSchemaNode base) {
61 super(moduleName, line, qname, path, base);
62 this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
64 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
66 augmenting = base.isAugmenting();
67 addedByUses = base.isAddedByUses();
69 configuration = base.isConfiguration();
70 presence = base.isPresenceContainer();
72 augmentations.addAll(base.getAvailableAugmentations());
77 protected String getStatementName() {
82 public ContainerSchemaNode build() {
83 if (instance != null) {
88 instance = new ContainerSchemaNodeImpl(this);
90 instance.augmenting = augmenting;
91 instance.addedByUses = addedByUses;
92 instance.configuration = configuration;
93 instance.constraints = constraints.toInstance();
94 instance.presence = presence;
97 if (originalNode == null && originalBuilder != null) {
98 originalNode = originalBuilder.build();
100 instance.original = originalNode;
103 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
104 augmentations.add(builder.build());
106 instance.augmentations = ImmutableSet.copyOf(augmentations);
109 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
110 unknownNodes.add(b.build());
112 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
117 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
118 return augmentationBuilders;
122 public void addAugmentation(final AugmentationSchemaBuilder augment) {
123 augmentationBuilders.add(augment);
127 public SchemaPath getPath() {
132 public void setPath(final SchemaPath path) {
137 public boolean isAugmenting() {
142 public void setAugmenting(final boolean augmenting) {
143 this.augmenting = augmenting;
147 public boolean isAddedByUses() {
152 public void setAddedByUses(final boolean addedByUses) {
153 this.addedByUses = addedByUses;
157 public ContainerSchemaNodeBuilder getOriginal() {
158 return originalBuilder;
162 public void setOriginal(final SchemaNodeBuilder builder) {
163 Preconditions.checkArgument(builder instanceof ContainerSchemaNodeBuilder, "Original of container cannot be "
165 this.originalBuilder = (ContainerSchemaNodeBuilder) builder;
169 public boolean isConfiguration() {
170 return configuration;
174 public void setConfiguration(final boolean configuration) {
175 this.configuration = configuration;
179 public ConstraintsBuilder getConstraints() {
183 public boolean isPresence() {
187 public void setPresence(final boolean presence) {
188 this.presence = presence;
192 public int hashCode() {
193 final int prime = 31;
195 result = prime * result + ((path == null) ? 0 : path.hashCode());
200 public boolean equals(final Object obj) {
207 if (getClass() != obj.getClass()) {
210 ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
212 if (other.path != null) {
215 } else if (!path.equals(other.path)) {
218 if (getParent() == null) {
219 if (other.getParent() != null) {
222 } else if (!getParent().equals(other.getParent())) {
229 public String toString() {
230 return "container " + qname.getLocalName();
233 private static final class ContainerSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements
234 ContainerSchemaNode, DerivableSchemaNode {
235 private final QName qname;
236 private final SchemaPath path;
238 private boolean augmenting;
239 private boolean addedByUses;
240 private boolean configuration;
241 private ContainerSchemaNode original;
242 private ConstraintDefinition constraints;
244 private ImmutableSet<AugmentationSchema> augmentations;
245 private ImmutableList<UnknownSchemaNode> unknownNodes;
247 private boolean presence;
249 public ContainerSchemaNodeImpl(final ContainerSchemaNodeBuilder builder) {
251 this.qname = builder.getQName();
252 this.path = builder.getPath();
256 public QName getQName() {
261 public SchemaPath getPath() {
266 public boolean isAugmenting() {
271 public boolean isAddedByUses() {
276 public Optional<ContainerSchemaNode> getOriginal() {
277 return Optional.fromNullable(original);
281 public boolean isConfiguration() {
282 return configuration;
286 public ConstraintDefinition getConstraints() {
291 public Set<AugmentationSchema> getAvailableAugmentations() {
292 return augmentations;
296 public boolean isPresenceContainer() {
301 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
306 public int hashCode() {
307 final int prime = 31;
309 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
310 result = prime * result + ((path == null) ? 0 : path.hashCode());
315 public boolean equals(final Object obj) {
322 if (getClass() != obj.getClass()) {
325 ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
327 if (other.qname != null) {
330 } else if (!qname.equals(other.qname)) {
334 if (other.path != null) {
337 } else if (!path.equals(other.path)) {
344 public String toString() {
345 return "container " + qname.getLocalName();