BUG-1276: fixed generated union constructor
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ListSchemaNodeBuilder.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 com.google.common.collect.ImmutableSet;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Set;
17
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
30 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
31
32 public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DataSchemaNodeBuilder,
33 AugmentationTargetBuilder {
34     private ListSchemaNodeImpl instance;
35     private boolean userOrdered;
36     private List<String> keys;
37     private List<QName> keyDefinition;
38     // SchemaNode args
39     private SchemaPath schemaPath;
40     // DataSchemaNode args
41     private boolean augmenting;
42     private boolean addedByUses;
43     private boolean configuration;
44     private final ConstraintsBuilder constraints;
45     // AugmentationTarget args
46     private final List<AugmentationSchema> augmentations = new ArrayList<>();
47     private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
48
49     public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
50         super(moduleName, line, qname);
51         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
52         constraints = new ConstraintsBuilderImpl(moduleName, line);
53     }
54
55     public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
56             final ListSchemaNode base) {
57         super(moduleName, line, qname,path,base);
58         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
59         constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
60
61         keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
62         userOrdered = base.isUserOrdered();
63
64         augmenting = base.isAugmenting();
65         addedByUses = base.isAddedByUses();
66         configuration = base.isConfiguration();
67
68         addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
69                 qname));
70         augmentations.addAll(base.getAvailableAugmentations());
71     }
72
73     @Override
74     public ListSchemaNode build() {
75         if (instance != null) {
76             return instance;
77         }
78         buildChildren();
79         instance = new ListSchemaNodeImpl(qname, schemaPath,this);
80
81         instance.augmenting = augmenting;
82         instance.addedByUses = addedByUses;
83         instance.configuration = configuration;
84         instance.constraints = constraints.toInstance();
85         instance.userOrdered = userOrdered;
86
87         // KEY
88         if (keys == null) {
89             instance.keyDefinition = ImmutableList.of();
90         } else {
91             keyDefinition = new ArrayList<>();
92             for (String key : keys) {
93                 keyDefinition.add(instance.getDataChildByName(key).getQName());
94             }
95             instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
96         }
97
98         // AUGMENTATIONS
99         for (AugmentationSchemaBuilder builder : augmentationBuilders) {
100             augmentations.add(builder.build());
101         }
102         instance.augmentations = ImmutableSet.copyOf(augmentations);
103
104         // UNKNOWN NODES
105         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
106             unknownNodes.add(b.build());
107         }
108         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
109
110         return instance;
111     }
112
113     @Override
114     protected String getStatementName() {
115         return "list";
116     }
117
118     @Override
119     public SchemaPath getPath() {
120         return schemaPath;
121     }
122
123     @Override
124     public void setPath(final SchemaPath path) {
125         this.schemaPath = path;
126     }
127
128     @Override
129     public void addAugmentation(final AugmentationSchemaBuilder augment) {
130         augmentationBuilders.add(augment);
131     }
132
133     public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
134         return augmentationBuilders;
135     }
136
137     public List<String> getKeys() {
138         return keys;
139     }
140
141     public void setKeys(final List<String> keys) {
142         this.keys = keys;
143     }
144
145     @Override
146     public boolean isAugmenting() {
147         return augmenting;
148     }
149
150     @Override
151     public void setAugmenting(final boolean augmenting) {
152         this.augmenting = augmenting;
153     }
154
155     @Override
156     public boolean isAddedByUses() {
157         return addedByUses;
158     }
159
160     @Override
161     public void setAddedByUses(final boolean addedByUses) {
162         this.addedByUses = addedByUses;
163     }
164
165     @Override
166     public boolean isConfiguration() {
167         return configuration;
168     }
169
170     @Override
171     public void setConfiguration(final boolean configuration) {
172         this.configuration = configuration;
173     }
174
175     @Override
176     public ConstraintsBuilder getConstraints() {
177         return constraints;
178     }
179
180     public boolean isUserOrdered() {
181         return userOrdered;
182     }
183
184     public void setUserOrdered(final boolean userOrdered) {
185         this.userOrdered = userOrdered;
186     }
187
188     @Override
189     public int hashCode() {
190         final int prime = 31;
191         int result = 1;
192         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
193         return result;
194     }
195
196     @Override
197     public boolean equals(final Object obj) {
198         if (this == obj) {
199             return true;
200         }
201         if (obj == null) {
202             return false;
203         }
204         if (getClass() != obj.getClass()) {
205             return false;
206         }
207         ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
208         if (schemaPath == null) {
209             if (other.schemaPath != null) {
210                 return false;
211             }
212         } else if (!schemaPath.equals(other.schemaPath)) {
213             return false;
214         }
215         if (getParent() == null) {
216             if (other.getParent() != null) {
217                 return false;
218             }
219         } else if (!getParent().equals(other.getParent())) {
220             return false;
221         }
222         return true;
223     }
224
225     @Override
226     public String toString() {
227         return "list " + qname.getLocalName();
228     }
229
230     private static final class ListSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements ListSchemaNode {
231         private final QName qname;
232         private final SchemaPath path;
233         private ImmutableList<QName> keyDefinition;
234         private boolean augmenting;
235         private boolean addedByUses;
236         private boolean configuration;
237         private ConstraintDefinition constraints;
238         private ImmutableSet<AugmentationSchema> augmentations;
239         private ImmutableList<UnknownSchemaNode> unknownNodes;
240         private boolean userOrdered;
241
242         private ListSchemaNodeImpl(final QName qname, final SchemaPath path, final ListSchemaNodeBuilder builder) {
243             super(builder);
244             this.qname = qname;
245             this.path = path;
246         }
247
248         @Override
249         public QName getQName() {
250             return qname;
251         }
252
253         @Override
254         public SchemaPath getPath() {
255             return path;
256         }
257
258         @Override
259         public List<QName> getKeyDefinition() {
260             return keyDefinition;
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 constraints;
281         }
282
283         @Override
284         public Set<AugmentationSchema> getAvailableAugmentations() {
285             return augmentations;
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             final ListSchemaNodeImpl other = (ListSchemaNodeImpl) 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             return "list " + qname.getLocalName();
339         }
340     }
341
342 }