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.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
26 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
28 import com.google.common.base.Preconditions;
29 import com.google.common.collect.ImmutableList;
30 import com.google.common.collect.ImmutableSet;
32 public final class ContainerSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
33 AugmentationTargetBuilder, DataSchemaNodeBuilder {
34 private ContainerSchemaNodeImpl instance;
35 private boolean presence;
37 private SchemaPath path;
38 // DataSchemaNode args
39 private boolean augmenting;
40 private boolean addedByUses;
41 private boolean configuration;
42 private final ConstraintsBuilder constraints;
43 // AugmentationTarget args
44 private final List<AugmentationSchema> augmentations = new ArrayList<>();
45 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
47 public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
48 super(moduleName, line, qname);
49 this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
50 this.constraints = new ConstraintsBuilderImpl(moduleName, line);
53 // constructor for uses
54 public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname,
55 final SchemaPath path, final ContainerSchemaNode base) {
56 super(moduleName, line, qname, path, base);
57 this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
59 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
61 augmenting = base.isAugmenting();
62 addedByUses = base.isAddedByUses();
63 configuration = base.isConfiguration();
64 presence = base.isPresenceContainer();
66 augmentations.addAll(base.getAvailableAugmentations());
71 protected String getStatementName() {
76 public ContainerSchemaNode build() {
77 if (instance != null) {
82 instance = new ContainerSchemaNodeImpl(this);
84 instance.augmenting = augmenting;
85 instance.addedByUses = addedByUses;
86 instance.configuration = configuration;
87 instance.constraints = constraints.toInstance();
88 instance.presence = presence;
91 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
92 augmentations.add(builder.build());
94 instance.augmentations = ImmutableSet.copyOf(augmentations);
97 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
98 unknownNodes.add(b.build());
100 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
105 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
106 return augmentationBuilders;
110 public void addAugmentation(final AugmentationSchemaBuilder augment) {
111 augmentationBuilders.add(augment);
115 public SchemaPath getPath() {
120 public void setPath(final SchemaPath path) {
125 public boolean isAugmenting() {
130 public void setAugmenting(final boolean augmenting) {
131 this.augmenting = augmenting;
135 public boolean isAddedByUses() {
140 public void setAddedByUses(final boolean addedByUses) {
141 this.addedByUses = addedByUses;
145 public boolean isConfiguration() {
146 return configuration;
150 public void setConfiguration(final boolean configuration) {
151 this.configuration = configuration;
155 public ConstraintsBuilder getConstraints() {
159 public boolean isPresence() {
163 public void setPresence(final boolean presence) {
164 this.presence = presence;
168 public int hashCode() {
169 final int prime = 31;
171 result = prime * result + ((path == null) ? 0 : path.hashCode());
176 public boolean equals(final Object obj) {
183 if (getClass() != obj.getClass()) {
186 ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
188 if (other.path != null) {
191 } else if (!path.equals(other.path)) {
194 // FIXME: Do we really need this? This actually triggers equals
195 // up to the root builder.
196 if (getParent() == null) {
197 if (other.getParent() != null) {
200 } else if (!getParent().equals(other.getParent())) {
207 public String toString() {
208 return "container " + qname.getLocalName();
211 private static final class ContainerSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements
212 ContainerSchemaNode {
213 private final QName qname;
214 private final SchemaPath path;
216 private boolean augmenting;
217 private boolean addedByUses;
218 private boolean configuration;
219 private ConstraintDefinition constraints;
221 private ImmutableSet<AugmentationSchema> augmentations;
222 private ImmutableList<UnknownSchemaNode> unknownNodes;
224 private boolean presence;
226 public ContainerSchemaNodeImpl(final ContainerSchemaNodeBuilder builder) {
228 this.qname = builder.getQName();
229 this.path = builder.getPath();
233 public QName getQName() {
238 public SchemaPath getPath() {
243 public boolean isAugmenting() {
248 public boolean isAddedByUses() {
253 public boolean isConfiguration() {
254 return configuration;
258 public ConstraintDefinition getConstraints() {
263 public Set<AugmentationSchema> getAvailableAugmentations() {
264 return augmentations;
268 public boolean isPresenceContainer() {
273 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
278 public int hashCode() {
279 final int prime = 31;
281 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
282 result = prime * result + ((path == null) ? 0 : path.hashCode());
287 public boolean equals(final Object obj) {
294 if (getClass() != obj.getClass()) {
297 ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
299 if (other.qname != null) {
302 } else if (!qname.equals(other.qname)) {
306 if (other.path != null) {
309 } else if (!path.equals(other.path)) {
316 public String toString() {
317 return "container " + qname.getLocalName();