Added Support for Union Type def resolving and bug fixes.
[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.net.URI;
11 import java.util.Collections;
12 import java.util.Date;
13 import java.util.List;
14
15 import org.opendaylight.controller.yang.common.QName;
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.TypeDefinition;
19 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
21 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
22 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
23
24 public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
25
26     private final QName typeName;
27     private final TypeDefinition<?> baseType;
28     private final SchemaPath path;
29     private final String description;
30     private final String reference;
31     private final List<UnknownSchemaNode> unknownSchemaNodes;
32
33     private List<RangeConstraint> ranges = Collections.emptyList();
34     private List<LengthConstraint> lengths = Collections.emptyList();
35     private List<PatternConstraint> patterns = Collections.emptyList();
36     private Integer fractionDigits = null;
37
38     private Status status;
39     private String units;
40     private Object defaultValue;
41
42     public static class Builder {
43         private final QName typeName;
44         private final TypeDefinition<?> baseType;
45
46         private final SchemaPath path;
47         private final String description;
48         private final String reference;
49
50         private List<UnknownSchemaNode> unknownSchemaNodes = Collections
51                 .emptyList();
52         private Status status = Status.CURRENT;
53         private String units = "";
54         private Object defaultValue = null;
55
56         private List<RangeConstraint> ranges = Collections.emptyList();
57         private List<LengthConstraint> lengths = Collections.emptyList();
58         private List<PatternConstraint> patterns = Collections.emptyList();
59         private Integer fractionDigits = null;
60
61         public Builder(final List<String> actualPath, final URI namespace,
62                 final Date revision, final QName typeName,
63                 TypeDefinition<?> baseType, final String description,
64                 final String reference) {
65             this.typeName = typeName;
66             this.baseType = baseType;
67             this.path = BaseTypes.schemaPath(actualPath, namespace, revision);
68             this.description = description;
69             this.reference = reference;
70         }
71
72         public Builder(final QName typeName, TypeDefinition<?> baseType,
73                 final String description, final String reference,
74                 SchemaPath path) {
75             this.typeName = typeName;
76             this.baseType = baseType;
77             this.path = path;
78             this.description = description;
79             this.reference = reference;
80         }
81
82         public Builder status(Status status) {
83             this.status = status;
84             return this;
85         }
86
87         public Builder units(String units) {
88             this.units = units;
89             return this;
90         }
91
92         public Builder defaultValue(final Object defaultValue) {
93             this.defaultValue = defaultValue;
94             return this;
95         }
96
97         public Builder unknownSchemaNodes(
98                 final List<UnknownSchemaNode> unknownSchemaNodes) {
99             this.unknownSchemaNodes = unknownSchemaNodes;
100             return this;
101         }
102
103         public Builder ranges(final List<RangeConstraint> ranges) {
104             if (ranges != null) {
105                 this.ranges = ranges;
106             }
107             return this;
108         }
109
110         public Builder lengths(final List<LengthConstraint> lengths) {
111             if (lengths != null) {
112                 this.lengths = lengths;
113             }
114             return this;
115         }
116
117         public Builder patterns(final List<PatternConstraint> patterns) {
118             if (patterns != null) {
119                 this.patterns = patterns;
120             }
121             return this;
122         }
123
124         public Builder fractionDigits(final Integer fractionDigits) {
125             this.fractionDigits = fractionDigits;
126             return this;
127         }
128
129         public ExtendedType build() {
130             return new ExtendedType(this);
131         }
132     }
133
134     private ExtendedType(Builder builder) {
135         this.typeName = builder.typeName;
136         this.baseType = builder.baseType;
137         this.path = builder.path;
138         this.description = builder.description;
139         this.reference = builder.reference;
140         this.unknownSchemaNodes = builder.unknownSchemaNodes;
141         this.status = builder.status;
142         this.units = builder.units;
143         this.defaultValue = builder.defaultValue;
144
145         this.ranges = builder.ranges;
146         this.lengths = builder.lengths;
147         this.patterns = builder.patterns;
148         this.fractionDigits = builder.fractionDigits;
149     }
150
151     @Override
152     public TypeDefinition<?> getBaseType() {
153         return baseType;
154     }
155
156     @Override
157     public String getUnits() {
158         return units;
159     }
160
161     @Override
162     public Object getDefaultValue() {
163         return defaultValue;
164     }
165
166     @Override
167     public QName getQName() {
168         return typeName;
169     }
170
171     @Override
172     public SchemaPath getPath() {
173         return path;
174     }
175
176     @Override
177     public String getDescription() {
178         return description;
179     }
180
181     @Override
182     public String getReference() {
183         return reference;
184     }
185
186     @Override
187     public Status getStatus() {
188         return status;
189     }
190
191     @Override
192     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
193         return unknownSchemaNodes;
194     }
195
196     @Override
197     public boolean equals(Object o) {
198         if (this == o) {
199             return true;
200         }
201         if (!(o instanceof ExtendedType)) {
202             return false;
203         }
204
205         ExtendedType that = (ExtendedType) o;
206         if (path != null ? !path.equals(that.path) : that.path != null) {
207             return false;
208         }
209         if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null)
210             return false;
211
212         return true;
213     }
214
215     @Override
216     public int hashCode() {
217         int result = typeName != null ? typeName.hashCode() : 0;
218         result = 31 * result + (path != null ? path.hashCode() : 0);
219         return result;
220     }
221
222     @Override
223     public String toString() {
224         StringBuilder builder = new StringBuilder();
225         builder.append("ExtendedType [typeName=");
226         builder.append(typeName);
227         builder.append(", baseType=");
228         builder.append(baseType);
229         builder.append(", path=");
230         builder.append(path);
231         builder.append(", description=");
232         builder.append(description);
233         builder.append(", reference=");
234         builder.append(reference);
235         builder.append(", unknownSchemaNodes=");
236         builder.append(unknownSchemaNodes);
237         builder.append(", status=");
238         builder.append(status);
239         builder.append(", units=");
240         builder.append(units);
241         builder.append(", defaultValue=");
242         builder.append(defaultValue);
243         builder.append("]");
244         return builder.toString();
245     }
246
247     public List<RangeConstraint> getRanges() {
248         return ranges;
249     }
250
251     public List<LengthConstraint> getLengths() {
252         return lengths;
253     }
254
255     public List<PatternConstraint> getPatterns() {
256         return patterns;
257     }
258
259     public Integer getFractionDigits() {
260         return fractionDigits;
261     }
262 }