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 java.net.URI;
11 import java.util.Collections;
12 import java.util.Date;
13 import java.util.List;
14
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
21 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
22 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
23
24 import com.google.common.base.Optional;
25 import com.google.common.base.Preconditions;
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             this.unknownSchemaNodes = unknownSchemaNodes;
154             return this;
155         }
156
157         public Builder ranges(final List<RangeConstraint> ranges) {
158             if (ranges != null) {
159                 this.ranges = ranges;
160             }
161             return this;
162         }
163
164         public Builder lengths(final List<LengthConstraint> lengths) {
165             if (lengths != null) {
166                 this.lengths = lengths;
167             }
168             return this;
169         }
170
171         public Builder patterns(final List<PatternConstraint> patterns) {
172             if (patterns != null) {
173                 this.patterns = patterns;
174             }
175             return this;
176         }
177
178         public Builder fractionDigits(final Integer fractionDigits) {
179             this.fractionDigits = fractionDigits;
180             return this;
181         }
182
183         public ExtendedType build() {
184             return new ExtendedType(this);
185         }
186     }
187
188     private ExtendedType(final Builder builder) {
189         this.typeName = builder.typeName;
190         this.baseType = builder.baseType;
191         this.path = builder.path;
192         this.description = builder.description;
193         this.reference = builder.reference;
194         this.unknownSchemaNodes = builder.unknownSchemaNodes;
195         this.status = builder.status;
196         this.units = builder.units;
197         this.defaultValue = builder.defaultValue;
198         this.addedByUses = builder.addedByUses;
199
200         this.ranges = builder.ranges;
201         this.lengths = builder.lengths;
202         this.patterns = builder.patterns;
203         this.fractionDigits = builder.fractionDigits;
204     }
205
206     @Override
207     public TypeDefinition<?> getBaseType() {
208         return baseType;
209     }
210
211     @Override
212     public String getUnits() {
213         return units;
214     }
215
216     @Override
217     public Object getDefaultValue() {
218         return defaultValue;
219     }
220
221     public boolean isAddedByUses() {
222         return addedByUses;
223     }
224
225     @Override
226     public QName getQName() {
227         return typeName;
228     }
229
230     @Override
231     public SchemaPath getPath() {
232         return path;
233     }
234
235     @Override
236     public String getDescription() {
237         return description;
238     }
239
240     @Override
241     public String getReference() {
242         return reference;
243     }
244
245     @Override
246     public Status getStatus() {
247         return status;
248     }
249
250     @Override
251     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
252         return unknownSchemaNodes;
253     }
254
255     @Override
256     public boolean equals(final Object o) {
257         if (this == o) {
258             return true;
259         }
260         if (!(o instanceof ExtendedType)) {
261             return false;
262         }
263
264         ExtendedType that = (ExtendedType) o;
265         if (path != null ? !path.equals(that.path) : that.path != null) {
266             return false;
267         }
268         if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null)
269             return false;
270
271         return true;
272     }
273
274     @Override
275     public int hashCode() {
276         int result = typeName != null ? typeName.hashCode() : 0;
277         result = 31 * result + (path != null ? path.hashCode() : 0);
278         return result;
279     }
280
281     @Override
282     public String toString() {
283         StringBuilder builder = new StringBuilder();
284         builder.append("ExtendedType [typeName=");
285         builder.append(typeName);
286         builder.append(", baseType=");
287         builder.append(baseType);
288         builder.append(", path=");
289         builder.append(path);
290         builder.append(", description=");
291         builder.append(description);
292         builder.append(", reference=");
293         builder.append(reference);
294         builder.append(", unknownSchemaNodes=");
295         builder.append(unknownSchemaNodes);
296         builder.append(", status=");
297         builder.append(status);
298         builder.append(", units=");
299         builder.append(units);
300         builder.append(", defaultValue=");
301         builder.append(defaultValue);
302         builder.append("]");
303         return builder.toString();
304     }
305
306     public List<RangeConstraint> getRangeConstraints() {
307         return ranges;
308     }
309
310     public List<LengthConstraint> getLengthConstraints() {
311         return lengths;
312     }
313
314     public List<PatternConstraint> getPatternConstraints() {
315         return patterns;
316     }
317
318     public Integer getFractionDigits() {
319         return fractionDigits;
320     }
321 }