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