c8c38b47cf40758412e0849cf8f45941f36e7f3c
[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.Collections;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
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.UnknownSchemaNode;
18 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
19 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
20 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
22
23 public final class UnknownSchemaNodeBuilderImpl extends AbstractBuilder implements UnknownSchemaNodeBuilder {
24     private QName qname;
25     private SchemaPath schemaPath;
26     private String description;
27     private String reference;
28     private Status status = Status.CURRENT;
29     private boolean addedByUses;
30
31     private UnknownSchemaNodeImpl instance;
32     private QName nodeType;
33     private String nodeParameter;
34
35     private ExtensionDefinition extensionDefinition;
36     private ExtensionBuilder extensionBuilder;
37
38     public UnknownSchemaNodeBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
39         super(moduleName, line);
40         this.qname = qname;
41         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
42     }
43
44     public UnknownSchemaNodeBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path, final UnknownSchemaNode base) {
45         super(moduleName, line);
46         this.qname = base.getQName();
47         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
48
49         this.nodeType = base.getNodeType();
50         this.nodeParameter = base.getNodeParameter();
51         this.description = base.getDescription();
52         this.reference = base.getReference();
53         this.status = base.getStatus();
54         this.addedByUses = base.isAddedByUses();
55         this.extensionDefinition = base.getExtensionDefinition();
56         this.unknownNodes.addAll(base.getUnknownSchemaNodes());
57     }
58
59     @Override
60     public QName getQName() {
61         return qname;
62     }
63
64     @Override
65     public SchemaPath getPath() {
66         return schemaPath;
67     }
68
69     @Override
70     public void setPath(SchemaPath schemaPath) {
71         this.schemaPath = schemaPath;
72     }
73
74     @Override
75     public int hashCode() {
76         final int prime = 31;
77         int result = 1;
78         result = prime * result + Objects.hashCode(qname);
79         result = prime * result + Objects.hashCode(schemaPath);
80         result = prime * result + Objects.hashCode(nodeType);
81         result = prime * result + Objects.hashCode(nodeParameter);
82         return result;
83     }
84
85     @Override
86     public boolean equals(final Object obj) {
87         if (this == obj) {
88             return true;
89         }
90         if (obj == null) {
91             return false;
92         }
93         if (getClass() != obj.getClass()) {
94             return false;
95         }
96         UnknownSchemaNodeBuilderImpl other = (UnknownSchemaNodeBuilderImpl) obj;
97         if (qname == null) {
98             if (other.qname != null) {
99                 return false;
100             }
101         } else if (!qname.equals(other.qname)) {
102             return false;
103         }
104         if (schemaPath == null) {
105             if (other.schemaPath != null) {
106                 return false;
107             }
108         } else if (!schemaPath.equals(other.schemaPath)) {
109             return false;
110         }
111         if (nodeType == null) {
112             if (other.nodeType != null) {
113                 return false;
114             }
115         } else if (!nodeType.equals(other.nodeType)) {
116             return false;
117         }
118         if (nodeParameter == null) {
119             if (other.nodeParameter != null) {
120                 return false;
121             }
122         } else if (!nodeParameter.equals(other.nodeParameter)) {
123             return false;
124         }
125         return true;
126     }
127
128     @Override
129     public UnknownSchemaNode build() {
130         if (instance != null) {
131             return instance;
132         }
133
134         instance = new UnknownSchemaNodeImpl(qname, schemaPath);
135
136         instance.setNodeType(nodeType);
137         instance.setNodeParameter(nodeParameter);
138
139         instance.description = description;
140         instance.reference = reference;
141         instance.status = status;
142         instance.addedByUses = addedByUses;
143
144         // EXTENSION
145         if (extensionDefinition != null) {
146             instance.setExtensionDefinition(extensionDefinition);
147         } else {
148             if (extensionBuilder != null) {
149                 instance.setExtensionDefinition(extensionBuilder.build());
150             }
151         }
152
153         // UNKNOWN NODES
154         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
155             unknownNodes.add(b.build());
156         }
157         Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
158         instance.setUnknownSchemaNodes(unknownNodes);
159
160         return instance;
161     }
162
163     @Override
164     public String getDescription() {
165         return description;
166     }
167
168     @Override
169     public void setDescription(final String description) {
170         this.description = description;
171     }
172
173     @Override
174     public String getReference() {
175         return reference;
176     }
177
178     @Override
179     public void setReference(final String reference) {
180         this.reference = reference;
181     }
182
183     @Override
184     public Status getStatus() {
185         return status;
186     }
187
188     @Override
189     public void setStatus(final Status status) {
190         if (status != null) {
191             this.status = status;
192         }
193     }
194
195     @Override
196     public boolean isAddedByUses() {
197         return addedByUses;
198     }
199
200     @Override
201     public void setAddedByUses(final boolean addedByUses) {
202         this.addedByUses = addedByUses;
203     }
204
205     @Override
206     public QName getNodeType() {
207         return nodeType;
208     }
209
210     @Override
211     public void setNodeType(final QName nodeType) {
212         this.nodeType = nodeType;
213     }
214
215     @Override
216     public String getNodeParameter() {
217         return nodeParameter;
218     }
219
220     @Override
221     public void setNodeParameter(final String nodeParameter) {
222         this.nodeParameter = nodeParameter;
223     }
224
225     @Override
226     public ExtensionDefinition getExtensionDefinition() {
227         return extensionDefinition;
228     }
229
230     @Override
231     public void setExtensionDefinition(final ExtensionDefinition extensionDefinition) {
232         this.extensionDefinition = extensionDefinition;
233     }
234
235     @Override
236     public ExtensionBuilder getExtensionBuilder() {
237         return extensionBuilder;
238     }
239
240     @Override
241     public void setExtensionBuilder(final ExtensionBuilder extension) {
242         this.extensionBuilder = extension;
243     }
244
245     @Override
246     public String toString() {
247         StringBuilder sb = new StringBuilder();
248         sb.append(nodeType.getNamespace());
249         sb.append(":");
250         sb.append(nodeType.getLocalName());
251         sb.append(" ");
252         sb.append(nodeParameter);
253         return sb.toString();
254     }
255
256 }