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