Refactored YANG types resolving.
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / yang / model / util / ExtendedType.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.model.util;
9
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.opendaylight.controller.yang.common.QName;
14 import org.opendaylight.controller.yang.model.api.SchemaPath;
15 import org.opendaylight.controller.yang.model.api.Status;
16 import org.opendaylight.controller.yang.model.api.TypeDefinition;
17 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
19 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
20 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
21
22 public class ExtendedType implements TypeDefinition {
23
24     private final QName typeName;
25     private final TypeDefinition<?> baseType;
26     private final SchemaPath path;
27     private final String description;
28     private final String reference;
29     private final List<UnknownSchemaNode> unknownSchemaNodes;
30
31     private List<RangeConstraint> ranges = Collections.emptyList();
32     private List<LengthConstraint> lengths = Collections.emptyList();
33     private List<PatternConstraint> patterns = Collections.emptyList();
34     private Integer fractionDigits = null;
35
36
37     private Status status;
38     private String units;
39     private Object defaultValue;
40
41     public static class Builder {
42         private final QName typeName;
43         private final TypeDefinition<?> baseType;
44
45         private final SchemaPath path;
46         private final String description;
47         private final String reference;
48
49         private List<UnknownSchemaNode> unknownSchemaNodes = Collections.emptyList();;
50         private Status status = Status.CURRENT;
51         private String units = "";
52         private Object defaultValue = null;
53
54         private List<RangeConstraint> ranges = Collections.emptyList();
55         private List<LengthConstraint> lengths = Collections.emptyList();
56         private List<PatternConstraint> patterns = Collections.emptyList();
57         private Integer fractionDigits = null;
58
59         public Builder(final QName typeName, TypeDefinition<?> baseType,
60                 final String description, final String reference) {
61             this.typeName = typeName;
62             this.baseType = baseType;
63             this.path = BaseTypes.schemaPath(typeName);
64             this.description = description;
65             this.reference = reference;
66         }
67
68         public Builder status(Status status) {
69             this.status = status;
70             return this;
71         }
72
73         public Builder units(String units) {
74             this.units = units;
75             return this;
76         }
77
78         public Builder defaultValue(final Object defaultValue) {
79             this.defaultValue = defaultValue;
80             return this;
81         }
82
83         public Builder unknownSchemaNodes(final List<UnknownSchemaNode> unknownSchemaNodes) {
84             this.unknownSchemaNodes = unknownSchemaNodes;
85             return this;
86         }
87
88         public Builder ranges(final List<RangeConstraint> ranges) {
89             if(ranges != null) {
90                 this.ranges = ranges;
91             }
92             return this;
93         }
94
95         public Builder lengths(final List<LengthConstraint> lengths) {
96             if(lengths != null) {
97                 this.lengths = lengths;
98             }
99             return this;
100         }
101
102         public Builder patterns(final List<PatternConstraint> patterns) {
103             if(patterns != null) {
104                 this.patterns = patterns;
105             }
106             return this;
107         }
108
109         public Builder fractionDigits(final Integer fractionDigits) {
110             this.fractionDigits = fractionDigits;
111             return this;
112         }
113
114         public ExtendedType build() {
115             return new ExtendedType(this);
116         }
117     }
118
119     private ExtendedType(Builder builder) {
120         this.typeName = builder.typeName;
121         this.baseType = builder.baseType;
122         this.path = builder.path;
123         this.description = builder.description;
124         this.reference = builder.reference;
125         this.unknownSchemaNodes = builder.unknownSchemaNodes;
126         this.status = builder.status;
127         this.units = builder.units;
128         this.defaultValue = builder.defaultValue;
129
130         this.ranges = builder.ranges;
131         this.lengths = builder.lengths;
132         this.patterns = builder.patterns;
133         this.fractionDigits = builder.fractionDigits;
134     }
135
136     @Override
137     public TypeDefinition<?> getBaseType() {
138         return baseType;
139     }
140
141     @Override
142     public String getUnits() {
143         return units;
144     }
145
146     @Override
147     public Object getDefaultValue() {
148         return defaultValue;
149     }
150
151     @Override
152     public QName getQName() {
153         return typeName;
154     }
155
156     @Override
157     public SchemaPath getPath() {
158         return path;
159     }
160
161     @Override
162     public String getDescription() {
163         return description;
164     }
165
166     @Override
167     public String getReference() {
168         return reference;
169     }
170
171     @Override
172     public Status getStatus() {
173         return status;
174     }
175
176     @Override
177     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
178         return unknownSchemaNodes;
179     }
180
181     @Override
182     public int hashCode() {
183         final int prime = 31;
184         int result = 1;
185         result = prime * result
186                 + ((baseType == null) ? 0 : baseType.hashCode());
187         result = prime * result
188                 + ((defaultValue == null) ? 0 : defaultValue.hashCode());
189         result = prime * result
190                 + ((description == null) ? 0 : description.hashCode());
191         result = prime * result
192                 + ((unknownSchemaNodes == null) ? 0 : unknownSchemaNodes.hashCode());
193         result = prime * result + ((path == null) ? 0 : path.hashCode());
194         result = prime * result
195                 + ((reference == null) ? 0 : reference.hashCode());
196         result = prime * result + ((status == null) ? 0 : status.hashCode());
197         result = prime * result
198                 + ((typeName == null) ? 0 : typeName.hashCode());
199         result = prime * result + ((units == null) ? 0 : units.hashCode());
200         return result;
201     }
202
203     @Override
204     public boolean equals(Object obj) {
205         if (this == obj) {
206             return true;
207         }
208         if (obj == null) {
209             return false;
210         }
211         if (getClass() != obj.getClass()) {
212             return false;
213         }
214         ExtendedType other = (ExtendedType) obj;
215         if (baseType == null) {
216             if (other.baseType != null) {
217                 return false;
218             }
219         } else if (!baseType.equals(other.baseType)) {
220             return false;
221         }
222         if (defaultValue == null) {
223             if (other.defaultValue != null) {
224                 return false;
225             }
226         } else if (!defaultValue.equals(other.defaultValue)) {
227             return false;
228         }
229         if (description == null) {
230             if (other.description != null) {
231                 return false;
232             }
233         } else if (!description.equals(other.description)) {
234             return false;
235         }
236         if (unknownSchemaNodes == null) {
237             if (other.unknownSchemaNodes != null) {
238                 return false;
239             }
240         } else if (!unknownSchemaNodes.equals(other.unknownSchemaNodes)) {
241             return false;
242         }
243         if (path == null) {
244             if (other.path != null) {
245                 return false;
246             }
247         } else if (!path.equals(other.path)) {
248             return false;
249         }
250         if (reference == null) {
251             if (other.reference != null) {
252                 return false;
253             }
254         } else if (!reference.equals(other.reference)) {
255             return false;
256         }
257         if (status != other.status) {
258             return false;
259         }
260         if (typeName == null) {
261             if (other.typeName != null) {
262                 return false;
263             }
264         } else if (!typeName.equals(other.typeName)) {
265             return false;
266         }
267         if (units == null) {
268             if (other.units != null) {
269                 return false;
270             }
271         } else if (!units.equals(other.units)) {
272             return false;
273         }
274         return true;
275     }
276
277     @Override
278     public String toString() {
279         StringBuilder builder2 = new StringBuilder();
280         builder2.append("ExtendedType [typeName=");
281         builder2.append(typeName);
282         builder2.append(", baseType=");
283         builder2.append(baseType);
284         builder2.append(", path=");
285         builder2.append(path);
286         builder2.append(", description=");
287         builder2.append(description);
288         builder2.append(", reference=");
289         builder2.append(reference);
290         builder2.append(", unknownSchemaNodes=");
291         builder2.append(unknownSchemaNodes);
292         builder2.append(", status=");
293         builder2.append(status);
294         builder2.append(", units=");
295         builder2.append(units);
296         builder2.append(", defaultValue=");
297         builder2.append(defaultValue);
298         builder2.append("]");
299         return builder2.toString();
300     }
301
302     public List<RangeConstraint> getRanges() {
303         return ranges;
304     }
305
306     public List<LengthConstraint> getLengths() {
307         return lengths;
308     }
309
310     public List<PatternConstraint> getPatterns() {
311         return patterns;
312     }
313
314     public Integer getFractionDigits() {
315         return fractionDigits;
316     }
317 }