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;
23 * Extended Type represents YANG type derived from other type.
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.
31 public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
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;
40 private final Status status;
41 private final String units;
42 private final Object defaultValue;
43 private final boolean addedByUses;
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;
52 * Creates Builder for extended / derived type.
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.
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);
65 public static class Builder {
66 private final QName typeName;
67 private final TypeDefinition<?> baseType;
69 private final SchemaPath path;
70 private final String description;
71 private final String reference;
73 private List<UnknownSchemaNode> unknownSchemaNodes = Collections
75 private Status status = Status.CURRENT;
76 private String units = null;
77 private Object defaultValue = null;
78 private boolean addedByUses;
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;
86 * Creates Builder for extended / derived type.
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.
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;
104 public Builder status(final Status status) {
105 this.status = status;
109 public Builder units(final String units) {
114 public Builder defaultValue(final Object defaultValue) {
115 this.defaultValue = defaultValue;
119 public Builder addedByUses(final boolean addedByUses) {
120 this.addedByUses = addedByUses;
124 public Builder unknownSchemaNodes(
125 final List<UnknownSchemaNode> unknownSchemaNodes) {
126 if (unknownSchemaNodes.isEmpty()) {
127 this.unknownSchemaNodes = Collections.emptyList();
129 this.unknownSchemaNodes = unknownSchemaNodes;
134 public Builder ranges(final List<RangeConstraint> ranges) {
135 if (ranges != null) {
136 this.ranges = ranges;
141 public Builder lengths(final List<LengthConstraint> lengths) {
142 if (lengths != null) {
143 this.lengths = lengths;
148 public Builder patterns(final List<PatternConstraint> patterns) {
149 if (patterns != null) {
150 this.patterns = patterns;
155 public Builder fractionDigits(final Integer fractionDigits) {
156 this.fractionDigits = fractionDigits;
160 public ExtendedType build() {
161 return new ExtendedType(this);
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;
177 this.ranges = builder.ranges;
178 this.lengths = builder.lengths;
179 this.patterns = builder.patterns;
180 this.fractionDigits = builder.fractionDigits;
184 public TypeDefinition<?> getBaseType() {
189 public String getUnits() {
194 public Object getDefaultValue() {
198 public boolean isAddedByUses() {
203 public QName getQName() {
208 public SchemaPath getPath() {
213 public String getDescription() {
218 public String getReference() {
223 public Status getStatus() {
228 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
229 return unknownSchemaNodes;
233 public boolean equals(final Object o) {
237 if (!(o instanceof ExtendedType)) {
241 ExtendedType that = (ExtendedType) o;
242 if (path != null ? !path.equals(that.path) : that.path != null) {
245 if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null) {
253 public int hashCode() {
254 int result = typeName != null ? typeName.hashCode() : 0;
255 result = 31 * result + (path != null ? path.hashCode() : 0);
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);
281 return builder.toString();
284 public List<RangeConstraint> getRangeConstraints() {
288 public List<LengthConstraint> getLengthConstraints() {
292 public List<PatternConstraint> getPatternConstraints() {
296 public Integer getFractionDigits() {
297 return fractionDigits;