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