BUG-4638: add more type checking methods
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / type / BaseTypes.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.BooleanTypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
21
22 /**
23  * Utility access methods for creating and accessing YANG base type definitions. YANG types come in two basic variants,
24  * depending on whether they fully define their base instance or model input is required to fully-form the type.
25  *
26  * The following types have their base type fully specified and are exposed as appropriate TypeDefinition sub-interfaces:
27  * <ul>
28  *     <li>boolean</li>
29  *     <li>empty</li>
30  *     <li>binary</li>
31  *     <li>int{8,16,32,64}</li>
32  *     <li>string</li>
33  *     <li>uint{8,16,32,64}</li>
34  * </ul>
35  *
36  * The following types require additional specification in the model and are exposed by means of a specialized
37  * {@link TypeBuilder}s for each type:
38  * <ul>
39  *    <li>decimal64</li>
40  *    <li>instance-identifier</li>
41  *    <li>enumeration</li>
42  *    <li>identityref</li>
43  *    <li>leafref</li>
44  *    <li>union</li>
45  * </ul>
46  */
47 @Beta
48 public final class BaseTypes {
49     private BaseTypes() {
50         throw new UnsupportedOperationException();
51     }
52
53     public static BinaryTypeDefinition binaryType() {
54         return BaseBinaryType.INSTANCE;
55     }
56
57     public static BitsTypeBuilder bitsTypeBuilder(final SchemaPath path) {
58         return new BitsTypeBuilder(path);
59     }
60
61     public static BooleanTypeDefinition booleanType() {
62         return BaseBooleanType.INSTANCE;
63     }
64
65     public static DecimalTypeBuilder decimalTypeBuilder(final SchemaPath path) {
66         return new DecimalTypeBuilder(path);
67     }
68
69     public static EmptyTypeDefinition emptyType() {
70         return BaseEmptyType.INSTANCE;
71     }
72
73     public static EnumerationTypeBuilder enumerationTypeBuilder(final SchemaPath path) {
74         return new EnumerationTypeBuilder(path);
75     }
76
77     public static IdentityrefTypeBuilder identityrefTypeBuilder(final SchemaPath path) {
78         return new IdentityrefTypeBuilder(path);
79     }
80
81     public static InstanceIdentifierTypeDefinition instanceIdentifierType() {
82         return BaseInstanceIdentifierType.INSTANCE;
83     }
84
85     public static IntegerTypeDefinition int8Type() {
86         return BaseInt8Type.INSTANCE;
87     }
88
89     public static boolean isInt8(final TypeDefinition<?> type) {
90         return BaseInt8Type.INSTANCE.getPath().equals(type.getPath());
91     }
92
93     public static IntegerTypeDefinition int16Type() {
94         return BaseInt16Type.INSTANCE;
95     }
96
97     public static boolean isInt16(final TypeDefinition<?> type) {
98         return BaseInt16Type.INSTANCE.getPath().equals(type.getPath());
99     }
100
101     public static IntegerTypeDefinition int32Type() {
102         return BaseInt32Type.INSTANCE;
103     }
104
105     public static boolean isInt32(final TypeDefinition<?> type) {
106         return BaseInt32Type.INSTANCE.getPath().equals(type.getPath());
107     }
108
109     public static IntegerTypeDefinition int64Type() {
110         return BaseInt64Type.INSTANCE;
111     }
112
113     public static boolean isInt64(final TypeDefinition<?> type) {
114         return BaseInt64Type.INSTANCE.getPath().equals(type.getPath());
115     }
116
117     public static LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) {
118         return new LeafrefTypeBuilder(path);
119     }
120
121     public static StringTypeDefinition stringType() {
122         return BaseStringType.INSTANCE;
123     }
124
125     public static UnionTypeBuilder unionTypeBuilder(final SchemaPath path) {
126         return new UnionTypeBuilder(path);
127     }
128
129     public static UnsignedIntegerTypeDefinition uint8Type() {
130         return BaseUint8Type.INSTANCE;
131     }
132
133     /**
134      * Check if a particular type is the base type for uint8. Unlike {@link DerivedTypes#isUint8(TypeDefinition)},
135      * this method does not perform recursive base type lookup.
136      *
137      * @param type The type to check
138      * @return If the type corresponds to the base uint8 type.
139      * @throws NullPointerException if type is null
140      */
141     public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
142         return BaseUint8Type.INSTANCE.getPath().equals(type.getPath());
143     }
144
145     public static UnsignedIntegerTypeDefinition uint16Type() {
146         return BaseUint16Type.INSTANCE;
147     }
148
149     /**
150      * Check if a particular type is the base type for uint16. Unlike {@link DerivedTypes#isUint16(TypeDefinition)},
151      * this method does not perform recursive base type lookup.
152      *
153      * @param type The type to check
154      * @return If the type corresponds to the base uint16 type.
155      * @throws NullPointerException if type is null
156      */
157     public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
158         return BaseUint16Type.INSTANCE.getPath().equals(type.getPath());
159     }
160
161     public static UnsignedIntegerTypeDefinition uint32Type() {
162         return BaseUint32Type.INSTANCE;
163     }
164
165     /**
166      * Check if a particular type is the base type for uint32. Unlike {@link DerivedTypes#isUint32(TypeDefinition)},
167      * this method does not perform recursive base type lookup.
168      *
169      * @param type The type to check
170      * @return If the type corresponds to the base uint32 type.
171      * @throws NullPointerException if type is null
172      */
173     public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
174         return BaseUint32Type.INSTANCE.getPath().equals(type.getPath());
175     }
176
177     public static UnsignedIntegerTypeDefinition uint64Type() {
178         return BaseUint64Type.INSTANCE;
179     }
180
181     /**
182      * Check if a particular type is the base type for uint64. Unlike {@link DerivedTypes#isUint64(TypeDefinition)},
183      * this method does not perform recursive base type lookup.
184      *
185      * @param type The type to check
186      * @return If the type corresponds to the base uint64 type.
187      * @throws NullPointerException if type is null
188      */
189     public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
190         return BaseUint64Type.INSTANCE.getPath().equals(type.getPath());
191     }
192
193     /**
194      * Return the base type of a particular type. This method performs recursive lookup through the type's base type
195      * until it finds the last element and returns it. If the argument is already the base type, it is returned as is.
196      *
197      * @param type Type for which to find the base type
198      * @return Base type of specified type
199      * @throws NullPointerException if type is null
200      */
201     public static TypeDefinition<?> baseTypeOf(@Nonnull final TypeDefinition<?> type) {
202         TypeDefinition<?> ret = type;
203         while (ret.getBaseType() != null) {
204             ret = ret.getBaseType();
205         }
206         return ret;
207     }
208 }