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;
25 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
27 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder,
29 private boolean isBuilt;
30 private final LeafSchemaNodeImpl instance;
32 private SchemaPath schemaPath;
33 private String description;
34 private String reference;
35 private Status status = Status.CURRENT;
36 // DataSchemaNode args
37 private boolean augmenting;
38 private boolean addedByUses;
39 private Boolean configuration;
40 private final ConstraintsBuilder constraints;
42 private String defaultStr;
43 private String unitsStr;
45 public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
46 super(moduleName, line, qname);
47 this.schemaPath = schemaPath;
48 instance = new LeafSchemaNodeImpl(qname);
49 constraints = new ConstraintsBuilder(moduleName, line);
53 public LeafSchemaNode build() {
55 instance.setPath(schemaPath);
56 instance.setConstraints(constraints.build());
57 instance.setDescription(description);
58 instance.setReference(reference);
59 instance.setStatus(status);
60 instance.setAugmenting(augmenting);
61 instance.setAddedByUses(addedByUses);
62 instance.setConfiguration(configuration);
63 instance.setDefault(defaultStr);
64 instance.setUnits(unitsStr);
66 if (type == null && typedef == null) {
67 throw new YangParseException(moduleName, line, "Failed to resolve leaf type.");
72 instance.setType(typedef.build());
74 instance.setType(type);
78 if (unknownNodes == null) {
79 unknownNodes = new ArrayList<UnknownSchemaNode>();
80 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
81 unknownNodes.add(b.build());
83 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
85 instance.setUnknownSchemaNodes(unknownNodes);
93 public void setQName(QName qname) {
98 public SchemaPath getPath() {
103 public void setPath(final SchemaPath path) {
104 this.schemaPath = path;
108 public ConstraintsBuilder getConstraints() {
113 public String getDescription() {
118 public void setDescription(final String description) {
119 this.description = description;
123 public String getReference() {
128 public void setReference(final String reference) {
129 this.reference = reference;
133 public Status getStatus() {
138 public void setStatus(final Status status) {
139 if (status != null) {
140 this.status = status;
145 public boolean isAugmenting() {
150 public void setAugmenting(final boolean augmenting) {
151 this.augmenting = augmenting;
155 public boolean isAddedByUses() {
160 public void setAddedByUses(final boolean addedByUses) {
161 this.addedByUses = addedByUses;
165 public Boolean isConfiguration() {
166 return configuration;
170 public void setConfiguration(final Boolean configuration) {
171 this.configuration = configuration;
174 public String getDefaultStr() {
178 public void setDefaultStr(String defaultStr) {
179 this.defaultStr = defaultStr;
182 public String getUnits() {
186 public void setUnits(String unitsStr) {
187 this.unitsStr = unitsStr;
191 public int hashCode() {
192 final int prime = 31;
194 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
199 public boolean equals(Object obj) {
206 if (getClass() != obj.getClass()) {
209 LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
210 if (schemaPath == null) {
211 if (other.schemaPath != null) {
214 } else if (!schemaPath.equals(other.schemaPath)) {
217 if (parent == null) {
218 if (other.parent != null) {
221 } else if (!parent.equals(other.parent)) {
228 public String toString() {
229 return "leaf " + qname.getLocalName();
232 private final class LeafSchemaNodeImpl implements LeafSchemaNode {
233 private final QName qname;
234 private SchemaPath path;
235 private String description;
236 private String reference;
237 private Status status = Status.CURRENT;
238 private boolean augmenting;
239 private boolean addedByUses;
240 private boolean configuration;
241 private ConstraintDefinition constraintsDef;
242 private TypeDefinition<?> type;
243 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
244 private String defaultStr;
245 private String unitsStr;
247 private LeafSchemaNodeImpl(final QName qname) {
252 public QName getQName() {
257 public SchemaPath getPath() {
261 private void setPath(final SchemaPath path) {
266 public String getDescription() {
270 private void setDescription(String description) {
271 this.description = description;
275 public String getReference() {
279 private void setReference(String reference) {
280 this.reference = reference;
284 public Status getStatus() {
288 private void setStatus(Status status) {
289 if (status != null) {
290 this.status = status;
295 public boolean isAugmenting() {
299 private void setAugmenting(boolean augmenting) {
300 this.augmenting = augmenting;
304 public boolean isAddedByUses() {
308 private void setAddedByUses(final boolean addedByUses) {
309 this.addedByUses = addedByUses;
313 public boolean isConfiguration() {
314 return configuration;
317 private void setConfiguration(boolean configuration) {
318 this.configuration = configuration;
322 public ConstraintDefinition getConstraints() {
323 return constraintsDef;
326 private void setConstraints(ConstraintDefinition constraintsDef) {
327 this.constraintsDef = constraintsDef;
331 public TypeDefinition<?> getType() {
335 private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
340 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
344 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
345 if (unknownNodes != null) {
346 this.unknownNodes = unknownNodes;
351 public String getDefault() {
355 private void setDefault(String defaultStr) {
356 this.defaultStr = defaultStr;
360 public String getUnits() {
364 public void setUnits(String unitsStr) {
365 this.unitsStr = unitsStr;
369 public int hashCode() {
370 final int prime = 31;
372 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
373 result = prime * result + ((path == null) ? 0 : path.hashCode());
378 public boolean equals(Object obj) {
385 if (getClass() != obj.getClass()) {
388 LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
390 if (other.qname != null) {
393 } else if (!qname.equals(other.qname)) {
397 if (other.path != null) {
400 } else if (!path.equals(other.path)) {
407 public String toString() {
408 StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
410 sb.append("qname=" + qname);
411 sb.append(", path=" + path);
413 return sb.toString();