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