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