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