Merge "API Cleanup: Moved package name computation to BindingMapping."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ContainerSchemaNodeBuilder.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.List;
12 import java.util.Set;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
16 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
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.Optional;
31 import com.google.common.base.Preconditions;
32 import com.google.common.collect.ImmutableList;
33 import com.google.common.collect.ImmutableSet;
34
35 public final class ContainerSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
36         AugmentationTargetBuilder, DataSchemaNodeBuilder {
37     private ContainerSchemaNodeImpl instance;
38     private boolean presence;
39     // SchemaNode args
40     private SchemaPath path;
41     // DataSchemaNode args
42     private boolean augmenting;
43     private boolean addedByUses;
44     private boolean configuration;
45     private ContainerSchemaNode originalNode;
46     private ContainerSchemaNodeBuilder originalBuilder;
47     private final ConstraintsBuilder constraints;
48     // AugmentationTarget args
49     private final List<AugmentationSchema> augmentations = new ArrayList<>();
50     private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
51
52     public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
53         super(moduleName, line, qname);
54         this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
55         this.constraints = new ConstraintsBuilderImpl(moduleName, line);
56     }
57
58     // constructor for uses
59     public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname,
60             final SchemaPath path, final ContainerSchemaNode base) {
61         super(moduleName, line, qname, path, base);
62         this.path = Preconditions.checkNotNull(path, "Schema Path must not be null");
63
64         constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
65
66         augmenting = base.isAugmenting();
67         addedByUses = base.isAddedByUses();
68         originalNode = base;
69         configuration = base.isConfiguration();
70         presence = base.isPresenceContainer();
71
72         augmentations.addAll(base.getAvailableAugmentations());
73
74     }
75
76     @Override
77     protected String getStatementName() {
78         return "container";
79     }
80
81     @Override
82     public ContainerSchemaNode build() {
83         if (instance != null) {
84             return instance;
85         }
86
87         buildChildren();
88         instance = new ContainerSchemaNodeImpl(this);
89
90         instance.augmenting = augmenting;
91         instance.addedByUses = addedByUses;
92         instance.configuration = configuration;
93         instance.constraints = constraints.toInstance();
94         instance.presence = presence;
95
96         // ORIGINAL NODE
97         if (originalNode == null && originalBuilder != null) {
98             originalNode = originalBuilder.build();
99         }
100         instance.original = originalNode;
101
102         // AUGMENTATIONS
103         for (AugmentationSchemaBuilder builder : augmentationBuilders) {
104             augmentations.add(builder.build());
105         }
106         instance.augmentations = ImmutableSet.copyOf(augmentations);
107
108         // UNKNOWN NODES
109         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
110             unknownNodes.add(b.build());
111         }
112         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
113
114         return instance;
115     }
116
117     public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
118         return augmentationBuilders;
119     }
120
121     @Override
122     public void addAugmentation(final AugmentationSchemaBuilder augment) {
123         augmentationBuilders.add(augment);
124     }
125
126     @Override
127     public SchemaPath getPath() {
128         return path;
129     }
130
131     @Override
132     public void setPath(final SchemaPath path) {
133         this.path = path;
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 ContainerSchemaNodeBuilder getOriginal() {
158         return originalBuilder;
159     }
160
161     @Override
162     public void setOriginal(final SchemaNodeBuilder builder) {
163         Preconditions.checkArgument(builder instanceof ContainerSchemaNodeBuilder, "Original of container cannot be "
164                 + builder);
165         this.originalBuilder = (ContainerSchemaNodeBuilder) builder;
166     }
167
168     @Override
169     public boolean isConfiguration() {
170         return configuration;
171     }
172
173     @Override
174     public void setConfiguration(final boolean configuration) {
175         this.configuration = configuration;
176     }
177
178     @Override
179     public ConstraintsBuilder getConstraints() {
180         return constraints;
181     }
182
183     public boolean isPresence() {
184         return presence;
185     }
186
187     public void setPresence(final boolean presence) {
188         this.presence = presence;
189     }
190
191     @Override
192     public int hashCode() {
193         final int prime = 31;
194         int result = 1;
195         result = prime * result + ((path == null) ? 0 : path.hashCode());
196         return result;
197     }
198
199     @Override
200     public boolean equals(final Object obj) {
201         if (this == obj) {
202             return true;
203         }
204         if (obj == null) {
205             return false;
206         }
207         if (getClass() != obj.getClass()) {
208             return false;
209         }
210         ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
211         if (path == null) {
212             if (other.path != null) {
213                 return false;
214             }
215         } else if (!path.equals(other.path)) {
216             return false;
217         }
218         if (getParent() == null) {
219             if (other.getParent() != null) {
220                 return false;
221             }
222         } else if (!getParent().equals(other.getParent())) {
223             return false;
224         }
225         return true;
226     }
227
228     @Override
229     public String toString() {
230         return "container " + qname.getLocalName();
231     }
232
233     private static final class ContainerSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements
234             ContainerSchemaNode, DerivableSchemaNode {
235         private final QName qname;
236         private final SchemaPath path;
237
238         private boolean augmenting;
239         private boolean addedByUses;
240         private boolean configuration;
241         private ContainerSchemaNode original;
242         private ConstraintDefinition constraints;
243
244         private ImmutableSet<AugmentationSchema> augmentations;
245         private ImmutableList<UnknownSchemaNode> unknownNodes;
246
247         private boolean presence;
248
249         public ContainerSchemaNodeImpl(final ContainerSchemaNodeBuilder builder) {
250             super(builder);
251             this.qname = builder.getQName();
252             this.path = builder.getPath();
253         }
254
255         @Override
256         public QName getQName() {
257             return qname;
258         }
259
260         @Override
261         public SchemaPath getPath() {
262             return path;
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 Optional<ContainerSchemaNode> getOriginal() {
277             return Optional.fromNullable(original);
278         }
279
280         @Override
281         public boolean isConfiguration() {
282             return configuration;
283         }
284
285         @Override
286         public ConstraintDefinition getConstraints() {
287             return constraints;
288         }
289
290         @Override
291         public Set<AugmentationSchema> getAvailableAugmentations() {
292             return augmentations;
293         }
294
295         @Override
296         public boolean isPresenceContainer() {
297             return presence;
298         }
299
300         @Override
301         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
302             return unknownNodes;
303         }
304
305         @Override
306         public int hashCode() {
307             final int prime = 31;
308             int result = 1;
309             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
310             result = prime * result + ((path == null) ? 0 : path.hashCode());
311             return result;
312         }
313
314         @Override
315         public boolean equals(final Object obj) {
316             if (this == obj) {
317                 return true;
318             }
319             if (obj == null) {
320                 return false;
321             }
322             if (getClass() != obj.getClass()) {
323                 return false;
324             }
325             ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
326             if (qname == null) {
327                 if (other.qname != null) {
328                     return false;
329                 }
330             } else if (!qname.equals(other.qname)) {
331                 return false;
332             }
333             if (path == null) {
334                 if (other.path != null) {
335                     return false;
336                 }
337             } else if (!path.equals(other.path)) {
338                 return false;
339             }
340             return true;
341         }
342
343         @Override
344         public String toString() {
345             return "container " + qname.getLocalName();
346         }
347
348     }
349
350 }