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.
26 * The following types have their base type fully specified and are exposed as appropriate TypeDefinition sub-interfaces:
31 * <li>int{8,16,32,64}</li>
33 * <li>uint{8,16,32,64}</li>
36 * The following types require additional specification in the model and are exposed by means of a specialized
37 * {@link TypeBuilder}s for each type:
40 * <li>instance-identifier</li>
41 * <li>enumeration</li>
42 * <li>identityref</li>
48 public final class BaseTypes {
50 throw new UnsupportedOperationException();
53 public static BinaryTypeDefinition binaryType() {
54 return BaseBinaryType.INSTANCE;
57 public static BitsTypeBuilder bitsTypeBuilder(final SchemaPath path) {
58 return new BitsTypeBuilder(path);
61 public static BooleanTypeDefinition booleanType() {
62 return BaseBooleanType.INSTANCE;
65 public static DecimalTypeBuilder decimalTypeBuilder(final SchemaPath path) {
66 return new DecimalTypeBuilder(path);
69 public static EmptyTypeDefinition emptyType() {
70 return BaseEmptyType.INSTANCE;
73 public static EnumerationTypeBuilder enumerationTypeBuilder(final SchemaPath path) {
74 return new EnumerationTypeBuilder(path);
77 public static IdentityrefTypeBuilder identityrefTypeBuilder(final SchemaPath path) {
78 return new IdentityrefTypeBuilder(path);
81 public static InstanceIdentifierTypeDefinition instanceIdentifierType() {
82 return BaseInstanceIdentifierType.INSTANCE;
85 public static IntegerTypeDefinition int8Type() {
86 return BaseInt8Type.INSTANCE;
89 public static boolean isInt8(final TypeDefinition<?> type) {
90 return BaseInt8Type.INSTANCE.getPath().equals(type.getPath());
93 public static IntegerTypeDefinition int16Type() {
94 return BaseInt16Type.INSTANCE;
97 public static boolean isInt16(final TypeDefinition<?> type) {
98 return BaseInt16Type.INSTANCE.getPath().equals(type.getPath());
101 public static IntegerTypeDefinition int32Type() {
102 return BaseInt32Type.INSTANCE;
105 public static boolean isInt32(final TypeDefinition<?> type) {
106 return BaseInt32Type.INSTANCE.getPath().equals(type.getPath());
109 public static IntegerTypeDefinition int64Type() {
110 return BaseInt64Type.INSTANCE;
113 public static boolean isInt64(final TypeDefinition<?> type) {
114 return BaseInt64Type.INSTANCE.getPath().equals(type.getPath());
117 public static LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) {
118 return new LeafrefTypeBuilder(path);
121 public static StringTypeDefinition stringType() {
122 return BaseStringType.INSTANCE;
125 public static UnionTypeBuilder unionTypeBuilder(final SchemaPath path) {
126 return new UnionTypeBuilder(path);
129 public static UnsignedIntegerTypeDefinition uint8Type() {
130 return BaseUint8Type.INSTANCE;
134 * Check if a particular type is the base type for uint8. Unlike {@link DerivedTypes#isUint8(TypeDefinition)},
135 * this method does not perform recursive base type lookup.
137 * @param type The type to check
138 * @return If the type corresponds to the base uint8 type.
139 * @throws NullPointerException if type is null
141 public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
142 return BaseUint8Type.INSTANCE.getPath().equals(type.getPath());
145 public static UnsignedIntegerTypeDefinition uint16Type() {
146 return BaseUint16Type.INSTANCE;
150 * Check if a particular type is the base type for uint16. Unlike {@link DerivedTypes#isUint16(TypeDefinition)},
151 * this method does not perform recursive base type lookup.
153 * @param type The type to check
154 * @return If the type corresponds to the base uint16 type.
155 * @throws NullPointerException if type is null
157 public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
158 return BaseUint16Type.INSTANCE.getPath().equals(type.getPath());
161 public static UnsignedIntegerTypeDefinition uint32Type() {
162 return BaseUint32Type.INSTANCE;
166 * Check if a particular type is the base type for uint32. Unlike {@link DerivedTypes#isUint32(TypeDefinition)},
167 * this method does not perform recursive base type lookup.
169 * @param type The type to check
170 * @return If the type corresponds to the base uint32 type.
171 * @throws NullPointerException if type is null
173 public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
174 return BaseUint32Type.INSTANCE.getPath().equals(type.getPath());
177 public static UnsignedIntegerTypeDefinition uint64Type() {
178 return BaseUint64Type.INSTANCE;
182 * Check if a particular type is the base type for uint64. Unlike {@link DerivedTypes#isUint64(TypeDefinition)},
183 * this method does not perform recursive base type lookup.
185 * @param type The type to check
186 * @return If the type corresponds to the base uint64 type.
187 * @throws NullPointerException if type is null
189 public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
190 return BaseUint64Type.INSTANCE.getPath().equals(type.getPath());
194 * Return the base type of a particular type. This method performs recursive lookup through the type's base type
195 * until it finds the last element and returns it. If the argument is already the base type, it is returned as is.
197 * @param type Type for which to find the base type
198 * @return Base type of specified type
199 * @throws NullPointerException if type is null
201 public static TypeDefinition<?> baseTypeOf(@Nonnull final TypeDefinition<?> type) {
202 TypeDefinition<?> ret = type;
203 while (ret.getBaseType() != null) {
204 ret = ret.getBaseType();