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