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;
14 import java.util.ArrayList;
15 import java.util.Date;
16 import java.util.List;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
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.UnknownSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
30 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
32 public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DataSchemaNodeBuilder,
33 AugmentationTargetBuilder {
34 private ListSchemaNodeImpl instance;
35 private boolean userOrdered;
36 private List<String> keys;
37 private List<QName> keyDefinition;
39 private SchemaPath schemaPath;
40 // DataSchemaNode args
41 private boolean augmenting;
42 private boolean addedByUses;
43 private boolean configuration;
44 private final ConstraintsBuilder constraints;
45 // AugmentationTarget args
46 private final List<AugmentationSchema> augmentations = new ArrayList<>();
47 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
49 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
50 super(moduleName, line, qname);
51 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
52 constraints = new ConstraintsBuilderImpl(moduleName, line);
55 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
56 final ListSchemaNode base) {
57 super(moduleName, line, qname,path,base);
58 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
59 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
61 keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
62 userOrdered = base.isUserOrdered();
64 augmenting = base.isAugmenting();
65 addedByUses = base.isAddedByUses();
66 configuration = base.isConfiguration();
68 URI ns = qname.getNamespace();
69 Date rev = qname.getRevision();
70 String pref = qname.getPrefix();
71 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
74 augmentations.addAll(base.getAvailableAugmentations());
78 public ListSchemaNode build() {
79 if (instance != null) {
83 instance = new ListSchemaNodeImpl(qname, schemaPath,this);
85 instance.augmenting = augmenting;
86 instance.addedByUses = addedByUses;
87 instance.configuration = configuration;
88 instance.constraints = constraints.toInstance();
89 instance.userOrdered = userOrdered;
93 instance.keyDefinition = ImmutableList.of();
95 keyDefinition = new ArrayList<>();
96 for (String key : keys) {
97 keyDefinition.add(instance.getDataChildByName(key).getQName());
99 instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
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);
118 protected String getStatementName() {
123 public SchemaPath getPath() {
128 public void setPath(final SchemaPath path) {
129 this.schemaPath = path;
133 public void addAugmentation(final AugmentationSchemaBuilder augment) {
134 augmentationBuilders.add(augment);
137 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
138 return augmentationBuilders;
141 public List<String> getKeys() {
145 public void setKeys(final List<String> keys) {
150 public boolean isAugmenting() {
155 public void setAugmenting(final boolean augmenting) {
156 this.augmenting = augmenting;
160 public boolean isAddedByUses() {
165 public void setAddedByUses(final boolean addedByUses) {
166 this.addedByUses = addedByUses;
170 public boolean isConfiguration() {
171 return configuration;
175 public void setConfiguration(final boolean configuration) {
176 this.configuration = configuration;
180 public ConstraintsBuilder getConstraints() {
184 public boolean isUserOrdered() {
188 public void setUserOrdered(final boolean userOrdered) {
189 this.userOrdered = userOrdered;
193 public int hashCode() {
194 final int prime = 31;
196 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
201 public boolean equals(final Object obj) {
208 if (getClass() != obj.getClass()) {
211 ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
212 if (schemaPath == null) {
213 if (other.schemaPath != null) {
216 } else if (!schemaPath.equals(other.schemaPath)) {
219 if (getParent() == null) {
220 if (other.getParent() != null) {
223 } else if (!getParent().equals(other.getParent())) {
230 public String toString() {
231 return "list " + qname.getLocalName();
234 private static final class ListSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements ListSchemaNode {
235 private final QName qname;
236 private final SchemaPath path;
237 private ImmutableList<QName> keyDefinition;
238 private boolean augmenting;
239 private boolean addedByUses;
240 private boolean configuration;
241 private ConstraintDefinition constraints;
242 private ImmutableSet<AugmentationSchema> augmentations;
243 private ImmutableList<UnknownSchemaNode> unknownNodes;
244 private boolean userOrdered;
246 private ListSchemaNodeImpl(final QName qname, final SchemaPath path, final ListSchemaNodeBuilder builder) {
253 public QName getQName() {
258 public SchemaPath getPath() {
263 public List<QName> getKeyDefinition() {
264 return keyDefinition;
268 public boolean isAugmenting() {
273 public boolean isAddedByUses() {
278 public boolean isConfiguration() {
279 return configuration;
283 public ConstraintDefinition getConstraints() {
288 public Set<AugmentationSchema> getAvailableAugmentations() {
289 return augmentations;
293 public boolean isUserOrdered() {
298 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
303 public int hashCode() {
304 final int prime = 31;
306 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
307 result = prime * result + ((path == null) ? 0 : path.hashCode());
312 public boolean equals(final Object obj) {
319 if (getClass() != obj.getClass()) {
322 final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
324 if (other.qname != null) {
327 } else if (!qname.equals(other.qname)) {
331 if (other.path != null) {
334 } else if (!path.equals(other.path)) {
341 public String toString() {
342 return "list " + qname.getLocalName();