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