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.
33 * A derived type can redefine the default value, description, status and reference of a particular type definition.
34 * It can only refine the units attribute, as that attribute is tied to the semantics of the value. The default value,
35 * and units attributes are inherited from the super (base or restricted) type, others are left undefined if not
36 * explicitly set. Status defaults to current.
39 public final class DerivedTypes {
40 private DerivedTypes() {
41 throw new UnsupportedOperationException();
44 public static DerivedTypeBuilder<?> derivedTypeBuilder(@Nonnull final TypeDefinition<?> baseType,
45 @Nonnull final SchemaPath path) {
46 if (baseType instanceof BinaryTypeDefinition) {
47 return derivedBinaryBuilder((BinaryTypeDefinition) baseType, path);
48 } else if (baseType instanceof BitsTypeDefinition) {
49 return derivedBitsBuilder((BitsTypeDefinition) baseType, path);
50 } else if (baseType instanceof BooleanTypeDefinition) {
51 return derivedBooleanBuilder((BooleanTypeDefinition) baseType, path);
52 } else if (baseType instanceof DecimalTypeDefinition) {
53 return derivedDecimalBuilder((DecimalTypeDefinition) baseType, path);
54 } else if (baseType instanceof EmptyTypeDefinition) {
55 return derivedEmptyBuilder((EmptyTypeDefinition) baseType, path);
56 } else if (baseType instanceof EnumTypeDefinition) {
57 return derivedEnumerationBuilder((EnumTypeDefinition) baseType, path);
58 } else if (baseType instanceof IdentityrefTypeDefinition) {
59 return derivedIdentityrefBuilder((IdentityrefTypeDefinition) baseType, path);
60 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
61 return derivedInstanceIdentifierBuilder((InstanceIdentifierTypeDefinition) baseType, path);
62 } else if (baseType instanceof IntegerTypeDefinition) {
63 return derivedIntegerBuilder((IntegerTypeDefinition) baseType, path);
64 } else if (baseType instanceof LeafrefTypeDefinition) {
65 return derivedLeafrefBuilder((LeafrefTypeDefinition) baseType, path);
66 } else if (baseType instanceof StringTypeDefinition) {
67 return derivedStringBuilder((StringTypeDefinition) baseType, path);
68 } else if (baseType instanceof UnionTypeDefinition) {
69 return derivedUnionBuilder((UnionTypeDefinition) baseType, path);
70 } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
71 return derivedUnsignedBuilder((UnsignedIntegerTypeDefinition) baseType, path);
73 throw new IllegalArgumentException("Unhandled type definition class " + baseType.getClass());
78 * Check if a particular type is corresponds to int8. Unlike {@link BaseTypes#isInt8(TypeDefinition)}, this
79 * method performs recursive lookup to find the base type.
81 * @param type The type to check
82 * @return If the type belongs to the int8 type family.
83 * @throws NullPointerException if type is null
85 public static boolean isInt8(@Nonnull final TypeDefinition<?> type) {
86 return BaseTypes.isInt8(BaseTypes.baseTypeOf(type));
90 * Check if a particular type is corresponds to int16. Unlike {@link BaseTypes#isInt16(TypeDefinition)}, this
91 * method performs recursive lookup to find the base type.
93 * @param type The type to check
94 * @return If the type belongs to the int16 type family.
95 * @throws NullPointerException if type is null
97 public static boolean isInt16(@Nonnull final TypeDefinition<?> type) {
98 return BaseTypes.isInt16(BaseTypes.baseTypeOf(type));
102 * Check if a particular type is corresponds to int32. Unlike {@link BaseTypes#isInt32(TypeDefinition)}, this
103 * method performs recursive lookup to find the base type.
105 * @param type The type to check
106 * @return If the type belongs to the int32 type family.
107 * @throws NullPointerException if type is null
109 public static boolean isInt32(@Nonnull final TypeDefinition<?> type) {
110 return BaseTypes.isInt32(BaseTypes.baseTypeOf(type));
114 * Check if a particular type is corresponds to int64. Unlike {@link BaseTypes#isInt64(TypeDefinition)}, this
115 * method performs recursive lookup to find the base type.
117 * @param type The type to check
118 * @return If the type belongs to the int64 type family.
119 * @throws NullPointerException if type is null
121 public static boolean isInt64(@Nonnull final TypeDefinition<?> type) {
122 return BaseTypes.isInt64(BaseTypes.baseTypeOf(type));
126 * Check if a particular type is corresponds to uint8. Unlike {@link BaseTypes#isUint8(TypeDefinition)}, this
127 * method performs recursive lookup to find the base type.
129 * @param type The type to check
130 * @return If the type belongs to the uint8 type family.
131 * @throws NullPointerException if type is null
133 public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
134 return BaseTypes.isUint8(BaseTypes.baseTypeOf(type));
138 * Check if a particular type is corresponds to uint16. Unlike {@link BaseTypes#isUint16(TypeDefinition)}, this
139 * method performs recursive lookup to find the base type.
141 * @param type The type to check
142 * @return If the type belongs to the uint16 type family.
143 * @throws NullPointerException if type is null
145 public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
146 return BaseTypes.isUint16(BaseTypes.baseTypeOf(type));
150 * Check if a particular type is corresponds to uint32. Unlike {@link BaseTypes#isUint32(TypeDefinition)}, this
151 * method performs recursive lookup to find the base type.
153 * @param type The type to check
154 * @return If the type belongs to the uint32 type family.
155 * @throws NullPointerException if type is null
157 public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
158 return BaseTypes.isUint32(BaseTypes.baseTypeOf(type));
162 * Check if a particular type is corresponds to uint64. Unlike {@link BaseTypes#isUint64(TypeDefinition)}, this
163 * method performs recursive lookup to find the base type.
165 * @param type The type to check
166 * @return If the type belongs to the uint64 type family.
167 * @throws NullPointerException if type is null
169 public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
170 return BaseTypes.isUint64(BaseTypes.baseTypeOf(type));
173 private static DerivedTypeBuilder<BinaryTypeDefinition> derivedBinaryBuilder(
174 @Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) {
175 return new DerivedTypeBuilder<BinaryTypeDefinition>(baseType, path) {
177 public BinaryTypeDefinition build() {
178 return new DerivedBinaryType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
179 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
184 private static DerivedTypeBuilder<BitsTypeDefinition> derivedBitsBuilder(final BitsTypeDefinition baseType,
185 final SchemaPath path) {
186 return new DerivedTypeBuilder<BitsTypeDefinition>(baseType, path) {
188 public BitsTypeDefinition build() {
189 return new DerivedBitsType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
190 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
195 private static DerivedTypeBuilder<BooleanTypeDefinition> derivedBooleanBuilder(
196 @Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) {
197 return new DerivedTypeBuilder<BooleanTypeDefinition>(baseType, path) {
199 public BooleanTypeDefinition build() {
200 return new DerivedBooleanType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
201 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
206 private static DerivedTypeBuilder<DecimalTypeDefinition> derivedDecimalBuilder(final DecimalTypeDefinition baseType,
207 final SchemaPath path) {
208 return new DerivedTypeBuilder<DecimalTypeDefinition>(baseType, path) {
210 public DecimalTypeDefinition build() {
211 return new DerivedDecimalType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
212 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
217 private static DerivedTypeBuilder<EmptyTypeDefinition> derivedEmptyBuilder(final EmptyTypeDefinition baseType,
218 final SchemaPath path) {
219 return new DerivedTypeBuilder<EmptyTypeDefinition>(baseType, path) {
221 public EmptyTypeDefinition build() {
222 return new DerivedEmptyType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
223 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
228 private static DerivedTypeBuilder<EnumTypeDefinition> derivedEnumerationBuilder(final EnumTypeDefinition baseType,
229 final SchemaPath path) {
230 return new DerivedTypeBuilder<EnumTypeDefinition>(baseType, path) {
232 public EnumTypeDefinition build() {
233 return new DerivedEnumerationType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
234 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
239 private static DerivedTypeBuilder<IdentityrefTypeDefinition> derivedIdentityrefBuilder(
240 final IdentityrefTypeDefinition baseType, final SchemaPath path) {
241 return new DerivedTypeBuilder<IdentityrefTypeDefinition>(baseType, path) {
243 public IdentityrefTypeDefinition build() {
244 return new DerivedIdentityrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
245 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
250 private static DerivedTypeBuilder<InstanceIdentifierTypeDefinition> derivedInstanceIdentifierBuilder(
251 final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
252 return new DerivedTypeBuilder<InstanceIdentifierTypeDefinition>(baseType, path) {
254 public InstanceIdentifierTypeDefinition build() {
255 return new DerivedInstanceIdentifierType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
256 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes(), baseType.requireInstance());
261 private static DerivedTypeBuilder<IntegerTypeDefinition> derivedIntegerBuilder(final IntegerTypeDefinition baseType,
262 final SchemaPath path) {
263 return new DerivedTypeBuilder<IntegerTypeDefinition>(baseType, path) {
265 public IntegerTypeDefinition build() {
266 return new DerivedIntegerType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
267 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
272 private static DerivedTypeBuilder<LeafrefTypeDefinition> derivedLeafrefBuilder(final LeafrefTypeDefinition baseType,
273 final SchemaPath path) {
274 return new DerivedTypeBuilder<LeafrefTypeDefinition>(baseType, path) {
276 public LeafrefTypeDefinition build() {
277 return new DerivedLeafrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
278 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
283 private static DerivedTypeBuilder<StringTypeDefinition> derivedStringBuilder(final StringTypeDefinition baseType,
284 final SchemaPath path) {
285 return new DerivedTypeBuilder<StringTypeDefinition>(baseType, path) {
287 public StringTypeDefinition build() {
288 return new DerivedStringType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
289 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
294 private static DerivedTypeBuilder<UnionTypeDefinition> derivedUnionBuilder(final UnionTypeDefinition baseType,
295 final SchemaPath path) {
296 return new DerivedTypeBuilder<UnionTypeDefinition>(baseType, path) {
298 public DerivedUnionType build() {
299 return new DerivedUnionType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
300 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
305 private static DerivedTypeBuilder<UnsignedIntegerTypeDefinition> derivedUnsignedBuilder(
306 final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) {
307 return new DerivedTypeBuilder<UnsignedIntegerTypeDefinition>(baseType, path) {
309 public UnsignedIntegerTypeDefinition build() {
310 return new DerivedUnsignedType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
311 getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());