2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util.type;
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;
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.
33 * The following types have their base type fully specified and are exposed as appropriate TypeDefinition
39 * <li>int{8,16,32,64}</li>
41 * <li>uint{8,16,32,64}</li>
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:
49 * <li>instance-identifier</li>
50 * <li>enumeration</li>
51 * <li>identityref</li>
57 public final class BaseTypes {
62 public static @NonNull BinaryTypeDefinition binaryType() {
63 return BaseBinaryType.INSTANCE;
66 public static @NonNull BitsTypeBuilder bitsTypeBuilder(final SchemaPath path) {
67 return new BitsTypeBuilder(path);
70 public static @NonNull BooleanTypeDefinition booleanType() {
71 return BaseBooleanType.INSTANCE;
74 public static @NonNull DecimalTypeBuilder decimalTypeBuilder(final SchemaPath path) {
75 return new DecimalTypeBuilder(path);
78 public static @NonNull EmptyTypeDefinition emptyType() {
79 return BaseEmptyType.INSTANCE;
82 public static @NonNull EnumerationTypeBuilder enumerationTypeBuilder(final SchemaPath path) {
83 return new EnumerationTypeBuilder(path);
86 public static @NonNull IdentityrefTypeBuilder identityrefTypeBuilder(final SchemaPath path) {
87 return new IdentityrefTypeBuilder(path);
90 public static @NonNull InstanceIdentifierTypeDefinition instanceIdentifierType() {
91 return BaseInstanceIdentifierType.INSTANCE;
94 public static @NonNull Int8TypeDefinition int8Type() {
95 return BaseInt8Type.INSTANCE;
99 * Check if a particular type definition represents the built-in int8 type.
101 * @param type Type definition
102 * @return True if the definition is the built-in int8 type.
104 public static boolean isInt8(final TypeDefinition<?> type) {
105 return BaseInt8Type.INSTANCE.getPath().equals(type.getPath());
108 public static @NonNull Int16TypeDefinition int16Type() {
109 return BaseInt16Type.INSTANCE;
113 * Check if a particular type definition represents the built-in int16 type.
115 * @param type Type definition
116 * @return True if the definition is the built-in int16 type.
118 public static boolean isInt16(final TypeDefinition<?> type) {
119 return BaseInt16Type.INSTANCE.getPath().equals(type.getPath());
122 public static @NonNull Int32TypeDefinition int32Type() {
123 return BaseInt32Type.INSTANCE;
127 * Check if a particular type definition represents the built-in int32 type.
129 * @param type Type definition
130 * @return True if the definition is the built-in int32 type.
133 public static boolean isInt32(final TypeDefinition<?> type) {
134 return BaseInt32Type.INSTANCE.getPath().equals(type.getPath());
137 public static @NonNull Int64TypeDefinition int64Type() {
138 return BaseInt64Type.INSTANCE;
142 * Check if a particular type definition represents the built-in int64 type.
144 * @param type Type definition
145 * @return True if the definition is the built-in int64 type.
147 public static boolean isInt64(final TypeDefinition<?> type) {
148 return BaseInt64Type.INSTANCE.getPath().equals(type.getPath());
151 public static @NonNull LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) {
152 return new LeafrefTypeBuilder(path);
155 public static @NonNull StringTypeDefinition stringType() {
156 return BaseStringType.INSTANCE;
159 public static UnionTypeBuilder unionTypeBuilder(final SchemaPath path) {
160 return new UnionTypeBuilder(path);
163 public static @NonNull Uint8TypeDefinition uint8Type() {
164 return BaseUint8Type.INSTANCE;
168 * Check if a particular type is the base type for uint8.
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
174 public static boolean isUint8(final @NonNull TypeDefinition<?> type) {
175 return BaseUint8Type.INSTANCE.getPath().equals(type.getPath());
178 public static @NonNull Uint16TypeDefinition uint16Type() {
179 return BaseUint16Type.INSTANCE;
183 * Check if a particular type is the base type for uint16.
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
189 public static boolean isUint16(final @NonNull TypeDefinition<?> type) {
190 return BaseUint16Type.INSTANCE.getPath().equals(type.getPath());
193 public static @NonNull Uint32TypeDefinition uint32Type() {
194 return BaseUint32Type.INSTANCE;
198 * Check if a particular type is the base type for uint32.
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
204 public static boolean isUint32(final @NonNull TypeDefinition<?> type) {
205 return BaseUint32Type.INSTANCE.getPath().equals(type.getPath());
208 public static @NonNull Uint64TypeDefinition uint64Type() {
209 return BaseUint64Type.INSTANCE;
213 * Check if a particular type is the base type for uint64.
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
219 public static boolean isUint64(final @NonNull TypeDefinition<?> type) {
220 return BaseUint64Type.INSTANCE.getPath().equals(type.getPath());
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.
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
231 public static @NonNull TypeDefinition<?> baseTypeOf(final @NonNull TypeDefinition<?> type) {
232 TypeDefinition<?> ret = type;
233 while (ret.getBaseType() != null) {
234 ret = ret.getBaseType();