2 * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.type;
10 import com.google.common.annotations.Beta;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
13 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
14 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
29 * YANG defines 'derived type' as a type created through a 'typedef' statement. These types are exposed in the
30 * hierarchical namespace and can be looked up.
32 * A derived type can redefine the default value, description, status and reference of a particular type definition.
33 * It can only refine the units attribute, as that attribute is tied to the semantics of the value. The default value,
34 * and units attributes are inherited from the super (base or restricted) type, others are left undefined if not
35 * explicitly set. Status defaults to current.
38 * FIXME: Create ConcreteTypes
39 * Leaf and leaf-list statements provide for a similar mechanism by which a particular type is changed, most
40 * notably with the ability to redefine the default type. The resulting types could conceivably be called
41 * 'concrete types', as they cannot be referenced by another leaf or type definition. This aspect needs to be
42 * split out into a 'ConcreteTypes' class.
44 * Builders should use the fly-weight pattern to minimize footprint for cases when leaves do not override any
45 * aspect of the base type.
48 public final class DerivedTypes {
49 private DerivedTypes() {
50 throw new UnsupportedOperationException();
53 public static DerivedTypeBuilder<?> derivedTypeBuilder(@Nonnull final TypeDefinition<?> baseType,
54 @Nonnull final SchemaPath path) {
55 if (baseType instanceof BinaryTypeDefinition) {
56 return derivedBinaryBuilder((BinaryTypeDefinition) baseType, path);
57 } else if (baseType instanceof BitsTypeDefinition) {
58 return derivedBitsBuilder((BitsTypeDefinition) baseType, path);
59 } else if (baseType instanceof BooleanTypeDefinition) {
60 return derivedBooleanBuilder((BooleanTypeDefinition) baseType, path);
61 } else if (baseType instanceof DecimalTypeDefinition) {
62 return derivedDecimalBuilder((DecimalTypeDefinition) baseType, path);
63 } else if (baseType instanceof EmptyTypeDefinition) {
64 return derivedEmptyBuilder((EmptyTypeDefinition) baseType, path);
65 } else if (baseType instanceof EnumTypeDefinition) {
66 return derivedEnumerationBuilder((EnumTypeDefinition) baseType, path);
67 } else if (baseType instanceof IdentityrefTypeDefinition) {
68 return derivedIdentityrefBuilder((IdentityrefTypeDefinition) baseType, path);
69 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
70 return derivedInstanceIdentifierBuilder((InstanceIdentifierTypeDefinition) baseType, path);
71 } else if (baseType instanceof IntegerTypeDefinition) {
72 return derivedIntegerBuilder((IntegerTypeDefinition) baseType, path);
73 } else if (baseType instanceof LeafrefTypeDefinition) {
74 return derivedLeafrefBuilder((LeafrefTypeDefinition) baseType, path);
75 } else if (baseType instanceof StringTypeDefinition) {
76 return derivedStringBuilder((StringTypeDefinition) baseType, path);
77 } else if (baseType instanceof UnionTypeDefinition) {
78 return derivedUnionBuilder((UnionTypeDefinition) baseType, path);
79 } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
80 return derivedUnsignedBuilder((UnsignedIntegerTypeDefinition) baseType, path);
82 throw new IllegalArgumentException("Unhandled type definition class " + baseType.getClass());
87 * Check if a particular type is corresponds to int8. Unlike {@link BaseTypes#isInt8(TypeDefinition)}, this
88 * method performs recursive lookup to find the base type.
90 * @param type The type to check
91 * @return If the type belongs to the int8 type family.
92 * @throws NullPointerException if type is null
94 public static boolean isInt8(@Nonnull final TypeDefinition<?> type) {
95 return BaseTypes.isInt8(BaseTypes.baseTypeOf(type));
99 * Check if a particular type is corresponds to int16. Unlike {@link BaseTypes#isInt16(TypeDefinition)}, this
100 * method performs recursive lookup to find the base type.
102 * @param type The type to check
103 * @return If the type belongs to the int16 type family.
104 * @throws NullPointerException if type is null
106 public static boolean isInt16(@Nonnull final TypeDefinition<?> type) {
107 return BaseTypes.isInt16(BaseTypes.baseTypeOf(type));
111 * Check if a particular type is corresponds to int32. Unlike {@link BaseTypes#isInt32(TypeDefinition)}, this
112 * method performs recursive lookup to find the base type.
114 * @param type The type to check
115 * @return If the type belongs to the int32 type family.
116 * @throws NullPointerException if type is null
118 public static boolean isInt32(@Nonnull final TypeDefinition<?> type) {
119 return BaseTypes.isInt32(BaseTypes.baseTypeOf(type));
123 * Check if a particular type is corresponds to int64. Unlike {@link BaseTypes#isInt64(TypeDefinition)}, this
124 * method performs recursive lookup to find the base type.
126 * @param type The type to check
127 * @return If the type belongs to the int64 type family.
128 * @throws NullPointerException if type is null
130 public static boolean isInt64(@Nonnull final TypeDefinition<?> type) {
131 return BaseTypes.isInt64(BaseTypes.baseTypeOf(type));
135 * Check if a particular type is corresponds to uint8. Unlike {@link BaseTypes#isUint8(TypeDefinition)}, this
136 * method performs recursive lookup to find the base type.
138 * @param type The type to check
139 * @return If the type belongs to the uint8 type family.
140 * @throws NullPointerException if type is null
142 public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
143 return BaseTypes.isUint8(BaseTypes.baseTypeOf(type));
147 * Check if a particular type is corresponds to uint16. Unlike {@link BaseTypes#isUint16(TypeDefinition)}, this
148 * method performs recursive lookup to find the base type.
150 * @param type The type to check
151 * @return If the type belongs to the uint16 type family.
152 * @throws NullPointerException if type is null
154 public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
155 return BaseTypes.isUint16(BaseTypes.baseTypeOf(type));
159 * Check if a particular type is corresponds to uint32. Unlike {@link BaseTypes#isUint32(TypeDefinition)}, this
160 * method performs recursive lookup to find the base type.
162 * @param type The type to check
163 * @return If the type belongs to the uint32 type family.
164 * @throws NullPointerException if type is null
166 public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
167 return BaseTypes.isUint32(BaseTypes.baseTypeOf(type));
171 * Check if a particular type is corresponds to uint64. Unlike {@link BaseTypes#isUint64(TypeDefinition)}, this
172 * method performs recursive lookup to find the base type.
174 * @param type The type to check
175 * @return If the type belongs to the uint64 type family.
176 * @throws NullPointerException if type is null
178 public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
179 return BaseTypes.isUint64(BaseTypes.baseTypeOf(type));
182 private static DerivedTypeBuilder<BinaryTypeDefinition> derivedBinaryBuilder(@Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) {
183 return new DerivedTypeBuilder<BinaryTypeDefinition>(baseType, path) {
185 public BinaryTypeDefinition build() {
186 return new DerivedBinaryType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
187 getStatus(), getUnits(), getUnknownSchemaNodes());
192 private static DerivedTypeBuilder<BitsTypeDefinition> derivedBitsBuilder(final BitsTypeDefinition baseType, final SchemaPath path) {
193 return new DerivedTypeBuilder<BitsTypeDefinition>(baseType, path) {
195 public BitsTypeDefinition build() {
196 return new DerivedBitsType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
197 getStatus(), getUnits(), getUnknownSchemaNodes());
202 private static DerivedTypeBuilder<BooleanTypeDefinition> derivedBooleanBuilder(@Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) {
203 return new DerivedTypeBuilder<BooleanTypeDefinition>(baseType, path) {
205 public BooleanTypeDefinition build() {
206 return new DerivedBooleanType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
207 getStatus(), getUnits(), getUnknownSchemaNodes());
212 private static DerivedTypeBuilder<DecimalTypeDefinition> derivedDecimalBuilder(final DecimalTypeDefinition baseType, final SchemaPath path) {
213 return new DerivedTypeBuilder<DecimalTypeDefinition>(baseType, path) {
215 public DecimalTypeDefinition build() {
216 return new DerivedDecimalType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
217 getStatus(), getUnits(), getUnknownSchemaNodes());
222 private static DerivedTypeBuilder<EmptyTypeDefinition> derivedEmptyBuilder(final EmptyTypeDefinition baseType, final SchemaPath path) {
223 return new DerivedTypeBuilder<EmptyTypeDefinition>(baseType, path) {
225 public EmptyTypeDefinition build() {
226 return new DerivedEmptyType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
227 getStatus(), getUnits(), getUnknownSchemaNodes());
232 private static DerivedTypeBuilder<EnumTypeDefinition> derivedEnumerationBuilder(final EnumTypeDefinition baseType, final SchemaPath path) {
233 return new DerivedTypeBuilder<EnumTypeDefinition>(baseType, path) {
235 public EnumTypeDefinition build() {
236 return new DerivedEnumerationType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
237 getStatus(), getUnits(), getUnknownSchemaNodes());
242 private static DerivedTypeBuilder<IdentityrefTypeDefinition> derivedIdentityrefBuilder(final IdentityrefTypeDefinition baseType, final SchemaPath path) {
243 return new DerivedTypeBuilder<IdentityrefTypeDefinition>(baseType, path) {
245 public IdentityrefTypeDefinition build() {
246 return new DerivedIdentityrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
247 getStatus(), getUnits(), getUnknownSchemaNodes());
252 private static DerivedTypeBuilder<InstanceIdentifierTypeDefinition> derivedInstanceIdentifierBuilder(final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
253 return new DerivedTypeBuilder<InstanceIdentifierTypeDefinition>(baseType, path) {
255 public InstanceIdentifierTypeDefinition build() {
256 return new DerivedInstanceIdentifierType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
257 getStatus(), getUnits(), getUnknownSchemaNodes(), baseType.requireInstance());
262 private static DerivedTypeBuilder<IntegerTypeDefinition> derivedIntegerBuilder(final IntegerTypeDefinition baseType, final SchemaPath path) {
263 return new DerivedTypeBuilder<IntegerTypeDefinition>(baseType, path) {
265 public IntegerTypeDefinition build() {
266 return new DerivedIntegerType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
267 getStatus(), getUnits(), getUnknownSchemaNodes());
272 private static DerivedTypeBuilder<LeafrefTypeDefinition> derivedLeafrefBuilder(final LeafrefTypeDefinition baseType, final SchemaPath path) {
273 return new DerivedTypeBuilder<LeafrefTypeDefinition>(baseType, path) {
275 public LeafrefTypeDefinition build() {
276 return new DerivedLeafrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
277 getStatus(), getUnits(), getUnknownSchemaNodes());
282 private static DerivedTypeBuilder<StringTypeDefinition> derivedStringBuilder(final StringTypeDefinition baseType, final SchemaPath path) {
283 return new DerivedTypeBuilder<StringTypeDefinition>(baseType, path) {
285 public StringTypeDefinition build() {
286 return new DerivedStringType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
287 getStatus(), getUnits(), getUnknownSchemaNodes());
292 private static DerivedTypeBuilder<UnionTypeDefinition> derivedUnionBuilder(final UnionTypeDefinition baseType, final SchemaPath path) {
293 return new DerivedTypeBuilder<UnionTypeDefinition>(baseType, path) {
295 public DerivedUnionType build() {
296 return new DerivedUnionType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
297 getStatus(), getUnits(), getUnknownSchemaNodes());
302 private static DerivedTypeBuilder<UnsignedIntegerTypeDefinition> derivedUnsignedBuilder(final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) {
303 return new DerivedTypeBuilder<UnsignedIntegerTypeDefinition>(baseType, path) {
305 public UnsignedIntegerTypeDefinition build() {
306 return new DerivedUnsignedType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
307 getStatus(), getUnits(), getUnknownSchemaNodes());