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 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;
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 {
59 throw new UnsupportedOperationException();
62 public static BinaryTypeDefinition binaryType() {
63 return BaseBinaryType.INSTANCE;
66 public static BitsTypeBuilder bitsTypeBuilder(final SchemaPath path) {
67 return new BitsTypeBuilder(path);
70 public static BooleanTypeDefinition booleanType() {
71 return BaseBooleanType.INSTANCE;
74 public static DecimalTypeBuilder decimalTypeBuilder(final SchemaPath path) {
75 return new DecimalTypeBuilder(path);
78 public static EmptyTypeDefinition emptyType() {
79 return BaseEmptyType.INSTANCE;
82 public static EnumerationTypeBuilder enumerationTypeBuilder(final SchemaPath path) {
83 return new EnumerationTypeBuilder(path);
86 public static IdentityrefTypeBuilder identityrefTypeBuilder(final SchemaPath path) {
87 return new IdentityrefTypeBuilder(path);
90 public static InstanceIdentifierTypeDefinition instanceIdentifierType() {
91 return BaseInstanceIdentifierType.INSTANCE;
94 public static 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 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 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 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 LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) {
152 return new LeafrefTypeBuilder(path);
155 public static StringTypeDefinition stringType() {
156 return BaseStringType.INSTANCE;
159 public static UnionTypeBuilder unionTypeBuilder(final SchemaPath path) {
160 return new UnionTypeBuilder(path);
163 public static Uint8TypeDefinition uint8Type() {
164 return BaseUint8Type.INSTANCE;
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.
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
175 public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
176 return BaseUint8Type.INSTANCE.getPath().equals(type.getPath());
179 public static Uint16TypeDefinition uint16Type() {
180 return BaseUint16Type.INSTANCE;
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.
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
191 public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
192 return BaseUint16Type.INSTANCE.getPath().equals(type.getPath());
195 public static Uint32TypeDefinition uint32Type() {
196 return BaseUint32Type.INSTANCE;
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.
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
207 public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
208 return BaseUint32Type.INSTANCE.getPath().equals(type.getPath());
211 public static Uint64TypeDefinition uint64Type() {
212 return BaseUint64Type.INSTANCE;
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.
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
223 public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
224 return BaseUint64Type.INSTANCE.getPath().equals(type.getPath());
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.
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
235 public static TypeDefinition<?> baseTypeOf(@Nonnull final TypeDefinition<?> type) {
236 TypeDefinition<?> ret = type;
237 while (ret.getBaseType() != null) {
238 ret = ret.getBaseType();