Refactored SchemaPath for yang java types. Fixed SchemaPath for augmented nodes types.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / FeatureBuilder.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.FeatureDefinition;
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 FeatureBuilder implements SchemaNodeBuilder {
22     private boolean isBuilt;
23     private final FeatureDefinitionImpl instance;
24     private final int line;
25     private final QName qname;
26     private SchemaPath schemaPath;
27     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
28
29     FeatureBuilder(final QName qname, final int line) {
30         this.qname = qname;
31         this.line = line;
32         instance = new FeatureDefinitionImpl(qname);
33     }
34
35     @Override
36     public FeatureDefinitionImpl build() {
37         if(!isBuilt) {
38             instance.setPath(schemaPath);
39
40             // UNKNOWN NODES
41             final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
42             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
43                 unknownNodes.add(b.build());
44             }
45             instance.setUnknownSchemaNodes(unknownNodes);
46
47             isBuilt = true;
48         }
49         return instance;
50     }
51
52     @Override
53     public int getLine() {
54         return line;
55     }
56
57     @Override
58     public QName getQName() {
59         return qname;
60     }
61
62     @Override
63     public SchemaPath getPath() {
64         return schemaPath;
65     }
66
67     @Override
68     public void setPath(SchemaPath schemaPath) {
69         this.schemaPath = schemaPath;
70     }
71
72     @Override
73     public void setDescription(final String description) {
74         instance.setDescription(description);
75     }
76
77     @Override
78     public void setReference(final String reference) {
79         instance.setReference(reference);
80     }
81
82     @Override
83     public void setStatus(final Status status) {
84         instance.setStatus(status);
85     }
86
87     @Override
88     public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
89         addedUnknownNodes.add(unknownNode);
90     }
91
92     private final class FeatureDefinitionImpl implements FeatureDefinition {
93         private final QName qname;
94         private SchemaPath path;
95         private String description;
96         private String reference;
97         private Status status = Status.CURRENT;
98         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
99
100         private FeatureDefinitionImpl(final QName qname) {
101             this.qname = qname;
102         }
103
104         @Override
105         public QName getQName() {
106             return qname;
107         }
108
109         @Override
110         public SchemaPath getPath() {
111             return path;
112         }
113
114         private void setPath(final SchemaPath path) {
115             this.path = path;
116         }
117
118         @Override
119         public String getDescription() {
120             return description;
121         }
122
123         private void setDescription(final String description) {
124             this.description = description;
125         }
126
127         @Override
128         public String getReference() {
129             return reference;
130         }
131
132         private void setReference(final String reference) {
133             this.reference = reference;
134         }
135
136         @Override
137         public Status getStatus() {
138             return status;
139         }
140
141         private void setStatus(Status status) {
142             if (status != null) {
143                 this.status = status;
144             }
145         }
146
147         @Override
148         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
149             return unknownNodes;
150         }
151
152         private void setUnknownSchemaNodes(
153                 final List<UnknownSchemaNode> unknownNodes) {
154             if (unknownNodes != null) {
155                 this.unknownNodes = unknownNodes;
156             }
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 + ((path == null) ? 0 : path.hashCode());
165             return result;
166         }
167
168         @Override
169         public boolean equals(Object obj) {
170             if (this == obj) {
171                 return true;
172             }
173             if (obj == null) {
174                 return false;
175             }
176             if (getClass() != obj.getClass()) {
177                 return false;
178             }
179             FeatureDefinitionImpl other = (FeatureDefinitionImpl) obj;
180             if (qname == null) {
181                 if (other.qname != null) {
182                     return false;
183                 }
184             } else if (!qname.equals(other.qname)) {
185                 return false;
186             }
187             if (path == null) {
188                 if (other.path != null) {
189                     return false;
190                 }
191             } else if (!path.equals(other.path)) {
192                 return false;
193             }
194             return true;
195         }
196
197         @Override
198         public String toString() {
199             StringBuilder sb = new StringBuilder(
200                     FeatureDefinitionImpl.class.getSimpleName());
201             sb.append("[name=" + qname + "]");
202             return sb.toString();
203         }
204     }
205
206 }