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.IntegerTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
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.
27 * The following types have their base type fully specified and are exposed as appropriate TypeDefinition
33 * <li>int{8,16,32,64}</li>
35 * <li>uint{8,16,32,64}</li>
39 * The following types require additional specification in the model and are exposed by means of a specialized
40 * {@link TypeBuilder}s for each type:
43 * <li>instance-identifier</li>
44 * <li>enumeration</li>
45 * <li>identityref</li>
51 public final class BaseTypes {
53 throw new UnsupportedOperationException();
56 public static BinaryTypeDefinition binaryType() {
57 return BaseBinaryType.INSTANCE;
60 public static BitsTypeBuilder bitsTypeBuilder(final SchemaPath path) {
61 return new BitsTypeBuilder(path);
64 public static BooleanTypeDefinition booleanType() {
65 return BaseBooleanType.INSTANCE;
68 public static DecimalTypeBuilder decimalTypeBuilder(final SchemaPath path) {
69 return new DecimalTypeBuilder(path);
72 public static EmptyTypeDefinition emptyType() {
73 return BaseEmptyType.INSTANCE;
76 public static EnumerationTypeBuilder enumerationTypeBuilder(final SchemaPath path) {
77 return new EnumerationTypeBuilder(path);
80 public static IdentityrefTypeBuilder identityrefTypeBuilder(final SchemaPath path) {
81 return new IdentityrefTypeBuilder(path);
84 public static InstanceIdentifierTypeDefinition instanceIdentifierType() {
85 return BaseInstanceIdentifierType.INSTANCE;
88 public static IntegerTypeDefinition int8Type() {
89 return BaseInt8Type.INSTANCE;
92 public static boolean isInt8(final TypeDefinition<?> type) {
93 return BaseInt8Type.INSTANCE.getPath().equals(type.getPath());
96 public static IntegerTypeDefinition int16Type() {
97 return BaseInt16Type.INSTANCE;
100 public static boolean isInt16(final TypeDefinition<?> type) {
101 return BaseInt16Type.INSTANCE.getPath().equals(type.getPath());
104 public static IntegerTypeDefinition int32Type() {
105 return BaseInt32Type.INSTANCE;
108 public static boolean isInt32(final TypeDefinition<?> type) {
109 return BaseInt32Type.INSTANCE.getPath().equals(type.getPath());
112 public static IntegerTypeDefinition int64Type() {
113 return BaseInt64Type.INSTANCE;
116 public static boolean isInt64(final TypeDefinition<?> type) {
117 return BaseInt64Type.INSTANCE.getPath().equals(type.getPath());
120 public static LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) {
121 return new LeafrefTypeBuilder(path);
124 public static StringTypeDefinition stringType() {
125 return BaseStringType.INSTANCE;
128 public static UnionTypeBuilder unionTypeBuilder(final SchemaPath path) {
129 return new UnionTypeBuilder(path);
132 public static UnsignedIntegerTypeDefinition uint8Type() {
133 return BaseUint8Type.INSTANCE;
137 * Check if a particular type is the base type for uint8. Unlike {@link DerivedTypes#isUint8(TypeDefinition)},
138 * this method does not perform recursive base type lookup.
140 * @param type The type to check
141 * @return If the type corresponds to the base uint8 type.
142 * @throws NullPointerException if type is null
144 public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
145 return BaseUint8Type.INSTANCE.getPath().equals(type.getPath());
148 public static UnsignedIntegerTypeDefinition uint16Type() {
149 return BaseUint16Type.INSTANCE;
153 * Check if a particular type is the base type for uint16. Unlike {@link DerivedTypes#isUint16(TypeDefinition)},
154 * this method does not perform recursive base type lookup.
156 * @param type The type to check
157 * @return If the type corresponds to the base uint16 type.
158 * @throws NullPointerException if type is null
160 public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
161 return BaseUint16Type.INSTANCE.getPath().equals(type.getPath());
164 public static UnsignedIntegerTypeDefinition uint32Type() {
165 return BaseUint32Type.INSTANCE;
169 * Check if a particular type is the base type for uint32. Unlike {@link DerivedTypes#isUint32(TypeDefinition)},
170 * this method does not perform recursive base type lookup.
172 * @param type The type to check
173 * @return If the type corresponds to the base uint32 type.
174 * @throws NullPointerException if type is null
176 public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
177 return BaseUint32Type.INSTANCE.getPath().equals(type.getPath());
180 public static UnsignedIntegerTypeDefinition uint64Type() {
181 return BaseUint64Type.INSTANCE;
185 * Check if a particular type is the base type for uint64. Unlike {@link DerivedTypes#isUint64(TypeDefinition)},
186 * this method does not perform recursive base type lookup.
188 * @param type The type to check
189 * @return If the type corresponds to the base uint64 type.
190 * @throws NullPointerException if type is null
192 public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
193 return BaseUint64Type.INSTANCE.getPath().equals(type.getPath());
197 * Return the base type of a particular type. This method performs recursive lookup through the type's base type
198 * until it finds the last element and returns it. If the argument is already the base type, it is returned as is.
200 * @param type Type for which to find the base type
201 * @return Base type of specified type
202 * @throws NullPointerException if type is null
204 public static TypeDefinition<?> baseTypeOf(@Nonnull final TypeDefinition<?> type) {
205 TypeDefinition<?> ret = type;
206 while (ret.getBaseType() != null) {
207 ret = ret.getBaseType();