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