BUG-1344: Implemented getOriginal() for DataSchemaNode.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / LeafListSchemaNodeBuilder.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.List;
11
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
14 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractTypeAwareBuilder;
25
26 import com.google.common.base.Optional;
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.ImmutableList;
29
30 public final class LeafListSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
31     private LeafListSchemaNodeImpl instance;
32     private boolean userOrdered;
33     // SchemaNode args
34     private SchemaPath schemaPath;
35     private String description;
36     private String reference;
37     private Status status = Status.CURRENT;
38     // DataSchemaNode args
39     private boolean augmenting;
40     private boolean addedByUses;
41     private LeafListSchemaNode originalNode;
42     private LeafListSchemaNodeBuilder originalBuilder;
43     private boolean configuration;
44     private final ConstraintsBuilder constraints;
45
46     public LeafListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
47         super(moduleName, line, qname);
48         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
49         constraints = new ConstraintsBuilderImpl(moduleName, line);
50     }
51
52     public LeafListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
53             final LeafListSchemaNode base) {
54         super(moduleName, line, qname);
55         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
56         constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
57
58         description = base.getDescription();
59         reference = base.getReference();
60         status = base.getStatus();
61         augmenting = base.isAugmenting();
62         addedByUses = base.isAddedByUses();
63         originalNode = base;
64         configuration = base.isConfiguration();
65         this.type = base.getType();
66         userOrdered = base.isUserOrdered();
67         unknownNodes.addAll(base.getUnknownSchemaNodes());
68     }
69
70     @Override
71     public LeafListSchemaNode build() {
72         if (instance != null) {
73             return instance;
74         }
75
76         instance = new LeafListSchemaNodeImpl(qname, schemaPath);
77
78         instance.description = description;
79         instance.reference = reference;
80         instance.status = status;
81         instance.augmenting = augmenting;
82         instance.addedByUses = addedByUses;
83         instance.configuration = configuration;
84         instance.constraintsDef = constraints.toInstance();
85         instance.userOrdered = userOrdered;
86
87         if (type == null) {
88             instance.type = typedef.build();
89         } else {
90             instance.type = type;
91         }
92
93         // ORIGINAL NODE
94         if (originalNode == null && originalBuilder != null) {
95             originalNode = originalBuilder.build();
96         }
97         instance.original = originalNode;
98
99         // UNKNOWN NODES
100         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
101             unknownNodes.add(b.build());
102         }
103         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
104
105         return instance;
106     }
107
108     @Override
109     public SchemaPath getPath() {
110         return schemaPath;
111     }
112
113     @Override
114     public void setPath(final SchemaPath path) {
115         this.schemaPath = path;
116     }
117
118     @Override
119     public String getDescription() {
120         return description;
121     }
122
123     @Override
124     public void setDescription(final String description) {
125         this.description = description;
126     }
127
128     @Override
129     public String getReference() {
130         return reference;
131     }
132
133     @Override
134     public void setReference(final String reference) {
135         this.reference = reference;
136     }
137
138     @Override
139     public Status getStatus() {
140         return status;
141     }
142
143     @Override
144     public void setStatus(final Status status) {
145         this.status = Preconditions.checkNotNull(status, "status cannot be null");
146     }
147
148     @Override
149     public boolean isAugmenting() {
150         return augmenting;
151     }
152
153     @Override
154     public void setAugmenting(final boolean augmenting) {
155         this.augmenting = augmenting;
156     }
157
158     @Override
159     public boolean isAddedByUses() {
160         return addedByUses;
161     }
162
163     @Override
164     public void setAddedByUses(final boolean addedByUses) {
165         this.addedByUses = addedByUses;
166     }
167
168     @Override
169     public LeafListSchemaNodeBuilder getOriginal() {
170         return originalBuilder;
171     }
172
173     @Override
174     public void setOriginal(final SchemaNodeBuilder builder) {
175         Preconditions.checkArgument(builder instanceof LeafListSchemaNodeBuilder, "Original of leaf-list cannot be "
176                 + builder);
177         this.originalBuilder = (LeafListSchemaNodeBuilder) builder;
178     }
179
180     @Override
181     public boolean isConfiguration() {
182         return configuration;
183     }
184
185     @Override
186     public void setConfiguration(final boolean configuration) {
187         this.configuration = configuration;
188     }
189
190     @Override
191     public ConstraintsBuilder getConstraints() {
192         return constraints;
193     }
194
195     public boolean isUserOrdered() {
196         return userOrdered;
197     }
198
199     public void setUserOrdered(final boolean userOrdered) {
200         this.userOrdered = userOrdered;
201     }
202
203     @Override
204     public int hashCode() {
205         final int prime = 31;
206         int result = 1;
207         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
208         return result;
209     }
210
211     @Override
212     public boolean equals(final Object obj) {
213         if (this == obj) {
214             return true;
215         }
216         if (obj == null) {
217             return false;
218         }
219         if (getClass() != obj.getClass()) {
220             return false;
221         }
222         LeafListSchemaNodeBuilder other = (LeafListSchemaNodeBuilder) obj;
223         if (schemaPath == null) {
224             if (other.schemaPath != null) {
225                 return false;
226             }
227         } else if (!schemaPath.equals(other.schemaPath)) {
228             return false;
229         }
230         if (getParent() == null) {
231             if (other.getParent() != null) {
232                 return false;
233             }
234         } else if (!getParent().equals(other.getParent())) {
235             return false;
236         }
237         return true;
238     }
239
240     @Override
241     public String toString() {
242         return "leaf-list " + qname.getLocalName();
243     }
244
245     private static final class LeafListSchemaNodeImpl implements LeafListSchemaNode, DerivableSchemaNode {
246         private final QName qname;
247         private final SchemaPath path;
248         private String description;
249         private String reference;
250         private Status status;
251         private boolean augmenting;
252         private boolean addedByUses;
253         private LeafListSchemaNode original;
254         private boolean configuration;
255         private ConstraintDefinition constraintsDef;
256         private TypeDefinition<?> type;
257         private boolean userOrdered;
258         private ImmutableList<UnknownSchemaNode> unknownNodes;
259
260         private LeafListSchemaNodeImpl(final QName qname, final SchemaPath path) {
261             this.qname = qname;
262             this.path = path;
263         }
264
265         @Override
266         public QName getQName() {
267             return qname;
268         }
269
270         @Override
271         public SchemaPath getPath() {
272             return path;
273         }
274
275         @Override
276         public String getDescription() {
277             return description;
278         }
279
280         @Override
281         public String getReference() {
282             return reference;
283         }
284
285         @Override
286         public Status getStatus() {
287             return status;
288         }
289
290         @Override
291         public boolean isAugmenting() {
292             return augmenting;
293         }
294
295         @Override
296         public boolean isAddedByUses() {
297             return addedByUses;
298         }
299
300         @Override
301         public Optional<LeafListSchemaNode> getOriginal() {
302             return Optional.fromNullable(original);
303         }
304
305         @Override
306         public boolean isConfiguration() {
307             return configuration;
308         }
309
310         @Override
311         public ConstraintDefinition getConstraints() {
312             return constraintsDef;
313         }
314
315         @Override
316         public TypeDefinition<?> getType() {
317             return type;
318         }
319
320         @Override
321         public boolean isUserOrdered() {
322             return userOrdered;
323         }
324
325         @Override
326         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
327             return unknownNodes;
328         }
329
330         @Override
331         public int hashCode() {
332             final int prime = 31;
333             int result = 1;
334             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
335             result = prime * result + ((path == null) ? 0 : path.hashCode());
336             return result;
337         }
338
339         @Override
340         public boolean equals(final Object obj) {
341             if (this == obj) {
342                 return true;
343             }
344             if (obj == null) {
345                 return false;
346             }
347             if (getClass() != obj.getClass()) {
348                 return false;
349             }
350             LeafListSchemaNodeImpl other = (LeafListSchemaNodeImpl) obj;
351             if (qname == null) {
352                 if (other.qname != null) {
353                     return false;
354                 }
355             } else if (!qname.equals(other.qname)) {
356                 return false;
357             }
358             if (path == null) {
359                 if (other.path != null) {
360                     return false;
361                 }
362             } else if (!path.equals(other.path)) {
363                 return false;
364             }
365             return true;
366         }
367
368         @Override
369         public String toString() {
370             StringBuilder sb = new StringBuilder(LeafListSchemaNodeImpl.class.getSimpleName());
371             sb.append("[");
372             sb.append(qname);
373             sb.append("]");
374             return sb.toString();
375         }
376     }
377
378 }