Fix for bug 24.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / IdentitySchemaNodeBuilder.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.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.IdentitySchemaNode;
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.parser.builder.api.SchemaNodeBuilder;
20
21 public final class IdentitySchemaNodeBuilder implements SchemaNodeBuilder {
22     private final IdentitySchemaNodeImpl instance;
23     private final int line;
24     private final QName qname;
25     private SchemaPath schemaPath;
26     private IdentitySchemaNodeBuilder baseIdentity;
27     private String baseIdentityName;
28     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
29
30     IdentitySchemaNodeBuilder(final QName qname, final int line) {
31         this.qname = qname;
32         this.line = line;
33         instance = new IdentitySchemaNodeImpl(qname);
34     }
35
36     @Override
37     public IdentitySchemaNode build() {
38         instance.setPath(schemaPath);
39         if (baseIdentity != null) {
40             instance.setBaseIdentity(baseIdentity.build());
41         }
42
43         // UNKNOWN NODES
44         final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
45         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
46             unknownNodes.add(b.build());
47         }
48         instance.setUnknownSchemaNodes(unknownNodes);
49
50         return instance;
51     }
52
53     @Override
54     public int getLine() {
55         return line;
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 void setDescription(final String description) {
75         instance.setDescription(description);
76     }
77
78     @Override
79     public void setReference(final String reference) {
80         instance.setReference(reference);
81     }
82
83     @Override
84     public void setStatus(final Status status) {
85         if (status != null) {
86             instance.setStatus(status);
87         }
88     }
89
90     @Override
91     public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
92         addedUnknownNodes.add(unknownNode);
93     }
94
95     public String getBaseIdentityName() {
96         return baseIdentityName;
97     }
98
99     public void setBaseIdentityName(final String baseIdentityName) {
100         this.baseIdentityName = baseIdentityName;
101     }
102
103     public void setBaseIdentity(final IdentitySchemaNodeBuilder baseType) {
104         this.baseIdentity = baseType;
105     }
106
107     private final class IdentitySchemaNodeImpl implements IdentitySchemaNode {
108         private final QName qname;
109         private IdentitySchemaNode baseIdentity;
110         private String description;
111         private String reference;
112         private Status status = Status.CURRENT;
113         private SchemaPath path;
114         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
115
116         private IdentitySchemaNodeImpl(final QName qname) {
117             this.qname = qname;
118         }
119
120         @Override
121         public QName getQName() {
122             return qname;
123         }
124
125         @Override
126         public IdentitySchemaNode getBaseIdentity() {
127             return baseIdentity;
128         }
129
130         private void setBaseIdentity(final IdentitySchemaNode baseIdentity) {
131             this.baseIdentity = baseIdentity;
132         }
133
134         @Override
135         public String getDescription() {
136             return description;
137         }
138
139         private void setDescription(final String description) {
140             this.description = description;
141         }
142
143         @Override
144         public String getReference() {
145             return reference;
146         }
147
148         private void setReference(final String reference) {
149             this.reference = reference;
150         }
151
152         @Override
153         public Status getStatus() {
154             return status;
155         }
156
157         private void setStatus(final Status status) {
158             if (status != null) {
159                 this.status = status;
160             }
161         }
162
163         @Override
164         public SchemaPath getPath() {
165             return path;
166         }
167
168         private void setPath(final SchemaPath path) {
169             this.path = path;
170         }
171
172         @Override
173         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
174             return unknownNodes;
175         }
176
177         private void setUnknownSchemaNodes(
178                 List<UnknownSchemaNode> unknownSchemaNodes) {
179             if (unknownSchemaNodes != null) {
180                 this.unknownNodes = unknownSchemaNodes;
181             }
182         }
183
184         @Override
185         public int hashCode() {
186             final int prime = 31;
187             int result = 1;
188             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
189             result = prime * result + ((path == null) ? 0 : path.hashCode());
190             return result;
191         }
192
193         @Override
194         public boolean equals(Object obj) {
195             if (this == obj) {
196                 return true;
197             }
198             if (obj == null) {
199                 return false;
200             }
201             if (getClass() != obj.getClass()) {
202                 return false;
203             }
204             IdentitySchemaNodeImpl other = (IdentitySchemaNodeImpl) obj;
205             if (qname == null) {
206                 if (other.qname != null) {
207                     return false;
208                 }
209             } else if (!qname.equals(other.qname)) {
210                 return false;
211             }
212             if (path == null) {
213                 if (other.path != null) {
214                     return false;
215                 }
216             } else if (!path.equals(other.path)) {
217                 return false;
218             }
219             return true;
220         }
221
222         @Override
223         public String toString() {
224             StringBuilder sb = new StringBuilder(
225                     IdentitySchemaNodeImpl.class.getSimpleName());
226             sb.append("[");
227             sb.append("base=" + baseIdentity);
228             sb.append(", qname=" + qname);
229             sb.append("]");
230             return sb.toString();
231         }
232     }
233
234 }