Merge "Cleanup warning suppressions"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / LeafSchemaNodeBuilder.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 org.opendaylight.yangtools.yang.common.QName;
11 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
12 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
13 import org.opendaylight.yangtools.yang.model.api.Status;
14 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
15 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
16 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
17 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
18 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractTypeAwareBuilder;
19 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.ImmutableList;
22
23 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
24     private LeafSchemaNodeImpl instance;
25     private String defaultStr;
26     private String unitsStr;
27     // SchemaNode args
28     private SchemaPath schemaPath;
29     private String description;
30     private String reference;
31     private Status status = Status.CURRENT;
32     // DataSchemaNode args
33     private boolean augmenting;
34     private boolean addedByUses;
35     private LeafSchemaNode originalNode;
36     private LeafSchemaNodeBuilder originalBuilder;
37     private boolean configuration;
38     private final ConstraintsBuilder constraints;
39
40     public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
41         super(moduleName, line, qname);
42         this.schemaPath = Preconditions.checkNotNull(schemaPath, "Schema Path must not be null");
43         constraints = new ConstraintsBuilderImpl(moduleName, line);
44     }
45
46     public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path, final LeafSchemaNode base) {
47         super(moduleName, line, qname);
48         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
49         constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
50
51         description = base.getDescription();
52         reference = base.getReference();
53         status = base.getStatus();
54         augmenting = base.isAugmenting();
55         addedByUses = base.isAddedByUses();
56         originalNode =base;
57         configuration = base.isConfiguration();
58         this.type = base.getType();
59         unknownNodes.addAll(base.getUnknownSchemaNodes());
60
61         defaultStr = base.getDefault();
62         unitsStr = base.getUnits();
63     }
64
65     @Override
66     public LeafSchemaNode build() {
67         if (instance != null) {
68             return instance;
69         }
70
71         instance = new LeafSchemaNodeImpl(qname, schemaPath);
72
73         instance.description = description;
74         instance.reference = reference;
75         instance.status = status;
76         instance.augmenting = augmenting;
77         instance.addedByUses = addedByUses;
78         instance.configuration = configuration;
79         instance.constraintsDef = constraints.toInstance();
80         instance.defaultStr = defaultStr;
81         instance.unitsStr = unitsStr;
82
83         if (type == null && typedef == null) {
84             throw new YangParseException(getModuleName(), getLine(), "Failed to resolve leaf type.");
85         }
86
87         // TYPE
88         if (type == null) {
89             instance.type = typedef.build();
90         } else {
91             instance.type = type;
92         }
93
94         // ORIGINAL NODE
95         if (originalNode == null && originalBuilder != null) {
96             originalNode = originalBuilder.build();
97         }
98         instance.original = originalNode;
99
100         // UNKNOWN NODES
101         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
102             unknownNodes.add(b.build());
103         }
104         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
105
106         return instance;
107     }
108
109     @Override
110     public SchemaPath getPath() {
111         return schemaPath;
112     }
113
114     @Override
115     public void setPath(final SchemaPath path) {
116         this.schemaPath = path;
117     }
118
119     @Override
120     public ConstraintsBuilder getConstraints() {
121         return constraints;
122     }
123
124     @Override
125     public String getDescription() {
126         return description;
127     }
128
129     @Override
130     public void setDescription(final String description) {
131         this.description = description;
132     }
133
134     @Override
135     public String getReference() {
136         return reference;
137     }
138
139     @Override
140     public void setReference(final String reference) {
141         this.reference = reference;
142     }
143
144     @Override
145     public Status getStatus() {
146         return status;
147     }
148
149     @Override
150     public void setStatus(final Status status) {
151         this.status = Preconditions.checkNotNull(status, "status cannot be null");
152     }
153
154     @Override
155     public boolean isAugmenting() {
156         return augmenting;
157     }
158
159     @Override
160     public void setAugmenting(final boolean augmenting) {
161         this.augmenting = augmenting;
162     }
163
164     @Override
165     public boolean isAddedByUses() {
166         return addedByUses;
167     }
168
169     @Override
170     public void setAddedByUses(final boolean addedByUses) {
171         this.addedByUses = addedByUses;
172     }
173
174     @Override
175     public LeafSchemaNodeBuilder getOriginal() {
176         return originalBuilder;
177     }
178
179     @Override
180     public void setOriginal(final SchemaNodeBuilder builder) {
181         Preconditions.checkArgument(builder instanceof LeafSchemaNodeBuilder, "Original of leaf cannot be " + builder);
182         this.originalBuilder = (LeafSchemaNodeBuilder) builder;
183     }
184
185     @Override
186     public boolean isConfiguration() {
187         return configuration;
188     }
189
190     @Override
191     public void setConfiguration(final boolean configuration) {
192         this.configuration = configuration;
193     }
194
195     public String getDefaultStr() {
196         return defaultStr;
197     }
198
199     public void setDefaultStr(final String defaultStr) {
200         this.defaultStr = defaultStr;
201     }
202
203     public String getUnits() {
204         return unitsStr;
205     }
206
207     public void setUnits(final String unitsStr) {
208         this.unitsStr = unitsStr;
209     }
210
211     @Override
212     public int hashCode() {
213         final int prime = 31;
214         int result = 1;
215         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
216         return result;
217     }
218
219     @Override
220     public boolean equals(final Object obj) {
221         if (this == obj) {
222             return true;
223         }
224         if (obj == null) {
225             return false;
226         }
227         if (getClass() != obj.getClass()) {
228             return false;
229         }
230         LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
231         if (schemaPath == null) {
232             if (other.schemaPath != null) {
233                 return false;
234             }
235         } else if (!schemaPath.equals(other.schemaPath)) {
236             return false;
237         }
238         if (getParent() == null) {
239             if (other.getParent() != null) {
240                 return false;
241             }
242         } else if (!getParent().equals(other.getParent())) {
243             return false;
244         }
245         return true;
246     }
247
248     @Override
249     public String toString() {
250         return "leaf " + qname.getLocalName();
251     }
252
253 }