Populate data/ hierarchy
[yangtools.git] / yang / yang-model-ri / src / main / java / org / opendaylight / yangtools / yang / model / ri / 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.ri.type;
9
10 import com.google.common.annotations.Beta;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.common.QName;
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;
27
28 /**
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.
31  *
32  * <p>
33  * The following types have their base type fully specified and are exposed as appropriate TypeDefinition
34  * sub-interfaces:
35  * <ul>
36  *     <li>boolean</li>
37  *     <li>empty</li>
38  *     <li>binary</li>
39  *     <li>int{8,16,32,64}</li>
40  *     <li>string</li>
41  *     <li>uint{8,16,32,64}</li>
42  * </ul>
43  *
44  * <p>
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:
47  * <ul>
48  *    <li>decimal64</li>
49  *    <li>instance-identifier</li>
50  *    <li>enumeration</li>
51  *    <li>identityref</li>
52  *    <li>leafref</li>
53  *    <li>union</li>
54  * </ul>
55  */
56 @Beta
57 public final class BaseTypes {
58     private BaseTypes() {
59         // Hidden on purpose
60     }
61
62     public static @NonNull BinaryTypeDefinition binaryType() {
63         return BaseBinaryType.INSTANCE;
64     }
65
66     public static @NonNull BitsTypeBuilder bitsTypeBuilder(final QName qname) {
67         return new BitsTypeBuilder(qname);
68     }
69
70     public static @NonNull BooleanTypeDefinition booleanType() {
71         return BaseBooleanType.INSTANCE;
72     }
73
74     public static @NonNull DecimalTypeBuilder decimalTypeBuilder(final QName qname) {
75         return new DecimalTypeBuilder(qname);
76     }
77
78     public static @NonNull EmptyTypeDefinition emptyType() {
79         return BaseEmptyType.INSTANCE;
80     }
81
82     public static @NonNull EnumerationTypeBuilder enumerationTypeBuilder(final QName qname) {
83         return new EnumerationTypeBuilder(qname);
84     }
85
86     public static @NonNull IdentityrefTypeBuilder identityrefTypeBuilder(final QName qname) {
87         return new IdentityrefTypeBuilder(qname);
88     }
89
90     public static @NonNull InstanceIdentifierTypeDefinition instanceIdentifierType() {
91         return BaseInstanceIdentifierType.INSTANCE;
92     }
93
94     public static @NonNull Int8TypeDefinition int8Type() {
95         return BaseInt8Type.INSTANCE;
96     }
97
98     public static @NonNull Int16TypeDefinition int16Type() {
99         return BaseInt16Type.INSTANCE;
100     }
101
102     public static @NonNull Int32TypeDefinition int32Type() {
103         return BaseInt32Type.INSTANCE;
104     }
105
106     public static @NonNull Int64TypeDefinition int64Type() {
107         return BaseInt64Type.INSTANCE;
108     }
109
110     public static @NonNull LeafrefTypeBuilder leafrefTypeBuilder(final QName qname) {
111         return new LeafrefTypeBuilder(qname);
112     }
113
114     public static @NonNull StringTypeDefinition stringType() {
115         return BaseStringType.INSTANCE;
116     }
117
118     public static UnionTypeBuilder unionTypeBuilder(final QName qname) {
119         return new UnionTypeBuilder(qname);
120     }
121
122     public static @NonNull Uint8TypeDefinition uint8Type() {
123         return BaseUint8Type.INSTANCE;
124     }
125
126     public static @NonNull Uint16TypeDefinition uint16Type() {
127         return BaseUint16Type.INSTANCE;
128     }
129
130     public static @NonNull Uint32TypeDefinition uint32Type() {
131         return BaseUint32Type.INSTANCE;
132     }
133
134     public static @NonNull Uint64TypeDefinition uint64Type() {
135         return BaseUint64Type.INSTANCE;
136     }
137
138     /**
139      * Return the base type of a particular type. This method performs recursive lookup through the type's base type
140      * until it finds the last element and returns it. If the argument is already the base type, it is returned as is.
141      *
142      * @param type Type for which to find the base type
143      * @return Base type of specified type
144      * @throws NullPointerException if type is null
145      */
146     public static @NonNull TypeDefinition<?> baseTypeOf(final @NonNull TypeDefinition<?> type) {
147         TypeDefinition<?> ret = type;
148         while (ret.getBaseType() != null) {
149             ret = ret.getBaseType();
150         }
151         return ret;
152     }
153 }