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 com.google.common.collect.ImmutableSet;
13 import java.util.ArrayList;
14 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.ConstraintDefinition;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
22 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
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 ContainerSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
31 AugmentationTargetBuilder, DataSchemaNodeBuilder {
32 private ContainerSchemaNodeImpl instance;
33 private boolean presence;
35 private SchemaPath path;
36 // DataSchemaNode args
37 private boolean augmenting;
38 private boolean addedByUses;
39 private boolean configuration;
40 private final ConstraintsBuilder constraints;
41 // AugmentationTarget args
42 private final List<AugmentationSchema> augmentations = new ArrayList<>();
43 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
45 public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
46 super(moduleName, line, qname);
47 this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
48 this.constraints = new ConstraintsBuilderImpl(moduleName, line);
51 // constructor for uses
52 public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname,
53 final SchemaPath path, final ContainerSchemaNode base) {
54 super(moduleName, line, qname, path, base);
55 this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
57 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
59 augmenting = base.isAugmenting();
60 addedByUses = base.isAddedByUses();
61 configuration = base.isConfiguration();
62 presence = base.isPresenceContainer();
64 augmentations.addAll(base.getAvailableAugmentations());
69 protected String getStatementName() {
74 public ContainerSchemaNode build() {
75 if (instance != null) {
80 instance = new ContainerSchemaNodeImpl(this);
82 instance.augmenting = augmenting;
83 instance.addedByUses = addedByUses;
84 instance.configuration = configuration;
85 instance.constraints = constraints.toInstance();
86 instance.presence = presence;
89 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
90 augmentations.add(builder.build());
92 instance.augmentations = ImmutableSet.copyOf(augmentations);
95 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
96 unknownNodes.add(b.build());
98 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
103 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
104 return augmentationBuilders;
108 public void addAugmentation(final AugmentationSchemaBuilder augment) {
109 augmentationBuilders.add(augment);
113 public SchemaPath getPath() {
118 public void setPath(final SchemaPath path) {
123 public boolean isAugmenting() {
128 public void setAugmenting(final boolean augmenting) {
129 this.augmenting = augmenting;
133 public boolean isAddedByUses() {
138 public void setAddedByUses(final boolean addedByUses) {
139 this.addedByUses = addedByUses;
143 public boolean isConfiguration() {
144 return configuration;
148 public void setConfiguration(final boolean configuration) {
149 this.configuration = configuration;
153 public ConstraintsBuilder getConstraints() {
157 public boolean isPresence() {
161 public void setPresence(final boolean presence) {
162 this.presence = presence;
166 public int hashCode() {
167 final int prime = 31;
169 result = prime * result + ((path == null) ? 0 : path.hashCode());
174 public boolean equals(final Object obj) {
181 if (getClass() != obj.getClass()) {
184 ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
186 if (other.path != null) {
189 } else if (!path.equals(other.path)) {
192 // FIXME: Do we really need this? This actually triggers equals
193 // up to the root builder.
194 if (getParent() == null) {
195 if (other.getParent() != null) {
198 } else if (!getParent().equals(other.getParent())) {
205 public String toString() {
206 return "container " + qname.getLocalName();
209 private static final class ContainerSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements
210 ContainerSchemaNode {
211 private final QName qname;
212 private final SchemaPath path;
214 private boolean augmenting;
215 private boolean addedByUses;
216 private boolean configuration;
217 private ConstraintDefinition constraints;
219 private ImmutableSet<AugmentationSchema> augmentations;
220 private ImmutableList<UnknownSchemaNode> unknownNodes;
222 private boolean presence;
224 public ContainerSchemaNodeImpl(final ContainerSchemaNodeBuilder builder) {
226 this.qname = builder.getQName();
227 this.path = builder.getPath();
231 public QName getQName() {
236 public SchemaPath getPath() {
241 public boolean isAugmenting() {
246 public boolean isAddedByUses() {
251 public boolean isConfiguration() {
252 return configuration;
256 public ConstraintDefinition getConstraints() {
261 public Set<AugmentationSchema> getAvailableAugmentations() {
262 return augmentations;
266 public boolean isPresenceContainer() {
271 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
276 public int hashCode() {
277 final int prime = 31;
279 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
280 result = prime * result + ((path == null) ? 0 : path.hashCode());
285 public boolean equals(final Object obj) {
292 if (getClass() != obj.getClass()) {
295 ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
297 if (other.qname != null) {
300 } else if (!qname.equals(other.qname)) {
304 if (other.path != null) {
307 } else if (!path.equals(other.path)) {
314 public String toString() {
315 return "container " + qname.getLocalName();