2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util;
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;
24 * Extended Type represents YANG type derived from other type.
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.
31 * @deprecated Use of this class is deprecated, use {@link DerivedType} instead.
34 public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
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;
43 private final Status status;
44 private final String units;
45 private final Object defaultValue;
46 private final boolean addedByUses;
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;
55 * Creates Builder for extended / derived type.
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.
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);
68 public static class Builder {
69 private final QName typeName;
70 private final TypeDefinition<?> baseType;
72 private final SchemaPath path;
73 private final String description;
74 private final String reference;
76 private List<UnknownSchemaNode> unknownSchemaNodes = Collections
78 private Status status = Status.CURRENT;
79 private String units = null;
80 private Object defaultValue = null;
81 private boolean addedByUses;
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;
89 * Creates Builder for extended / derived type.
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.
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;
107 public Builder status(final Status status) {
108 this.status = status;
112 public Builder units(final String units) {
117 public Builder defaultValue(final Object defaultValue) {
118 this.defaultValue = defaultValue;
122 public Builder addedByUses(final boolean addedByUses) {
123 this.addedByUses = addedByUses;
127 public Builder unknownSchemaNodes(
128 final List<UnknownSchemaNode> unknownSchemaNodes) {
129 if (unknownSchemaNodes.isEmpty()) {
130 this.unknownSchemaNodes = Collections.emptyList();
132 this.unknownSchemaNodes = unknownSchemaNodes;
137 public Builder ranges(final List<RangeConstraint> ranges) {
138 if (ranges != null) {
139 this.ranges = ranges;
144 public Builder lengths(final List<LengthConstraint> lengths) {
145 if (lengths != null) {
146 this.lengths = lengths;
151 public Builder patterns(final List<PatternConstraint> patterns) {
152 if (patterns != null) {
153 this.patterns = patterns;
158 public Builder fractionDigits(final Integer fractionDigits) {
159 this.fractionDigits = fractionDigits;
163 public ExtendedType build() {
164 return new ExtendedType(this);
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;
180 this.ranges = builder.ranges;
181 this.lengths = builder.lengths;
182 this.patterns = builder.patterns;
183 this.fractionDigits = builder.fractionDigits;
187 public TypeDefinition<?> getBaseType() {
192 public String getUnits() {
197 public Object getDefaultValue() {
201 public boolean isAddedByUses() {
206 public QName getQName() {
211 public SchemaPath getPath() {
216 public String getDescription() {
221 public String getReference() {
226 public Status getStatus() {
231 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
232 return unknownSchemaNodes;
236 public boolean equals(final Object o) {
240 if (!(o instanceof ExtendedType)) {
244 ExtendedType that = (ExtendedType) o;
245 if (path != null ? !path.equals(that.path) : that.path != null) {
248 if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null) {
256 public int hashCode() {
257 int result = typeName != null ? typeName.hashCode() : 0;
258 result = 31 * result + (path != null ? path.hashCode() : 0);
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);
284 return builder.toString();
287 public List<RangeConstraint> getRangeConstraints() {
291 public List<LengthConstraint> getLengthConstraints() {
295 public List<PatternConstraint> getPatternConstraints() {
299 public Integer getFractionDigits() {
300 return fractionDigits;