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