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