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.List;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
14 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.Status;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
20 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractTypeAwareBuilder;
23 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
25 import com.google.common.base.Preconditions;
26 import com.google.common.collect.ImmutableList;
28 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
29 private LeafSchemaNodeImpl instance;
30 private String defaultStr;
31 private String unitsStr;
33 private SchemaPath schemaPath;
34 private String description;
35 private String reference;
36 private Status status = Status.CURRENT;
37 // DataSchemaNode args
38 private boolean augmenting;
39 private boolean addedByUses;
40 private boolean configuration;
41 private final ConstraintsBuilder constraints;
43 public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
44 super(moduleName, line, qname);
45 this.schemaPath = Preconditions.checkNotNull(schemaPath, "Schema Path must not be null");
46 constraints = new ConstraintsBuilderImpl(moduleName, line);
49 public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path, final LeafSchemaNode base) {
50 super(moduleName, line, qname);
51 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
52 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
54 description = base.getDescription();
55 reference = base.getReference();
56 status = base.getStatus();
57 augmenting = base.isAugmenting();
58 addedByUses = base.isAddedByUses();
59 configuration = base.isConfiguration();
60 this.type = base.getType();
61 unknownNodes.addAll(base.getUnknownSchemaNodes());
63 defaultStr = base.getDefault();
64 unitsStr = base.getUnits();
68 public LeafSchemaNode build() {
69 if (instance != null) {
73 instance = new LeafSchemaNodeImpl(qname, schemaPath);
75 instance.description = description;
76 instance.reference = reference;
77 instance.status = status;
78 instance.augmenting = augmenting;
79 instance.addedByUses = addedByUses;
80 instance.configuration = configuration;
81 instance.constraintsDef = constraints.toInstance();
82 instance.defaultStr = defaultStr;
83 instance.unitsStr = unitsStr;
85 if (type == null && typedef == null) {
86 throw new YangParseException(getModuleName(), getLine(), "Failed to resolve leaf type.");
91 instance.type = typedef.build();
97 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
98 unknownNodes.add(b.build());
100 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
106 public SchemaPath getPath() {
111 public void setPath(final SchemaPath path) {
112 this.schemaPath = path;
116 public ConstraintsBuilder getConstraints() {
121 public String getDescription() {
126 public void setDescription(final String description) {
127 this.description = description;
131 public String getReference() {
136 public void setReference(final String reference) {
137 this.reference = reference;
141 public Status getStatus() {
146 public void setStatus(final Status status) {
147 this.status = Preconditions.checkNotNull(status, "status cannot be null");
151 public boolean isAugmenting() {
156 public void setAugmenting(final boolean augmenting) {
157 this.augmenting = augmenting;
161 public boolean isAddedByUses() {
166 public void setAddedByUses(final boolean addedByUses) {
167 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(final String defaultStr) {
185 this.defaultStr = defaultStr;
188 public String getUnits() {
192 public void setUnits(final 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(final 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 (getParent() == null) {
224 if (other.getParent() != null) {
227 } else if (!getParent().equals(other.getParent())) {
234 public String toString() {
235 return "leaf " + qname.getLocalName();
238 private static final class LeafSchemaNodeImpl implements LeafSchemaNode {
239 private final QName qname;
240 private final SchemaPath path;
241 private String description;
242 private String reference;
243 private Status status;
244 private boolean augmenting;
245 private boolean addedByUses;
246 private boolean configuration;
247 private ConstraintDefinition constraintsDef;
248 private TypeDefinition<?> type;
249 private ImmutableList<UnknownSchemaNode> unknownNodes;
250 private String defaultStr;
251 private String unitsStr;
253 private LeafSchemaNodeImpl(final QName qname, final SchemaPath path) {
259 public QName getQName() {
264 public SchemaPath getPath() {
269 public String getDescription() {
274 public String getReference() {
279 public Status getStatus() {
284 public boolean isAugmenting() {
289 public boolean isAddedByUses() {
294 public boolean isConfiguration() {
295 return configuration;
299 public ConstraintDefinition getConstraints() {
300 return constraintsDef;
304 public TypeDefinition<?> getType() {
309 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
314 public String getDefault() {
319 public String getUnits() {
324 public int hashCode() {
325 final int prime = 31;
327 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
328 result = prime * result + ((path == null) ? 0 : path.hashCode());
333 public boolean equals(final Object obj) {
340 if (getClass() != obj.getClass()) {
343 LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
345 if (other.qname != null) {
348 } else if (!qname.equals(other.qname)) {
352 if (other.path != null) {
355 } else if (!path.equals(other.path)) {
362 public String toString() {
363 StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
365 sb.append("qname=" + qname);
366 sb.append(", path=" + path);
368 return sb.toString();