Merge "BUG-868: do not use InstanceIdentifier constructor"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / LeafSchemaNodeBuilder.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 java.util.List;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
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.UnknownSchemaNodeBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractTypeAwareBuilder;
24 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
25
26 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
27     private LeafSchemaNodeImpl instance;
28     private String defaultStr;
29     private String unitsStr;
30     // SchemaNode args
31     private SchemaPath schemaPath;
32     private String description;
33     private String reference;
34     private Status status = Status.CURRENT;
35     // DataSchemaNode args
36     private boolean augmenting;
37     private boolean addedByUses;
38     private boolean configuration;
39     private final ConstraintsBuilder constraints;
40
41     public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
42         super(moduleName, line, qname);
43         this.schemaPath = Preconditions.checkNotNull(schemaPath, "Schema Path must not be null");
44         constraints = new ConstraintsBuilderImpl(moduleName, line);
45     }
46
47     public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path, final LeafSchemaNode base) {
48         super(moduleName, line, qname);
49         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
50         constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
51
52         description = base.getDescription();
53         reference = base.getReference();
54         status = base.getStatus();
55         augmenting = base.isAugmenting();
56         addedByUses = base.isAddedByUses();
57         configuration = base.isConfiguration();
58         this.type = base.getType();
59         unknownNodes.addAll(base.getUnknownSchemaNodes());
60
61         defaultStr = base.getDefault();
62         unitsStr = base.getUnits();
63     }
64
65     @Override
66     public LeafSchemaNode build() {
67         if (instance != null) {
68             return instance;
69         }
70
71         instance = new LeafSchemaNodeImpl(qname, schemaPath);
72
73         instance.description = description;
74         instance.reference = reference;
75         instance.status = status;
76         instance.augmenting = augmenting;
77         instance.addedByUses = addedByUses;
78         instance.configuration = configuration;
79         instance.constraintsDef = constraints.toInstance();
80         instance.defaultStr = defaultStr;
81         instance.unitsStr = unitsStr;
82
83         if (type == null && typedef == null) {
84             throw new YangParseException(getModuleName(), getLine(), "Failed to resolve leaf type.");
85         }
86
87         // TYPE
88         if (type == null) {
89             instance.type = typedef.build();
90         } else {
91             instance.type = type;
92         }
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     @Override
104     public SchemaPath getPath() {
105         return schemaPath;
106     }
107
108     @Override
109     public void setPath(final SchemaPath path) {
110         this.schemaPath = path;
111     }
112
113     @Override
114     public ConstraintsBuilder getConstraints() {
115         return constraints;
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 boolean isConfiguration() {
170         return configuration;
171     }
172
173     @Override
174     public void setConfiguration(final boolean configuration) {
175         this.configuration = configuration;
176     }
177
178     public String getDefaultStr() {
179         return defaultStr;
180     }
181
182     public void setDefaultStr(final String defaultStr) {
183         this.defaultStr = defaultStr;
184     }
185
186     public String getUnits() {
187         return unitsStr;
188     }
189
190     public void setUnits(final String unitsStr) {
191         this.unitsStr = unitsStr;
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         LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) 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 "leaf " + qname.getLocalName();
234     }
235
236     private static final class LeafSchemaNodeImpl implements LeafSchemaNode {
237         private final QName qname;
238         private final SchemaPath path;
239         private String description;
240         private String reference;
241         private Status status;
242         private boolean augmenting;
243         private boolean addedByUses;
244         private boolean configuration;
245         private ConstraintDefinition constraintsDef;
246         private TypeDefinition<?> type;
247         private ImmutableList<UnknownSchemaNode> unknownNodes;
248         private String defaultStr;
249         private String unitsStr;
250
251         private LeafSchemaNodeImpl(final QName qname, final SchemaPath path) {
252             this.qname = qname;
253             this.path = path;
254         }
255
256         @Override
257         public QName getQName() {
258             return qname;
259         }
260
261         @Override
262         public SchemaPath getPath() {
263             return path;
264         }
265
266         @Override
267         public String getDescription() {
268             return description;
269         }
270
271         @Override
272         public String getReference() {
273             return reference;
274         }
275
276         @Override
277         public Status getStatus() {
278             return status;
279         }
280
281         @Override
282         public boolean isAugmenting() {
283             return augmenting;
284         }
285
286         @Override
287         public boolean isAddedByUses() {
288             return addedByUses;
289         }
290
291         @Override
292         public boolean isConfiguration() {
293             return configuration;
294         }
295
296         @Override
297         public ConstraintDefinition getConstraints() {
298             return constraintsDef;
299         }
300
301         @Override
302         public TypeDefinition<?> getType() {
303             return type;
304         }
305
306         @Override
307         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
308             return unknownNodes;
309         }
310
311         @Override
312         public String getDefault() {
313             return defaultStr;
314         }
315
316         @Override
317         public String getUnits() {
318             return unitsStr;
319         }
320
321         @Override
322         public int hashCode() {
323             final int prime = 31;
324             int result = 1;
325             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
326             result = prime * result + ((path == null) ? 0 : path.hashCode());
327             return result;
328         }
329
330         @Override
331         public boolean equals(final Object obj) {
332             if (this == obj) {
333                 return true;
334             }
335             if (obj == null) {
336                 return false;
337             }
338             if (getClass() != obj.getClass()) {
339                 return false;
340             }
341             LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
342             if (qname == null) {
343                 if (other.qname != null) {
344                     return false;
345                 }
346             } else if (!qname.equals(other.qname)) {
347                 return false;
348             }
349             if (path == null) {
350                 if (other.path != null) {
351                     return false;
352                 }
353             } else if (!path.equals(other.path)) {
354                 return false;
355             }
356             return true;
357         }
358
359         @Override
360         public String toString() {
361             StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
362             sb.append("[");
363             sb.append("qname=").append(qname);
364             sb.append(", path=").append(path);
365             sb.append("]");
366             return sb.toString();
367         }
368     }
369
370 }