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.AbstractTypeAwareBuilder;
20 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
21 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
23 import com.google.common.base.Preconditions;
24 import com.google.common.collect.ImmutableList;
26 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
27 private LeafSchemaNodeImpl instance;
28 private String defaultStr;
29 private String unitsStr;
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 public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
42 super(moduleName, line, qname);
43 this.schemaPath = schemaPath;
44 constraints = new ConstraintsBuilder(moduleName, line);
47 public LeafSchemaNodeBuilder(String moduleName, int line, QName qname, SchemaPath path, LeafSchemaNode base) {
48 super(moduleName, line, qname);
49 this.schemaPath = path;
50 constraints = new ConstraintsBuilder(moduleName, line, base.getConstraints());
52 description = base.getDescription();
53 reference = base.getReference();
54 status = base.getStatus();
55 augmenting = base.isAugmenting();
56 addedByUses = base.isAddedByUses();
57 configuration = base.isConfiguration();
58 this.type = base.getType();
59 unknownNodes.addAll(base.getUnknownSchemaNodes());
61 defaultStr = base.getDefault();
62 unitsStr = base.getUnits();
66 public LeafSchemaNode build() {
67 if (instance != null) {
71 instance = new LeafSchemaNodeImpl(qname, schemaPath);
73 instance.description = description;
74 instance.reference = reference;
75 instance.status = status;
76 instance.augmenting = augmenting;
77 instance.addedByUses = addedByUses;
78 instance.configuration = configuration;
79 instance.constraintsDef = constraints.build();
80 instance.defaultStr = defaultStr;
81 instance.unitsStr = unitsStr;
83 if (type == null && typedef == null) {
84 throw new YangParseException(moduleName, line, "Failed to resolve leaf type.");
89 instance.type = typedef.build();
95 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
96 unknownNodes.add(b.build());
98 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
104 public SchemaPath getPath() {
109 public void setPath(SchemaPath path) {
110 this.schemaPath = path;
114 public ConstraintsBuilder getConstraints() {
119 public String getDescription() {
124 public void setDescription(final String description) {
125 this.description = description;
129 public String getReference() {
134 public void setReference(final String reference) {
135 this.reference = reference;
139 public Status getStatus() {
144 public void setStatus(Status status) {
145 this.status = Preconditions.checkNotNull(status, "status cannot be null");
149 public boolean isAugmenting() {
154 public void setAugmenting(final boolean augmenting) {
155 this.augmenting = augmenting;
159 public boolean isAddedByUses() {
164 public void setAddedByUses(final boolean addedByUses) {
165 this.addedByUses = addedByUses;
169 public boolean isConfiguration() {
170 return configuration;
174 public void setConfiguration(final boolean configuration) {
175 this.configuration = configuration;
178 public String getDefaultStr() {
182 public void setDefaultStr(String defaultStr) {
183 this.defaultStr = defaultStr;
186 public String getUnits() {
190 public void setUnits(String unitsStr) {
191 this.unitsStr = unitsStr;
195 public int hashCode() {
196 final int prime = 31;
198 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
203 public boolean equals(Object obj) {
210 if (getClass() != obj.getClass()) {
213 LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
214 if (schemaPath == null) {
215 if (other.schemaPath != null) {
218 } else if (!schemaPath.equals(other.schemaPath)) {
221 if (parentBuilder == null) {
222 if (other.parentBuilder != null) {
225 } else if (!parentBuilder.equals(other.parentBuilder)) {
232 public String toString() {
233 return "leaf " + qname.getLocalName();
236 private static final class LeafSchemaNodeImpl implements LeafSchemaNode {
237 private final QName qname;
238 private final SchemaPath path;
239 private String description;
240 private String reference;
241 private Status status;
242 private boolean augmenting;
243 private boolean addedByUses;
244 private boolean configuration;
245 private ConstraintDefinition constraintsDef;
246 private TypeDefinition<?> type;
247 private ImmutableList<UnknownSchemaNode> unknownNodes;
248 private String defaultStr;
249 private String unitsStr;
251 private LeafSchemaNodeImpl(final QName qname, final SchemaPath path) {
257 public QName getQName() {
262 public SchemaPath getPath() {
267 public String getDescription() {
272 public String getReference() {
277 public Status getStatus() {
282 public boolean isAugmenting() {
287 public boolean isAddedByUses() {
292 public boolean isConfiguration() {
293 return configuration;
297 public ConstraintDefinition getConstraints() {
298 return constraintsDef;
302 public TypeDefinition<?> getType() {
307 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
312 public String getDefault() {
317 public String getUnits() {
322 public int hashCode() {
323 final int prime = 31;
325 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
326 result = prime * result + ((path == null) ? 0 : path.hashCode());
331 public boolean equals(Object obj) {
338 if (getClass() != obj.getClass()) {
341 LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
343 if (other.qname != null) {
346 } else if (!qname.equals(other.qname)) {
350 if (other.path != null) {
353 } else if (!path.equals(other.path)) {
360 public String toString() {
361 StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
363 sb.append("qname=" + qname);
364 sb.append(", path=" + path);
366 return sb.toString();