Merge branch 'master' of ../controller
[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 org.eclipse.jdt.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 @NonNull BinaryTypeDefinition binaryType() {
63         return BaseBinaryType.INSTANCE;
64     }
65
66     public static @NonNull BitsTypeBuilder bitsTypeBuilder(final SchemaPath path) {
67         return new BitsTypeBuilder(path);
68     }
69
70     public static @NonNull BooleanTypeDefinition booleanType() {
71         return BaseBooleanType.INSTANCE;
72     }
73
74     public static @NonNull DecimalTypeBuilder decimalTypeBuilder(final SchemaPath path) {
75         return new DecimalTypeBuilder(path);
76     }
77
78     public static @NonNull EmptyTypeDefinition emptyType() {
79         return BaseEmptyType.INSTANCE;
80     }
81
82     public static @NonNull EnumerationTypeBuilder enumerationTypeBuilder(final SchemaPath path) {
83         return new EnumerationTypeBuilder(path);
84     }
85
86     public static @NonNull IdentityrefTypeBuilder identityrefTypeBuilder(final SchemaPath path) {
87         return new IdentityrefTypeBuilder(path);
88     }
89
90     public static @NonNull InstanceIdentifierTypeDefinition instanceIdentifierType() {
91         return BaseInstanceIdentifierType.INSTANCE;
92     }
93
94     public static @NonNull 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 @NonNull 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 @NonNull 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 @NonNull 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 @NonNull LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) {
152         return new LeafrefTypeBuilder(path);
153     }
154
155     public static @NonNull 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 @NonNull Uint8TypeDefinition uint8Type() {
164         return BaseUint8Type.INSTANCE;
165     }
166
167     /**
168      * Check if a particular type is the base type for uint8.
169      *
170      * @param type The type to check
171      * @return If the type corresponds to the base uint8 type.
172      * @throws NullPointerException if type is null
173      */
174     public static boolean isUint8(final @NonNull TypeDefinition<?> type) {
175         return BaseUint8Type.INSTANCE.getPath().equals(type.getPath());
176     }
177
178     public static @NonNull Uint16TypeDefinition uint16Type() {
179         return BaseUint16Type.INSTANCE;
180     }
181
182     /**
183      * Check if a particular type is the base type for uint16.
184      *
185      * @param type The type to check
186      * @return If the type corresponds to the base uint16 type.
187      * @throws NullPointerException if type is null
188      */
189     public static boolean isUint16(final @NonNull TypeDefinition<?> type) {
190         return BaseUint16Type.INSTANCE.getPath().equals(type.getPath());
191     }
192
193     public static @NonNull Uint32TypeDefinition uint32Type() {
194         return BaseUint32Type.INSTANCE;
195     }
196
197     /**
198      * Check if a particular type is the base type for uint32.
199      *
200      * @param type The type to check
201      * @return If the type corresponds to the base uint32 type.
202      * @throws NullPointerException if type is null
203      */
204     public static boolean isUint32(final @NonNull TypeDefinition<?> type) {
205         return BaseUint32Type.INSTANCE.getPath().equals(type.getPath());
206     }
207
208     public static @NonNull Uint64TypeDefinition uint64Type() {
209         return BaseUint64Type.INSTANCE;
210     }
211
212     /**
213      * Check if a particular type is the base type for uint64.
214      *
215      * @param type The type to check
216      * @return If the type corresponds to the base uint64 type.
217      * @throws NullPointerException if type is null
218      */
219     public static boolean isUint64(final @NonNull TypeDefinition<?> type) {
220         return BaseUint64Type.INSTANCE.getPath().equals(type.getPath());
221     }
222
223     /**
224      * Return the base type of a particular type. This method performs recursive lookup through the type's base type
225      * until it finds the last element and returns it. If the argument is already the base type, it is returned as is.
226      *
227      * @param type Type for which to find the base type
228      * @return Base type of specified type
229      * @throws NullPointerException if type is null
230      */
231     public static @NonNull TypeDefinition<?> baseTypeOf(final @NonNull TypeDefinition<?> type) {
232         TypeDefinition<?> ret = type;
233         while (ret.getBaseType() != null) {
234             ret = ret.getBaseType();
235         }
236         return ret;
237     }
238 }