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;
11 import java.util.Collections;
12 import java.util.Date;
13 import java.util.List;
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;
24 import com.google.common.base.Optional;
25 import com.google.common.base.Preconditions;
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 this.unknownSchemaNodes = unknownSchemaNodes;
157 public Builder ranges(final List<RangeConstraint> ranges) {
158 if (ranges != null) {
159 this.ranges = ranges;
164 public Builder lengths(final List<LengthConstraint> lengths) {
165 if (lengths != null) {
166 this.lengths = lengths;
171 public Builder patterns(final List<PatternConstraint> patterns) {
172 if (patterns != null) {
173 this.patterns = patterns;
178 public Builder fractionDigits(final Integer fractionDigits) {
179 this.fractionDigits = fractionDigits;
183 public ExtendedType build() {
184 return new ExtendedType(this);
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;
200 this.ranges = builder.ranges;
201 this.lengths = builder.lengths;
202 this.patterns = builder.patterns;
203 this.fractionDigits = builder.fractionDigits;
207 public TypeDefinition<?> getBaseType() {
212 public String getUnits() {
217 public Object getDefaultValue() {
221 public boolean isAddedByUses() {
226 public QName getQName() {
231 public SchemaPath getPath() {
236 public String getDescription() {
241 public String getReference() {
246 public Status getStatus() {
251 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
252 return unknownSchemaNodes;
256 public boolean equals(final Object o) {
260 if (!(o instanceof ExtendedType)) {
264 ExtendedType that = (ExtendedType) o;
265 if (path != null ? !path.equals(that.path) : that.path != null) {
268 if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null)
275 public int hashCode() {
276 int result = typeName != null ? typeName.hashCode() : 0;
277 result = 31 * result + (path != null ? path.hashCode() : 0);
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);
303 return builder.toString();
306 public List<RangeConstraint> getRangeConstraints() {
310 public List<LengthConstraint> getLengthConstraints() {
314 public List<PatternConstraint> getPatternConstraints() {
318 public Integer getFractionDigits() {
319 return fractionDigits;