2 * Copyright (c) 2015 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.Preconditions;
11 import java.util.List;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
14 import org.opendaylight.yangtools.yang.model.api.Status;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
32 * Implementations of derived type.
34 * This is set of utility classes which implements derived YANG type,
35 * preserving original implemented interface instead of {@link ExtendedType}
36 * which does not preserve final type of data.
38 * @deprecated Use {@link org.opendaylight.yangtools.yang.model.util.type.DerivedTypes} or
39 * {@link org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes} instead
42 public abstract class DerivedType<T extends TypeDefinition<T>> implements TypeDefinition<T> {
44 private final ExtendedType definition;
45 private final Class<T> publicType;
47 DerivedType(final Class<T> publicType, final ExtendedType delegate) {
48 this.definition = Preconditions.checkNotNull(delegate);
49 this.publicType = Preconditions.checkNotNull(publicType);
52 public static TypeDefinition<?> from(final TypeDefinition<?> type) {
53 if(type instanceof ExtendedType) {
54 return from((ExtendedType) type);
59 public static TypeDefinition<?> from(final ExtendedType type) {
60 TypeDefinition<? extends TypeDefinition<?>> baseType = type;
61 while (baseType.getBaseType() != null) {
62 baseType = baseType.getBaseType();
64 if (baseType instanceof BinaryTypeDefinition) {
65 return new DerivedBinaryType(type);
67 if (baseType instanceof BooleanTypeDefinition) {
68 return new DerivedBooleanType(type);
70 if (baseType instanceof DecimalTypeDefinition) {
71 return new DerivedDecimalType(type);
73 if (baseType instanceof IdentityrefTypeDefinition) {
74 return new DerivedIdentityrefType(type);
76 if (baseType instanceof InstanceIdentifierTypeDefinition) {
77 return new DerivedInstanceIdentifierType(type);
79 if (baseType instanceof IntegerTypeDefinition) {
80 return new DerivedIntegerType(type);
82 if (baseType instanceof LeafrefTypeDefinition) {
83 return new DerivedLeafrefType(type);
85 if (baseType instanceof UnsignedIntegerTypeDefinition) {
86 return new DerivedUnsignedIntegerType(type);
88 if (baseType instanceof StringTypeDefinition) {
89 return new DerivedStringType(type);
91 if(baseType instanceof UnionTypeDefinition) {
92 return new DerivedUnionType(type);
94 if(baseType instanceof EnumTypeDefinition) {
95 return new DerivedEnumType(type);
97 if(baseType instanceof BitsTypeDefinition) {
98 return new DerivedBitsType(type);
100 throw new IllegalArgumentException("Not supported base type of " + baseType.getClass());
104 public final QName getQName() {
105 return definition.getQName();
109 public final SchemaPath getPath() {
110 return definition.getPath();
114 public final List<UnknownSchemaNode> getUnknownSchemaNodes() {
115 return definition.getUnknownSchemaNodes();
119 public final String getDescription() {
120 return definition.getDescription();
124 public final String getReference() {
125 return definition.getReference();
129 public final String getUnits() {
130 return definition.getUnits();
134 public final Object getDefaultValue() {
135 return definition.getDefaultValue();
139 public final Status getStatus() {
140 return definition.getStatus();
144 public final T getBaseType() {
145 final TypeDefinition<?> base = definition.getBaseType();
146 if (publicType.isInstance(base)) {
147 return publicType.cast(base);
148 } else if (base instanceof ExtendedType) {
149 return createDerived((ExtendedType) base);
151 throw new IllegalStateException("Unsupported base type.");
154 protected ExtendedType delegate() {
160 * Creates derived type from supplied ExtendedType, which will implement
161 * proper {@link TypeDefinition} interface.
163 * @param base Base definition, which does not implement concrete API
164 * @return wrapper which implements proper subinterface of {@link TypeDefinition}.
166 abstract T createDerived(ExtendedType base);