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