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