BUG-4638: add more type checking methods
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / type / DerivedTypes.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.util.type;
9
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;
27
28 /**
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.
31  *
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.
36  */
37 /*
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.
43  *
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.
46  */
47 @Beta
48 public final class DerivedTypes {
49     private DerivedTypes() {
50         throw new UnsupportedOperationException();
51     }
52
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);
81         } else {
82             throw new IllegalArgumentException("Unhandled type definition class " + baseType.getClass());
83         }
84     }
85
86     /**
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.
89      *
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
93      */
94     public static boolean isInt8(@Nonnull final TypeDefinition<?> type) {
95         return BaseTypes.isInt8(BaseTypes.baseTypeOf(type));
96     }
97
98     /**
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.
101      *
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
105      */
106     public static boolean isInt16(@Nonnull final TypeDefinition<?> type) {
107         return BaseTypes.isInt16(BaseTypes.baseTypeOf(type));
108     }
109
110     /**
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.
113      *
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
117      */
118     public static boolean isInt32(@Nonnull final TypeDefinition<?> type) {
119         return BaseTypes.isInt32(BaseTypes.baseTypeOf(type));
120     }
121
122     /**
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.
125      *
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
129      */
130     public static boolean isInt64(@Nonnull final TypeDefinition<?> type) {
131         return BaseTypes.isInt64(BaseTypes.baseTypeOf(type));
132     }
133
134     /**
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.
137      *
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
141      */
142     public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
143         return BaseTypes.isUint8(BaseTypes.baseTypeOf(type));
144     }
145
146     /**
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.
149      *
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
153      */
154     public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
155         return BaseTypes.isUint16(BaseTypes.baseTypeOf(type));
156     }
157
158     /**
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.
161      *
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
165      */
166     public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
167         return BaseTypes.isUint32(BaseTypes.baseTypeOf(type));
168     }
169
170     /**
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.
173      *
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
177      */
178     public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
179         return BaseTypes.isUint64(BaseTypes.baseTypeOf(type));
180     }
181
182     private static DerivedTypeBuilder<BinaryTypeDefinition> derivedBinaryBuilder(@Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) {
183         return new DerivedTypeBuilder<BinaryTypeDefinition>(baseType, path) {
184             @Override
185             public BinaryTypeDefinition build() {
186                 return new DerivedBinaryType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
187                     getStatus(), getUnits(), getUnknownSchemaNodes());
188             }
189         };
190     }
191
192     private static DerivedTypeBuilder<BitsTypeDefinition> derivedBitsBuilder(final BitsTypeDefinition baseType, final SchemaPath path) {
193         return new DerivedTypeBuilder<BitsTypeDefinition>(baseType, path) {
194             @Override
195             public BitsTypeDefinition build() {
196                 return new DerivedBitsType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
197                     getStatus(), getUnits(), getUnknownSchemaNodes());
198             }
199         };
200     }
201
202     private static DerivedTypeBuilder<BooleanTypeDefinition> derivedBooleanBuilder(@Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) {
203         return new DerivedTypeBuilder<BooleanTypeDefinition>(baseType, path) {
204             @Override
205             public BooleanTypeDefinition build() {
206                 return new DerivedBooleanType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
207                     getStatus(), getUnits(), getUnknownSchemaNodes());
208             }
209         };
210     }
211
212     private static DerivedTypeBuilder<DecimalTypeDefinition> derivedDecimalBuilder(final DecimalTypeDefinition baseType, final SchemaPath path) {
213         return new DerivedTypeBuilder<DecimalTypeDefinition>(baseType, path) {
214             @Override
215             public DecimalTypeDefinition build() {
216                 return new DerivedDecimalType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
217                     getStatus(), getUnits(), getUnknownSchemaNodes());
218             }
219         };
220     }
221
222     private static DerivedTypeBuilder<EmptyTypeDefinition> derivedEmptyBuilder(final EmptyTypeDefinition baseType, final SchemaPath path) {
223         return new DerivedTypeBuilder<EmptyTypeDefinition>(baseType, path) {
224             @Override
225             public EmptyTypeDefinition build() {
226                 return new DerivedEmptyType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
227                     getStatus(), getUnits(), getUnknownSchemaNodes());
228             }
229         };
230     }
231
232     private static DerivedTypeBuilder<EnumTypeDefinition> derivedEnumerationBuilder(final EnumTypeDefinition baseType, final SchemaPath path) {
233         return new DerivedTypeBuilder<EnumTypeDefinition>(baseType, path) {
234             @Override
235             public EnumTypeDefinition build() {
236                 return new DerivedEnumerationType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
237                     getStatus(), getUnits(), getUnknownSchemaNodes());
238             }
239         };
240     }
241
242     private static DerivedTypeBuilder<IdentityrefTypeDefinition> derivedIdentityrefBuilder(final IdentityrefTypeDefinition baseType, final SchemaPath path) {
243         return new DerivedTypeBuilder<IdentityrefTypeDefinition>(baseType, path) {
244             @Override
245             public IdentityrefTypeDefinition build() {
246                 return new DerivedIdentityrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
247                     getStatus(), getUnits(), getUnknownSchemaNodes());
248             }
249         };
250     }
251
252     private static DerivedTypeBuilder<InstanceIdentifierTypeDefinition> derivedInstanceIdentifierBuilder(final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
253         return new DerivedTypeBuilder<InstanceIdentifierTypeDefinition>(baseType, path) {
254             @Override
255             public InstanceIdentifierTypeDefinition build() {
256                 return new DerivedInstanceIdentifierType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
257                     getStatus(), getUnits(), getUnknownSchemaNodes(), baseType.requireInstance());
258             }
259         };
260     }
261
262     private static DerivedTypeBuilder<IntegerTypeDefinition> derivedIntegerBuilder(final IntegerTypeDefinition baseType, final SchemaPath path) {
263         return new DerivedTypeBuilder<IntegerTypeDefinition>(baseType, path) {
264             @Override
265             public IntegerTypeDefinition build() {
266                 return new DerivedIntegerType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
267                     getStatus(), getUnits(), getUnknownSchemaNodes());
268             }
269         };
270     }
271
272     private static DerivedTypeBuilder<LeafrefTypeDefinition> derivedLeafrefBuilder(final LeafrefTypeDefinition baseType, final SchemaPath path) {
273         return new DerivedTypeBuilder<LeafrefTypeDefinition>(baseType, path) {
274             @Override
275             public LeafrefTypeDefinition build() {
276                 return new DerivedLeafrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
277                     getStatus(), getUnits(), getUnknownSchemaNodes());
278             }
279         };
280     }
281
282     private static DerivedTypeBuilder<StringTypeDefinition> derivedStringBuilder(final StringTypeDefinition baseType, final SchemaPath path) {
283         return new DerivedTypeBuilder<StringTypeDefinition>(baseType, path) {
284             @Override
285             public StringTypeDefinition build() {
286                 return new DerivedStringType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
287                     getStatus(), getUnits(), getUnknownSchemaNodes());
288             }
289         };
290     }
291
292     private static DerivedTypeBuilder<UnionTypeDefinition> derivedUnionBuilder(final UnionTypeDefinition baseType, final SchemaPath path) {
293         return new DerivedTypeBuilder<UnionTypeDefinition>(baseType, path) {
294             @Override
295             public DerivedUnionType build() {
296                 return new DerivedUnionType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
297                     getStatus(), getUnits(), getUnknownSchemaNodes());
298             }
299         };
300     }
301
302     private static DerivedTypeBuilder<UnsignedIntegerTypeDefinition> derivedUnsignedBuilder(final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) {
303         return new DerivedTypeBuilder<UnsignedIntegerTypeDefinition>(baseType, path) {
304             @Override
305             public UnsignedIntegerTypeDefinition build() {
306                 return new DerivedUnsignedType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
307                     getStatus(), getUnits(), getUnknownSchemaNodes());
308             }
309         };
310     }
311 }