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