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