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