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.DerivableSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
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 ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DataSchemaNodeBuilder,
36 AugmentationTargetBuilder {
37 private ListSchemaNodeImpl instance;
38 private boolean userOrdered;
39 private List<String> keys;
40 private List<QName> keyDefinition;
42 private SchemaPath schemaPath;
43 // DataSchemaNode args
44 private boolean augmenting;
45 private boolean addedByUses;
46 private ListSchemaNodeBuilder originalBuilder;
47 private ListSchemaNode originalNode;
48 private boolean configuration;
49 private final ConstraintsBuilder constraints;
50 // AugmentationTarget args
51 private final List<AugmentationSchema> augmentations = new ArrayList<>();
52 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
54 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
55 super(moduleName, line, qname);
56 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
57 constraints = new ConstraintsBuilderImpl(moduleName, line);
60 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
61 final ListSchemaNode base) {
62 super(moduleName, line, qname,path,base);
63 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
64 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
66 keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
67 userOrdered = base.isUserOrdered();
69 augmenting = base.isAugmenting();
70 addedByUses = base.isAddedByUses();
72 configuration = base.isConfiguration();
74 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
76 augmentations.addAll(base.getAvailableAugmentations());
80 public ListSchemaNode build() {
81 if (instance != null) {
85 instance = new ListSchemaNodeImpl(qname, schemaPath,this);
87 instance.augmenting = augmenting;
88 instance.addedByUses = addedByUses;
89 instance.configuration = configuration;
90 instance.constraints = constraints.toInstance();
91 instance.userOrdered = userOrdered;
95 instance.keyDefinition = ImmutableList.of();
97 keyDefinition = new ArrayList<>();
98 for (String key : keys) {
99 keyDefinition.add(instance.getDataChildByName(key).getQName());
101 instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
105 if (originalNode == null && originalBuilder != null) {
106 originalNode = originalBuilder.build();
108 instance.original = originalNode;
111 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
112 augmentations.add(builder.build());
114 instance.augmentations = ImmutableSet.copyOf(augmentations);
117 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
118 unknownNodes.add(b.build());
120 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
126 protected String getStatementName() {
131 public SchemaPath getPath() {
136 public void setPath(final SchemaPath path) {
137 this.schemaPath = path;
141 public void addAugmentation(final AugmentationSchemaBuilder augment) {
142 augmentationBuilders.add(augment);
145 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
146 return augmentationBuilders;
149 public List<String> getKeys() {
153 public void setKeys(final List<String> keys) {
158 public boolean isAugmenting() {
163 public void setAugmenting(final boolean augmenting) {
164 this.augmenting = augmenting;
168 public boolean isAddedByUses() {
173 public void setAddedByUses(final boolean addedByUses) {
174 this.addedByUses = addedByUses;
178 public ListSchemaNodeBuilder getOriginal() {
179 return originalBuilder;
183 public void setOriginal(final SchemaNodeBuilder builder) {
184 Preconditions.checkArgument(builder instanceof ListSchemaNodeBuilder, "Original of list cannot be " + builder);
185 this.originalBuilder = (ListSchemaNodeBuilder) builder;
189 public boolean isConfiguration() {
190 return configuration;
194 public void setConfiguration(final boolean configuration) {
195 this.configuration = configuration;
199 public ConstraintsBuilder getConstraints() {
203 public boolean isUserOrdered() {
207 public void setUserOrdered(final boolean userOrdered) {
208 this.userOrdered = userOrdered;
212 public int hashCode() {
213 final int prime = 31;
215 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
220 public boolean equals(final Object obj) {
227 if (getClass() != obj.getClass()) {
230 ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
231 if (schemaPath == null) {
232 if (other.schemaPath != null) {
235 } else if (!schemaPath.equals(other.schemaPath)) {
238 if (getParent() == null) {
239 if (other.getParent() != null) {
242 } else if (!getParent().equals(other.getParent())) {
249 public String toString() {
250 return "list " + qname.getLocalName();
253 private static final class ListSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements ListSchemaNode, DerivableSchemaNode {
254 private final QName qname;
255 private final SchemaPath path;
256 private ImmutableList<QName> keyDefinition;
257 private boolean augmenting;
258 private boolean addedByUses;
259 private ListSchemaNode original;
260 private boolean configuration;
261 private ConstraintDefinition constraints;
262 private ImmutableSet<AugmentationSchema> augmentations;
263 private ImmutableList<UnknownSchemaNode> unknownNodes;
264 private boolean userOrdered;
266 private ListSchemaNodeImpl(final QName qname, final SchemaPath path, final ListSchemaNodeBuilder builder) {
273 public QName getQName() {
278 public SchemaPath getPath() {
283 public List<QName> getKeyDefinition() {
284 return keyDefinition;
288 public boolean isAugmenting() {
293 public boolean isAddedByUses() {
298 public Optional<ListSchemaNode> getOriginal() {
299 return Optional.fromNullable(original);
303 public boolean isConfiguration() {
304 return configuration;
308 public ConstraintDefinition getConstraints() {
313 public Set<AugmentationSchema> getAvailableAugmentations() {
314 return augmentations;
318 public boolean isUserOrdered() {
323 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
328 public int hashCode() {
329 final int prime = 31;
331 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
332 result = prime * result + ((path == null) ? 0 : path.hashCode());
337 public boolean equals(final Object obj) {
344 if (getClass() != obj.getClass()) {
347 final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
349 if (other.qname != null) {
352 } else if (!qname.equals(other.qname)) {
356 if (other.path != null) {
359 } else if (!path.equals(other.path)) {
366 public String toString() {
367 return "list " + qname.getLocalName();