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