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