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