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