BUG-4638: rename LeafTypes to ConcreteTypes
[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 @Beta
38 public final class DerivedTypes {
39     private DerivedTypes() {
40         throw new UnsupportedOperationException();
41     }
42
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);
71         } else {
72             throw new IllegalArgumentException("Unhandled type definition class " + baseType.getClass());
73         }
74     }
75
76     /**
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.
79      *
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
83      */
84     public static boolean isInt8(@Nonnull final TypeDefinition<?> type) {
85         return BaseTypes.isInt8(BaseTypes.baseTypeOf(type));
86     }
87
88     /**
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.
91      *
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
95      */
96     public static boolean isInt16(@Nonnull final TypeDefinition<?> type) {
97         return BaseTypes.isInt16(BaseTypes.baseTypeOf(type));
98     }
99
100     /**
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.
103      *
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
107      */
108     public static boolean isInt32(@Nonnull final TypeDefinition<?> type) {
109         return BaseTypes.isInt32(BaseTypes.baseTypeOf(type));
110     }
111
112     /**
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.
115      *
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
119      */
120     public static boolean isInt64(@Nonnull final TypeDefinition<?> type) {
121         return BaseTypes.isInt64(BaseTypes.baseTypeOf(type));
122     }
123
124     /**
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.
127      *
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
131      */
132     public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
133         return BaseTypes.isUint8(BaseTypes.baseTypeOf(type));
134     }
135
136     /**
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.
139      *
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
143      */
144     public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
145         return BaseTypes.isUint16(BaseTypes.baseTypeOf(type));
146     }
147
148     /**
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.
151      *
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
155      */
156     public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
157         return BaseTypes.isUint32(BaseTypes.baseTypeOf(type));
158     }
159
160     /**
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.
163      *
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
167      */
168     public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
169         return BaseTypes.isUint64(BaseTypes.baseTypeOf(type));
170     }
171
172     private static DerivedTypeBuilder<BinaryTypeDefinition> derivedBinaryBuilder(@Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) {
173         return new DerivedTypeBuilder<BinaryTypeDefinition>(baseType, path) {
174             @Override
175             public BinaryTypeDefinition build() {
176                 return new DerivedBinaryType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
177                     getStatus(), getUnits(), getUnknownSchemaNodes());
178             }
179         };
180     }
181
182     private static DerivedTypeBuilder<BitsTypeDefinition> derivedBitsBuilder(final BitsTypeDefinition baseType, final SchemaPath path) {
183         return new DerivedTypeBuilder<BitsTypeDefinition>(baseType, path) {
184             @Override
185             public BitsTypeDefinition build() {
186                 return new DerivedBitsType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
187                     getStatus(), getUnits(), getUnknownSchemaNodes());
188             }
189         };
190     }
191
192     private static DerivedTypeBuilder<BooleanTypeDefinition> derivedBooleanBuilder(@Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) {
193         return new DerivedTypeBuilder<BooleanTypeDefinition>(baseType, path) {
194             @Override
195             public BooleanTypeDefinition build() {
196                 return new DerivedBooleanType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
197                     getStatus(), getUnits(), getUnknownSchemaNodes());
198             }
199         };
200     }
201
202     private static DerivedTypeBuilder<DecimalTypeDefinition> derivedDecimalBuilder(final DecimalTypeDefinition baseType, final SchemaPath path) {
203         return new DerivedTypeBuilder<DecimalTypeDefinition>(baseType, path) {
204             @Override
205             public DecimalTypeDefinition build() {
206                 return new DerivedDecimalType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
207                     getStatus(), getUnits(), getUnknownSchemaNodes());
208             }
209         };
210     }
211
212     private static DerivedTypeBuilder<EmptyTypeDefinition> derivedEmptyBuilder(final EmptyTypeDefinition baseType, final SchemaPath path) {
213         return new DerivedTypeBuilder<EmptyTypeDefinition>(baseType, path) {
214             @Override
215             public EmptyTypeDefinition build() {
216                 return new DerivedEmptyType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
217                     getStatus(), getUnits(), getUnknownSchemaNodes());
218             }
219         };
220     }
221
222     private static DerivedTypeBuilder<EnumTypeDefinition> derivedEnumerationBuilder(final EnumTypeDefinition baseType, final SchemaPath path) {
223         return new DerivedTypeBuilder<EnumTypeDefinition>(baseType, path) {
224             @Override
225             public EnumTypeDefinition build() {
226                 return new DerivedEnumerationType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
227                     getStatus(), getUnits(), getUnknownSchemaNodes());
228             }
229         };
230     }
231
232     private static DerivedTypeBuilder<IdentityrefTypeDefinition> derivedIdentityrefBuilder(final IdentityrefTypeDefinition baseType, final SchemaPath path) {
233         return new DerivedTypeBuilder<IdentityrefTypeDefinition>(baseType, path) {
234             @Override
235             public IdentityrefTypeDefinition build() {
236                 return new DerivedIdentityrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
237                     getStatus(), getUnits(), getUnknownSchemaNodes());
238             }
239         };
240     }
241
242     private static DerivedTypeBuilder<InstanceIdentifierTypeDefinition> derivedInstanceIdentifierBuilder(final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
243         return new DerivedTypeBuilder<InstanceIdentifierTypeDefinition>(baseType, path) {
244             @Override
245             public InstanceIdentifierTypeDefinition build() {
246                 return new DerivedInstanceIdentifierType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
247                     getStatus(), getUnits(), getUnknownSchemaNodes(), baseType.requireInstance());
248             }
249         };
250     }
251
252     private static DerivedTypeBuilder<IntegerTypeDefinition> derivedIntegerBuilder(final IntegerTypeDefinition baseType, final SchemaPath path) {
253         return new DerivedTypeBuilder<IntegerTypeDefinition>(baseType, path) {
254             @Override
255             public IntegerTypeDefinition build() {
256                 return new DerivedIntegerType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
257                     getStatus(), getUnits(), getUnknownSchemaNodes());
258             }
259         };
260     }
261
262     private static DerivedTypeBuilder<LeafrefTypeDefinition> derivedLeafrefBuilder(final LeafrefTypeDefinition baseType, final SchemaPath path) {
263         return new DerivedTypeBuilder<LeafrefTypeDefinition>(baseType, path) {
264             @Override
265             public LeafrefTypeDefinition build() {
266                 return new DerivedLeafrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
267                     getStatus(), getUnits(), getUnknownSchemaNodes());
268             }
269         };
270     }
271
272     private static DerivedTypeBuilder<StringTypeDefinition> derivedStringBuilder(final StringTypeDefinition baseType, final SchemaPath path) {
273         return new DerivedTypeBuilder<StringTypeDefinition>(baseType, path) {
274             @Override
275             public StringTypeDefinition build() {
276                 return new DerivedStringType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
277                     getStatus(), getUnits(), getUnknownSchemaNodes());
278             }
279         };
280     }
281
282     private static DerivedTypeBuilder<UnionTypeDefinition> derivedUnionBuilder(final UnionTypeDefinition baseType, final SchemaPath path) {
283         return new DerivedTypeBuilder<UnionTypeDefinition>(baseType, path) {
284             @Override
285             public DerivedUnionType build() {
286                 return new DerivedUnionType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
287                     getStatus(), getUnits(), getUnknownSchemaNodes());
288             }
289         };
290     }
291
292     private static DerivedTypeBuilder<UnsignedIntegerTypeDefinition> derivedUnsignedBuilder(final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) {
293         return new DerivedTypeBuilder<UnsignedIntegerTypeDefinition>(baseType, path) {
294             @Override
295             public UnsignedIntegerTypeDefinition build() {
296                 return new DerivedUnsignedType(getBaseType(), getPath(), getDefaultValue(), getDescription(), getReference(),
297                     getStatus(), getUnits(), getUnknownSchemaNodes());
298             }
299         };
300     }
301 }