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;
14 import java.util.Collections;
15 import java.util.Date;
16 import java.util.List;
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;
27 * Extended Type represents YANG type derived from other type.
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.
35 public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
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;
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;
49 private final Status status;
50 private final String units;
51 private final Object defaultValue;
52 private final boolean addedByUses;
56 * Creates Builder for extended / derived type.
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.
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);
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;
98 * @deprecated Use {@link ExtendedType#builder(QName, TypeDefinition, Optional, Optional, SchemaPath)} instead.
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));
110 * Creates Builder for extended / derived type.
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.
118 * @deprecated Use {@link ExtendedType#builder(QName, TypeDefinition, Optional, Optional, SchemaPath)} instead.
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;
131 public Builder status(final Status status) {
132 this.status = status;
136 public Builder units(final String units) {
141 public Builder defaultValue(final Object defaultValue) {
142 this.defaultValue = defaultValue;
146 public Builder addedByUses(final boolean addedByUses) {
147 this.addedByUses = addedByUses;
151 public Builder unknownSchemaNodes(
152 final List<UnknownSchemaNode> unknownSchemaNodes) {
153 if (unknownSchemaNodes.isEmpty()) {
154 this.unknownSchemaNodes = Collections.emptyList();
156 this.unknownSchemaNodes = unknownSchemaNodes;
161 public Builder ranges(final List<RangeConstraint> ranges) {
162 if (ranges != null) {
163 this.ranges = ranges;
168 public Builder lengths(final List<LengthConstraint> lengths) {
169 if (lengths != null) {
170 this.lengths = lengths;
175 public Builder patterns(final List<PatternConstraint> patterns) {
176 if (patterns != null) {
177 this.patterns = patterns;
182 public Builder fractionDigits(final Integer fractionDigits) {
183 this.fractionDigits = fractionDigits;
187 public ExtendedType build() {
188 return new ExtendedType(this);
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;
204 this.ranges = builder.ranges;
205 this.lengths = builder.lengths;
206 this.patterns = builder.patterns;
207 this.fractionDigits = builder.fractionDigits;
211 public TypeDefinition<?> getBaseType() {
216 public String getUnits() {
221 public Object getDefaultValue() {
225 public boolean isAddedByUses() {
230 public QName getQName() {
235 public SchemaPath getPath() {
240 public String getDescription() {
245 public String getReference() {
250 public Status getStatus() {
255 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
256 return unknownSchemaNodes;
260 public boolean equals(final Object o) {
264 if (!(o instanceof ExtendedType)) {
268 ExtendedType that = (ExtendedType) o;
269 if (path != null ? !path.equals(that.path) : that.path != null) {
272 if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null) {
280 public int hashCode() {
281 int result = typeName != null ? typeName.hashCode() : 0;
282 result = 31 * result + (path != null ? path.hashCode() : 0);
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);
308 return builder.toString();
311 public List<RangeConstraint> getRangeConstraints() {
315 public List<LengthConstraint> getLengthConstraints() {
319 public List<PatternConstraint> getPatternConstraints() {
323 public Integer getFractionDigits() {
324 return fractionDigits;