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