Refactored YangModelParserImpl
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / 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.model.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.opendaylight.controller.yang.common.QName;
14 import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
15 import org.opendaylight.controller.yang.model.api.SchemaPath;
16 import org.opendaylight.controller.yang.model.api.Status;
17 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder;
19
20 public class IdentitySchemaNodeBuilder implements SchemaNodeBuilder {
21
22     private final QName qname;
23     private final IdentitySchemaNodeImpl instance;
24     private IdentitySchemaNodeBuilder baseIdentity;
25     private String baseIdentityName;
26
27     IdentitySchemaNodeBuilder(QName qname) {
28         this.qname = qname;
29         instance = new IdentitySchemaNodeImpl(qname);
30     }
31
32     @Override
33     public IdentitySchemaNode build() {
34         if(baseIdentity != null) {
35             IdentitySchemaNode base = baseIdentity.build();
36             instance.setBaseIdentity(base);
37         }
38         return instance;
39     }
40
41     @Override
42     public QName getQName() {
43         return qname;
44     }
45
46     @Override
47     public void setPath(SchemaPath path) {
48         instance.setPath(path);
49     }
50
51     @Override
52     public void setDescription(String description) {
53         instance.setDescription(description);
54     }
55
56     @Override
57     public void setReference(String reference) {
58         instance.setReference(reference);
59     }
60
61     @Override
62     public void setStatus(Status status) {
63         if (status != null) {
64             instance.setStatus(status);
65         }
66     }
67
68     @Override
69     public void addUnknownSchemaNode(
70             UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) {
71         throw new IllegalStateException(
72                 "Can not add schema node to identity statement");
73     }
74
75     public String getBaseIdentityName() {
76         return baseIdentityName;
77     }
78
79     public void setBaseIdentityName(String baseIdentityName) {
80         this.baseIdentityName = baseIdentityName;
81     }
82
83     public void setBaseIdentity(IdentitySchemaNodeBuilder baseType) {
84         this.baseIdentity = baseType;
85     }
86
87     private class IdentitySchemaNodeImpl implements IdentitySchemaNode {
88         private final QName qname;
89         private IdentitySchemaNode baseIdentity;
90         private String description;
91         private String reference;
92         private Status status = Status.CURRENT;
93         private SchemaPath path;
94
95         private IdentitySchemaNodeImpl(QName qname) {
96             this.qname = qname;
97         }
98
99         @Override
100         public QName getQName() {
101             return qname;
102         }
103
104         @Override
105         public IdentitySchemaNode getBaseIdentity() {
106             return baseIdentity;
107         }
108
109         private void setBaseIdentity(IdentitySchemaNode baseIdentity) {
110             this.baseIdentity = baseIdentity;
111         }
112
113         @Override
114         public String getDescription() {
115             return description;
116         }
117
118         private void setDescription(String description) {
119             this.description = description;
120         }
121
122         @Override
123         public String getReference() {
124             return reference;
125         }
126
127         private void setReference(String reference) {
128             this.reference = reference;
129         }
130
131         @Override
132         public Status getStatus() {
133             return status;
134         }
135
136         private void setStatus(Status status) {
137             if (status != null) {
138                 this.status = status;
139             }
140         }
141
142         @Override
143         public SchemaPath getPath() {
144             return path;
145         }
146
147         private void setPath(SchemaPath path) {
148             this.path = path;
149         }
150
151         @Override
152         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
153             return Collections.emptyList();
154         }
155
156         @Override
157         public int hashCode() {
158             final int prime = 31;
159             int result = 1;
160             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
161             return result;
162         }
163
164         @Override
165         public boolean equals(Object obj) {
166             if (this == obj) {
167                 return true;
168             }
169             if (obj == null) {
170                 return false;
171             }
172             if (getClass() != obj.getClass()) {
173                 return false;
174             }
175             IdentitySchemaNodeImpl other = (IdentitySchemaNodeImpl) obj;
176             if (qname == null) {
177                 if (other.qname != null) {
178                     return false;
179                 }
180             } else if (!qname.equals(other.qname)) {
181                 return false;
182             }
183             return true;
184         }
185
186         @Override
187         public String toString() {
188             StringBuilder sb = new StringBuilder(
189                     IdentitySchemaNodeImpl.class.getSimpleName());
190             sb.append("[");
191             sb.append("base=" + baseIdentity);
192             sb.append(", qname=" + qname);
193             sb.append(", description=" + description);
194             sb.append(", reference=" + reference);
195             sb.append(", status=" + status);
196             sb.append("]");
197             return sb.toString();
198         }
199     }
200
201 }