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.Collections;
12 import java.util.List;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
16 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.Status;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractTypeAwareBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingMember;
24 import org.opendaylight.yangtools.yang.parser.util.Comparators;
26 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder,
28 private boolean isBuilt;
29 private final LeafSchemaNodeImpl instance;
31 private SchemaPath schemaPath;
32 private String description;
33 private String reference;
34 private Status status = Status.CURRENT;
35 // DataSchemaNode args
36 private boolean augmenting;
37 private boolean addedByUses;
38 private Boolean configuration;
39 private final ConstraintsBuilder constraints;
41 private String defaultStr;
42 private String unitsStr;
44 public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
45 super(moduleName, line, qname);
46 this.schemaPath = schemaPath;
47 instance = new LeafSchemaNodeImpl(qname);
48 constraints = new ConstraintsBuilder(moduleName, line);
51 public LeafSchemaNodeBuilder(final LeafSchemaNodeBuilder b) {
52 super(b.getModuleName(), b.getLine(), b.getQName());
53 instance = new LeafSchemaNodeImpl(qname);
54 constraints = b.getConstraints();
55 schemaPath = b.getPath();
58 typedef = b.getTypedef();
60 description = b.getDescription();
61 reference = b.getReference();
62 status = b.getStatus();
63 augmenting = b.isAugmenting();
64 addedByUses = b.isAddedByUses();
65 configuration = b.isConfiguration();
66 unknownNodes = b.unknownNodes;
67 addedUnknownNodes.addAll(b.getUnknownNodeBuilders());
69 defaultStr = b.getDefaultStr();
70 unitsStr = b.getUnits();
74 public LeafSchemaNode build() {
76 instance.setPath(schemaPath);
77 instance.setConstraints(constraints.build());
78 instance.setDescription(description);
79 instance.setReference(reference);
80 instance.setStatus(status);
81 instance.setAugmenting(augmenting);
82 instance.setAddedByUses(addedByUses);
83 instance.setConfiguration(configuration);
84 instance.setDefault(defaultStr);
85 instance.setUnits(unitsStr);
89 instance.setType(typedef.build());
91 instance.setType(type);
95 if (unknownNodes == null) {
96 unknownNodes = new ArrayList<UnknownSchemaNode>();
97 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
98 unknownNodes.add(b.build());
100 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
102 instance.setUnknownSchemaNodes(unknownNodes);
109 public SchemaPath getPath() {
114 public void setPath(final SchemaPath path) {
115 this.schemaPath = path;
119 public ConstraintsBuilder getConstraints() {
123 public String getDescription() {
128 public void setDescription(final String description) {
129 this.description = description;
132 public String getReference() {
137 public void setReference(final String reference) {
138 this.reference = reference;
141 public Status getStatus() {
146 public void setStatus(final Status status) {
147 if (status != null) {
148 this.status = status;
152 public boolean isAugmenting() {
157 public void setAugmenting(final boolean augmenting) {
158 this.augmenting = augmenting;
162 public boolean isAddedByUses() {
167 public void setAddedByUses(final boolean addedByUses) {
168 this.addedByUses = addedByUses;
171 public Boolean isConfiguration() {
172 return configuration;
176 public void setConfiguration(final Boolean configuration) {
177 this.configuration = configuration;
180 public String getDefaultStr() {
184 public void setDefaultStr(String defaultStr) {
185 this.defaultStr = defaultStr;
188 public String getUnits() {
192 public void setUnits(String unitsStr) {
193 this.unitsStr = unitsStr;
197 public int hashCode() {
198 final int prime = 31;
200 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
205 public boolean equals(Object obj) {
212 if (getClass() != obj.getClass()) {
215 LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
216 if (schemaPath == null) {
217 if (other.schemaPath != null) {
220 } else if (!schemaPath.equals(other.schemaPath)) {
223 if (parent == null) {
224 if (other.parent != null) {
227 } else if (!parent.equals(other.parent)) {
234 public String toString() {
235 return "leaf " + qname.getLocalName();
238 private final class LeafSchemaNodeImpl implements LeafSchemaNode {
239 private final QName qname;
240 private SchemaPath path;
241 private String description;
242 private String reference;
243 private Status status = Status.CURRENT;
244 private boolean augmenting;
245 private boolean addedByUses;
246 private boolean configuration;
247 private ConstraintDefinition constraintsDef;
248 private TypeDefinition<?> type;
249 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
250 private String defaultStr;
251 private String unitsStr;
253 private LeafSchemaNodeImpl(final QName qname) {
258 public QName getQName() {
263 public SchemaPath getPath() {
267 private void setPath(final SchemaPath path) {
272 public String getDescription() {
276 private void setDescription(String description) {
277 this.description = description;
281 public String getReference() {
285 private void setReference(String reference) {
286 this.reference = reference;
290 public Status getStatus() {
294 private void setStatus(Status status) {
295 if (status != null) {
296 this.status = status;
301 public boolean isAugmenting() {
305 private void setAugmenting(boolean augmenting) {
306 this.augmenting = augmenting;
310 public boolean isAddedByUses() {
314 private void setAddedByUses(final boolean addedByUses) {
315 this.addedByUses = addedByUses;
319 public boolean isConfiguration() {
320 return configuration;
323 private void setConfiguration(boolean configuration) {
324 this.configuration = configuration;
328 public ConstraintDefinition getConstraints() {
329 return constraintsDef;
332 private void setConstraints(ConstraintDefinition constraintsDef) {
333 this.constraintsDef = constraintsDef;
337 public TypeDefinition<?> getType() {
341 private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
346 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
350 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
351 if (unknownNodes != null) {
352 this.unknownNodes = unknownNodes;
357 public String getDefault() {
361 private void setDefault(String defaultStr) {
362 this.defaultStr = defaultStr;
366 public String getUnits() {
370 public void setUnits(String unitsStr) {
371 this.unitsStr = unitsStr;
375 public int hashCode() {
376 final int prime = 31;
378 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
379 result = prime * result + ((path == null) ? 0 : path.hashCode());
384 public boolean equals(Object obj) {
391 if (getClass() != obj.getClass()) {
394 LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
396 if (other.qname != null) {
399 } else if (!qname.equals(other.qname)) {
403 if (other.path != null) {
406 } else if (!path.equals(other.path)) {
413 public String toString() {
414 StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
416 sb.append("qname=" + qname);
417 sb.append(", path=" + path);
419 return sb.toString();