Merge "Package names for enclosed Types of TOs were changed to fully qualified. Fully...
[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 String moduleName, final int line, final QName qname) {
27         super(moduleName, 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             if (unknownNodes == null) {
41                 unknownNodes = new ArrayList<UnknownSchemaNode>();
42                 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
43                     unknownNodes.add(b.build());
44                 }
45                 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
46             }
47             instance.setUnknownSchemaNodes(unknownNodes);
48
49             isBuilt = true;
50         }
51         return instance;
52     }
53
54     @Override
55     public String toString() {
56         return "feature " + qname.getLocalName();
57     }
58
59     private final class FeatureDefinitionImpl implements FeatureDefinition {
60         private final QName qname;
61         private SchemaPath path;
62         private String description;
63         private String reference;
64         private Status status = Status.CURRENT;
65         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
66
67         private FeatureDefinitionImpl(final QName qname) {
68             this.qname = qname;
69         }
70
71         @Override
72         public QName getQName() {
73             return qname;
74         }
75
76         @Override
77         public SchemaPath getPath() {
78             return path;
79         }
80
81         private void setPath(final SchemaPath path) {
82             this.path = path;
83         }
84
85         @Override
86         public String getDescription() {
87             return description;
88         }
89
90         private void setDescription(final String description) {
91             this.description = description;
92         }
93
94         @Override
95         public String getReference() {
96             return reference;
97         }
98
99         private void setReference(final String reference) {
100             this.reference = reference;
101         }
102
103         @Override
104         public Status getStatus() {
105             return status;
106         }
107
108         private void setStatus(Status status) {
109             if (status != null) {
110                 this.status = status;
111             }
112         }
113
114         @Override
115         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
116             return unknownNodes;
117         }
118
119         private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
120             if (unknownNodes != null) {
121                 this.unknownNodes = unknownNodes;
122             }
123         }
124
125         @Override
126         public int hashCode() {
127             final int prime = 31;
128             int result = 1;
129             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
130             result = prime * result + ((path == null) ? 0 : path.hashCode());
131             return result;
132         }
133
134         @Override
135         public boolean equals(Object obj) {
136             if (this == obj) {
137                 return true;
138             }
139             if (obj == null) {
140                 return false;
141             }
142             if (getClass() != obj.getClass()) {
143                 return false;
144             }
145             FeatureDefinitionImpl other = (FeatureDefinitionImpl) obj;
146             if (qname == null) {
147                 if (other.qname != null) {
148                     return false;
149                 }
150             } else if (!qname.equals(other.qname)) {
151                 return false;
152             }
153             if (path == null) {
154                 if (other.path != null) {
155                     return false;
156                 }
157             } else if (!path.equals(other.path)) {
158                 return false;
159             }
160             return true;
161         }
162
163         @Override
164         public String toString() {
165             StringBuilder sb = new StringBuilder(FeatureDefinitionImpl.class.getSimpleName());
166             sb.append("[name=" + qname + "]");
167             return sb.toString();
168         }
169     }
170
171 }