Enable checkstyle in yang-model-util
[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 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;
21
22 /**
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.
25  *
26  * <p>
27  * The following types have their base type fully specified and are exposed as appropriate TypeDefinition
28  * sub-interfaces:
29  * <ul>
30  *     <li>boolean</li>
31  *     <li>empty</li>
32  *     <li>binary</li>
33  *     <li>int{8,16,32,64}</li>
34  *     <li>string</li>
35  *     <li>uint{8,16,32,64}</li>
36  * </ul>
37  *
38  * <p>
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:
41  * <ul>
42  *    <li>decimal64</li>
43  *    <li>instance-identifier</li>
44  *    <li>enumeration</li>
45  *    <li>identityref</li>
46  *    <li>leafref</li>
47  *    <li>union</li>
48  * </ul>
49  */
50 @Beta
51 public final class BaseTypes {
52     private BaseTypes() {
53         throw new UnsupportedOperationException();
54     }
55
56     public static BinaryTypeDefinition binaryType() {
57         return BaseBinaryType.INSTANCE;
58     }
59
60     public static BitsTypeBuilder bitsTypeBuilder(final SchemaPath path) {
61         return new BitsTypeBuilder(path);
62     }
63
64     public static BooleanTypeDefinition booleanType() {
65         return BaseBooleanType.INSTANCE;
66     }
67
68     public static DecimalTypeBuilder decimalTypeBuilder(final SchemaPath path) {
69         return new DecimalTypeBuilder(path);
70     }
71
72     public static EmptyTypeDefinition emptyType() {
73         return BaseEmptyType.INSTANCE;
74     }
75
76     public static EnumerationTypeBuilder enumerationTypeBuilder(final SchemaPath path) {
77         return new EnumerationTypeBuilder(path);
78     }
79
80     public static IdentityrefTypeBuilder identityrefTypeBuilder(final SchemaPath path) {
81         return new IdentityrefTypeBuilder(path);
82     }
83
84     public static InstanceIdentifierTypeDefinition instanceIdentifierType() {
85         return BaseInstanceIdentifierType.INSTANCE;
86     }
87
88     public static IntegerTypeDefinition int8Type() {
89         return BaseInt8Type.INSTANCE;
90     }
91
92     public static boolean isInt8(final TypeDefinition<?> type) {
93         return BaseInt8Type.INSTANCE.getPath().equals(type.getPath());
94     }
95
96     public static IntegerTypeDefinition int16Type() {
97         return BaseInt16Type.INSTANCE;
98     }
99
100     public static boolean isInt16(final TypeDefinition<?> type) {
101         return BaseInt16Type.INSTANCE.getPath().equals(type.getPath());
102     }
103
104     public static IntegerTypeDefinition int32Type() {
105         return BaseInt32Type.INSTANCE;
106     }
107
108     public static boolean isInt32(final TypeDefinition<?> type) {
109         return BaseInt32Type.INSTANCE.getPath().equals(type.getPath());
110     }
111
112     public static IntegerTypeDefinition int64Type() {
113         return BaseInt64Type.INSTANCE;
114     }
115
116     public static boolean isInt64(final TypeDefinition<?> type) {
117         return BaseInt64Type.INSTANCE.getPath().equals(type.getPath());
118     }
119
120     public static LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) {
121         return new LeafrefTypeBuilder(path);
122     }
123
124     public static StringTypeDefinition stringType() {
125         return BaseStringType.INSTANCE;
126     }
127
128     public static UnionTypeBuilder unionTypeBuilder(final SchemaPath path) {
129         return new UnionTypeBuilder(path);
130     }
131
132     public static UnsignedIntegerTypeDefinition uint8Type() {
133         return BaseUint8Type.INSTANCE;
134     }
135
136     /**
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.
139      *
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
143      */
144     public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
145         return BaseUint8Type.INSTANCE.getPath().equals(type.getPath());
146     }
147
148     public static UnsignedIntegerTypeDefinition uint16Type() {
149         return BaseUint16Type.INSTANCE;
150     }
151
152     /**
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.
155      *
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
159      */
160     public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
161         return BaseUint16Type.INSTANCE.getPath().equals(type.getPath());
162     }
163
164     public static UnsignedIntegerTypeDefinition uint32Type() {
165         return BaseUint32Type.INSTANCE;
166     }
167
168     /**
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.
171      *
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
175      */
176     public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
177         return BaseUint32Type.INSTANCE.getPath().equals(type.getPath());
178     }
179
180     public static UnsignedIntegerTypeDefinition uint64Type() {
181         return BaseUint64Type.INSTANCE;
182     }
183
184     /**
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.
187      *
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
191      */
192     public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
193         return BaseUint64Type.INSTANCE.getPath().equals(type.getPath());
194     }
195
196     /**
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.
199      *
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
203      */
204     public static TypeDefinition<?> baseTypeOf(@Nonnull final TypeDefinition<?> type) {
205         TypeDefinition<?> ret = type;
206         while (ret.getBaseType() != null) {
207             ret = ret.getBaseType();
208         }
209         return ret;
210     }
211 }