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 public final class DerivedTypes {
39 private DerivedTypes() {
40 throw new UnsupportedOperationException();
43 public static DerivedTypeBuilder<?> derivedTypeBuilder(@Nonnull final TypeDefinition<?> baseType,
44 @Nonnull final SchemaPath path) {
45 if (baseType instanceof BinaryTypeDefinition) {
46 return derivedBinaryBuilder((BinaryTypeDefinition) baseType, path);
47 } else if (baseType instanceof BitsTypeDefinition) {
48 return derivedBitsBuilder((BitsTypeDefinition) baseType, path);
49 } else if (baseType instanceof BooleanTypeDefinition) {
50 return derivedBooleanBuilder((BooleanTypeDefinition) baseType, path);
51 } else if (baseType instanceof DecimalTypeDefinition) {
52 return derivedDecimalBuilder((DecimalTypeDefinition) baseType, path);
53 } else if (baseType instanceof EmptyTypeDefinition) {
54 return derivedEmptyBuilder((EmptyTypeDefinition) baseType, path);
55 } else if (baseType instanceof EnumTypeDefinition) {
56 return derivedEnumerationBuilder((EnumTypeDefinition) baseType, path);
57 } else if (baseType instanceof IdentityrefTypeDefinition) {
58 return derivedIdentityrefBuilder((IdentityrefTypeDefinition) baseType, path);
59 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
60 return derivedInstanceIdentifierBuilder((InstanceIdentifierTypeDefinition) baseType, path);
61 } else if (baseType instanceof IntegerTypeDefinition) {
62 return derivedIntegerBuilder((IntegerTypeDefinition) baseType, path);
63 } else if (baseType instanceof LeafrefTypeDefinition) {
64 return derivedLeafrefBuilder((LeafrefTypeDefinition) baseType, path);
65 } else if (baseType instanceof StringTypeDefinition) {
66 return derivedStringBuilder((StringTypeDefinition) baseType, path);
67 } else if (baseType instanceof UnionTypeDefinition) {
68 return derivedUnionBuilder((UnionTypeDefinition) baseType, path);
69 } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
70 return derivedUnsignedBuilder((UnsignedIntegerTypeDefinition) baseType, path);
72 throw new IllegalArgumentException("Unhandled type definition class " + baseType.getClass());
77 * Check if a particular type is corresponds to int8. Unlike {@link BaseTypes#isInt8(TypeDefinition)}, this
78 * method performs recursive lookup to find the base type.
80 * @param type The type to check
81 * @return If the type belongs to the int8 type family.
82 * @throws NullPointerException if type is null
84 public static boolean isInt8(@Nonnull final TypeDefinition<?> type) {
85 return BaseTypes.isInt8(BaseTypes.baseTypeOf(type));
89 * Check if a particular type is corresponds to int16. Unlike {@link BaseTypes#isInt16(TypeDefinition)}, this
90 * method performs recursive lookup to find the base type.
92 * @param type The type to check
93 * @return If the type belongs to the int16 type family.
94 * @throws NullPointerException if type is null
96 public static boolean isInt16(@Nonnull final TypeDefinition<?> type) {
97 return BaseTypes.isInt16(BaseTypes.baseTypeOf(type));
101 * Check if a particular type is corresponds to int32. Unlike {@link BaseTypes#isInt32(TypeDefinition)}, this
102 * method performs recursive lookup to find the base type.
104 * @param type The type to check
105 * @return If the type belongs to the int32 type family.
106 * @throws NullPointerException if type is null
108 public static boolean isInt32(@Nonnull final TypeDefinition<?> type) {
109 return BaseTypes.isInt32(BaseTypes.baseTypeOf(type));
113 * Check if a particular type is corresponds to int64. Unlike {@link BaseTypes#isInt64(TypeDefinition)}, this
114 * method performs recursive lookup to find the base type.
116 * @param type The type to check
117 * @return If the type belongs to the int64 type family.
118 * @throws NullPointerException if type is null
120 public static boolean isInt64(@Nonnull final TypeDefinition<?> type) {
121 return BaseTypes.isInt64(BaseTypes.baseTypeOf(type));
125 * Check if a particular type is corresponds to uint8. Unlike {@link BaseTypes#isUint8(TypeDefinition)}, this
126 * method performs recursive lookup to find the base type.
128 * @param type The type to check
129 * @return If the type belongs to the uint8 type family.
130 * @throws NullPointerException if type is null
132 public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
133 return BaseTypes.isUint8(BaseTypes.baseTypeOf(type));
137 * Check if a particular type is corresponds to uint16. Unlike {@link BaseTypes#isUint16(TypeDefinition)}, this
138 * method performs recursive lookup to find the base type.
140 * @param type The type to check
141 * @return If the type belongs to the uint16 type family.
142 * @throws NullPointerException if type is null
144 public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
145 return BaseTypes.isUint16(BaseTypes.baseTypeOf(type));
149 * Check if a particular type is corresponds to uint32. Unlike {@link BaseTypes#isUint32(TypeDefinition)}, this
150 * method performs recursive lookup to find the base type.
152 * @param type The type to check
153 * @return If the type belongs to the uint32 type family.
154 * @throws NullPointerException if type is null
156 public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
157 return BaseTypes.isUint32(BaseTypes.baseTypeOf(type));
161 * Check if a particular type is corresponds to uint64. Unlike {@link BaseTypes#isUint64(TypeDefinition)}, this
162 * method performs recursive lookup to find the base type.
164 * @param type The type to check
165 * @return If the type belongs to the uint64 type family.
166 * @throws NullPointerException if type is null
168 public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
169 return BaseTypes.isUint64(BaseTypes.baseTypeOf(type));
172 private static DerivedTypeBuilder<BinaryTypeDefinition> derivedBinaryBuilder(@Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) {
173 return new DerivedTypeBuilder<BinaryTypeDefinition>(baseType, path) {
175 public BinaryTypeDefinition build() {
176 return new DerivedBinaryType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
177 getStatus(), getUnits(), getUnknownSchemaNodes());
182 private static DerivedTypeBuilder<BitsTypeDefinition> derivedBitsBuilder(final BitsTypeDefinition baseType, final SchemaPath path) {
183 return new DerivedTypeBuilder<BitsTypeDefinition>(baseType, path) {
185 public BitsTypeDefinition build() {
186 return new DerivedBitsType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
187 getStatus(), getUnits(), getUnknownSchemaNodes());
192 private static DerivedTypeBuilder<BooleanTypeDefinition> derivedBooleanBuilder(@Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) {
193 return new DerivedTypeBuilder<BooleanTypeDefinition>(baseType, path) {
195 public BooleanTypeDefinition build() {
196 return new DerivedBooleanType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
197 getStatus(), getUnits(), getUnknownSchemaNodes());
202 private static DerivedTypeBuilder<DecimalTypeDefinition> derivedDecimalBuilder(final DecimalTypeDefinition baseType, final SchemaPath path) {
203 return new DerivedTypeBuilder<DecimalTypeDefinition>(baseType, path) {
205 public DecimalTypeDefinition build() {
206 return new DerivedDecimalType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
207 getStatus(), getUnits(), getUnknownSchemaNodes());
212 private static DerivedTypeBuilder<EmptyTypeDefinition> derivedEmptyBuilder(final EmptyTypeDefinition baseType, final SchemaPath path) {
213 return new DerivedTypeBuilder<EmptyTypeDefinition>(baseType, path) {
215 public EmptyTypeDefinition build() {
216 return new DerivedEmptyType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
217 getStatus(), getUnits(), getUnknownSchemaNodes());
222 private static DerivedTypeBuilder<EnumTypeDefinition> derivedEnumerationBuilder(final EnumTypeDefinition baseType, final SchemaPath path) {
223 return new DerivedTypeBuilder<EnumTypeDefinition>(baseType, path) {
225 public EnumTypeDefinition build() {
226 return new DerivedEnumerationType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
227 getStatus(), getUnits(), getUnknownSchemaNodes());
232 private static DerivedTypeBuilder<IdentityrefTypeDefinition> derivedIdentityrefBuilder(final IdentityrefTypeDefinition baseType, final SchemaPath path) {
233 return new DerivedTypeBuilder<IdentityrefTypeDefinition>(baseType, path) {
235 public IdentityrefTypeDefinition build() {
236 return new DerivedIdentityrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
237 getStatus(), getUnits(), getUnknownSchemaNodes());
242 private static DerivedTypeBuilder<InstanceIdentifierTypeDefinition> derivedInstanceIdentifierBuilder(final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
243 return new DerivedTypeBuilder<InstanceIdentifierTypeDefinition>(baseType, path) {
245 public InstanceIdentifierTypeDefinition build() {
246 return new DerivedInstanceIdentifierType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
247 getStatus(), getUnits(), getUnknownSchemaNodes(), baseType.requireInstance());
252 private static DerivedTypeBuilder<IntegerTypeDefinition> derivedIntegerBuilder(final IntegerTypeDefinition baseType, final SchemaPath path) {
253 return new DerivedTypeBuilder<IntegerTypeDefinition>(baseType, path) {
255 public IntegerTypeDefinition build() {
256 return new DerivedIntegerType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
257 getStatus(), getUnits(), getUnknownSchemaNodes());
262 private static DerivedTypeBuilder<LeafrefTypeDefinition> derivedLeafrefBuilder(final LeafrefTypeDefinition baseType, final SchemaPath path) {
263 return new DerivedTypeBuilder<LeafrefTypeDefinition>(baseType, path) {
265 public LeafrefTypeDefinition build() {
266 return new DerivedLeafrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
267 getStatus(), getUnits(), getUnknownSchemaNodes());
272 private static DerivedTypeBuilder<StringTypeDefinition> derivedStringBuilder(final StringTypeDefinition baseType, final SchemaPath path) {
273 return new DerivedTypeBuilder<StringTypeDefinition>(baseType, path) {
275 public StringTypeDefinition build() {
276 return new DerivedStringType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
277 getStatus(), getUnits(), getUnknownSchemaNodes());
282 private static DerivedTypeBuilder<UnionTypeDefinition> derivedUnionBuilder(final UnionTypeDefinition baseType, final SchemaPath path) {
283 return new DerivedTypeBuilder<UnionTypeDefinition>(baseType, path) {
285 public DerivedUnionType build() {
286 return new DerivedUnionType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
287 getStatus(), getUnits(), getUnknownSchemaNodes());
292 private static DerivedTypeBuilder<UnsignedIntegerTypeDefinition> derivedUnsignedBuilder(final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) {
293 return new DerivedTypeBuilder<UnsignedIntegerTypeDefinition>(baseType, path) {
295 public UnsignedIntegerTypeDefinition build() {
296 return new DerivedUnsignedType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
297 getStatus(), getUnits(), getUnknownSchemaNodes());