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