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