Merge "Renamed getLength() method to length() in classes generated from typedefs."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / LeafSchemaNodeBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
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.util.Comparators;
24 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
25
26 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
27     private boolean isBuilt;
28     private final LeafSchemaNodeImpl instance;
29     // SchemaNode args
30     private SchemaPath schemaPath;
31     // DataSchemaNode args
32     private final ConstraintsBuilder constraints;
33
34     public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
35         super(moduleName, line, qname);
36         this.schemaPath = schemaPath;
37         instance = new LeafSchemaNodeImpl(qname, schemaPath);
38         constraints = new ConstraintsBuilder(moduleName, line);
39     }
40
41     public LeafSchemaNodeBuilder(String moduleName, int line, QName qname, SchemaPath path, LeafSchemaNode base) {
42         super(moduleName, line, qname);
43         this.schemaPath = path;
44         instance = new LeafSchemaNodeImpl(qname, path);
45         constraints = new ConstraintsBuilder(moduleName, line, base.getConstraints());
46
47         instance.description = base.getDescription();
48         instance.reference = base.getReference();
49         instance.status = base.getStatus();
50         instance.augmenting = base.isAugmenting();
51         instance.addedByUses = base.isAddedByUses();
52         instance.configuration = base.isConfiguration();
53         instance.constraintsDef = base.getConstraints();
54         this.type = base.getType();
55         instance.unknownNodes.addAll(base.getUnknownSchemaNodes());
56         instance.defaultStr = base.getDefault();
57         instance.unitsStr = base.getUnits();
58     }
59
60     @Override
61     public LeafSchemaNode build() {
62         if (!isBuilt) {
63             instance.setConstraints(constraints.build());
64
65             if (type == null && typedef == null) {
66                 throw new YangParseException(moduleName, line, "Failed to resolve leaf type.");
67             }
68
69             // TYPE
70             if (type == null) {
71                 instance.setType(typedef.build());
72             } else {
73                 instance.setType(type);
74             }
75
76             // UNKNOWN NODES
77             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
78                 unknownNodes.add(b.build());
79             }
80             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
81             instance.addUnknownSchemaNodes(unknownNodes);
82
83             isBuilt = true;
84         }
85         return instance;
86     }
87
88
89     @Override
90     public SchemaPath getPath() {
91         return schemaPath;
92     }
93
94     @Override
95     public void setPath(SchemaPath path) {
96         instance.path = path;
97     }
98
99     @Override
100     public ConstraintsBuilder getConstraints() {
101         return constraints;
102     }
103
104     @Override
105     public String getDescription() {
106         return instance.description;
107     }
108
109     @Override
110     public void setDescription(final String description) {
111         instance.description = description;
112     }
113
114     @Override
115     public String getReference() {
116         return instance.reference;
117     }
118
119     @Override
120     public void setReference(final String reference) {
121         instance.reference = reference;
122     }
123
124     @Override
125     public Status getStatus() {
126         return instance.status;
127     }
128
129     @Override
130     public void setStatus(Status status) {
131         if (status != null) {
132             instance.status = status;
133         }
134     }
135
136     @Override
137     public boolean isAugmenting() {
138         return instance.augmenting;
139     }
140
141     @Override
142     public void setAugmenting(final boolean augmenting) {
143         instance.augmenting = augmenting;
144     }
145
146     @Override
147     public boolean isAddedByUses() {
148         return instance.addedByUses;
149     }
150
151     @Override
152     public void setAddedByUses(final boolean addedByUses) {
153         instance.addedByUses = addedByUses;
154     }
155
156     @Override
157     public Boolean isConfiguration() {
158         return instance.configuration;
159     }
160
161     @Override
162     public void setConfiguration(final Boolean configuration) {
163         instance.configuration = configuration;
164     }
165
166     public String getDefaultStr() {
167         return instance.defaultStr;
168     }
169
170     public void setDefaultStr(String defaultStr) {
171         instance.defaultStr = defaultStr;
172     }
173
174     public String getUnits() {
175         return instance.unitsStr;
176     }
177
178     public void setUnits(String unitsStr) {
179         instance.unitsStr = unitsStr;
180     }
181
182     @Override
183     public int hashCode() {
184         final int prime = 31;
185         int result = 1;
186         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
187         return result;
188     }
189
190     @Override
191     public boolean equals(Object obj) {
192         if (this == obj) {
193             return true;
194         }
195         if (obj == null) {
196             return false;
197         }
198         if (getClass() != obj.getClass()) {
199             return false;
200         }
201         LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
202         if (schemaPath == null) {
203             if (other.schemaPath != null) {
204                 return false;
205             }
206         } else if (!schemaPath.equals(other.schemaPath)) {
207             return false;
208         }
209         if (parentBuilder == null) {
210             if (other.parentBuilder != null) {
211                 return false;
212             }
213         } else if (!parentBuilder.equals(other.parentBuilder)) {
214             return false;
215         }
216         return true;
217     }
218
219     @Override
220     public String toString() {
221         return "leaf " + qname.getLocalName();
222     }
223
224     private static final class LeafSchemaNodeImpl implements LeafSchemaNode {
225         private final QName qname;
226         private SchemaPath path;
227         private String description;
228         private String reference;
229         private Status status = Status.CURRENT;
230         private boolean augmenting;
231         private boolean addedByUses;
232         private boolean configuration;
233         private ConstraintDefinition constraintsDef;
234         private TypeDefinition<?> type;
235         private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
236         private String defaultStr;
237         private String unitsStr;
238
239         private LeafSchemaNodeImpl(final QName qname, final SchemaPath path) {
240             this.qname = qname;
241             this.path = path;
242         }
243
244         @Override
245         public QName getQName() {
246             return qname;
247         }
248
249         @Override
250         public SchemaPath getPath() {
251             return path;
252         }
253
254         @Override
255         public String getDescription() {
256             return description;
257         }
258
259         @Override
260         public String getReference() {
261             return reference;
262         }
263
264         @Override
265         public Status getStatus() {
266             return status;
267         }
268
269         @Override
270         public boolean isAugmenting() {
271             return augmenting;
272         }
273
274         @Override
275         public boolean isAddedByUses() {
276             return addedByUses;
277         }
278
279         @Override
280         public boolean isConfiguration() {
281             return configuration;
282         }
283
284         @Override
285         public ConstraintDefinition getConstraints() {
286             return constraintsDef;
287         }
288
289         private void setConstraints(ConstraintDefinition constraintsDef) {
290             this.constraintsDef = constraintsDef;
291         }
292
293         @Override
294         public TypeDefinition<?> getType() {
295             return type;
296         }
297
298         private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
299             this.type = type;
300         }
301
302         @Override
303         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
304             return Collections.unmodifiableList(unknownNodes);
305         }
306
307         private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
308             if (unknownNodes != null) {
309                 this.unknownNodes.addAll(unknownNodes);
310             }
311         }
312
313         @Override
314         public String getDefault() {
315             return defaultStr;
316         }
317
318         @Override
319         public String getUnits() {
320             return unitsStr;
321         }
322
323         @Override
324         public int hashCode() {
325             final int prime = 31;
326             int result = 1;
327             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
328             result = prime * result + ((path == null) ? 0 : path.hashCode());
329             return result;
330         }
331
332         @Override
333         public boolean equals(Object obj) {
334             if (this == obj) {
335                 return true;
336             }
337             if (obj == null) {
338                 return false;
339             }
340             if (getClass() != obj.getClass()) {
341                 return false;
342             }
343             LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
344             if (qname == null) {
345                 if (other.qname != null) {
346                     return false;
347                 }
348             } else if (!qname.equals(other.qname)) {
349                 return false;
350             }
351             if (path == null) {
352                 if (other.path != null) {
353                     return false;
354                 }
355             } else if (!path.equals(other.path)) {
356                 return false;
357             }
358             return true;
359         }
360
361         @Override
362         public String toString() {
363             StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
364             sb.append("[");
365             sb.append("qname=" + qname);
366             sb.append(", path=" + path);
367             sb.append("]");
368             return sb.toString();
369         }
370     }
371
372 }