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