Implemented typedef units statement in generated code.
[yangtools.git] / code-generator / binding-generator-util / src / main / java / org / opendaylight / yangtools / binding / generator / util / Types.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.binding.generator.util;
9
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13 import java.util.concurrent.Future;
14
15 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
16 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
17 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
18 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
19 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
20 import org.opendaylight.yangtools.sal.binding.model.api.Type;
21 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
22 import org.opendaylight.yangtools.yang.binding.Augmentable;
23 import org.opendaylight.yangtools.yang.binding.Augmentation;
24 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
25
26 public final class Types {
27     private static final Type SET_TYPE = typeForClass(Set.class);
28     private static final Type LIST_TYPE = typeForClass(List.class);
29     private static final Type MAP_TYPE = typeForClass(Map.class);
30
31     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
32     public static final ConcreteType FUTURE = typeForClass(Future.class);
33     public static final ConcreteType STRING = typeForClass(String.class);
34     public static final ConcreteType VOID = typeForClass(Void.class);
35
36     /**
37      * It is not desirable to create instance of this class
38      */
39     private Types() {
40     }
41
42     /**
43      * Creates the instance of type
44      * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
45      * ConcreteType} which represents JAVA <code>void</code> type.
46      *
47      * @return <code>ConcreteType</code> instance which represents JAVA
48      *         <code>void</code>
49      */
50     public static ConcreteType voidType() {
51         return VOID;
52     }
53
54     /**
55      * Creates the instance of type
56      * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
57      * ConcreteType} which represents primitive JAVA type for which package
58      * doesn't exist.
59      *
60      * @param primitiveType
61      *            string containing programaticall construction based on
62      *            primitive type (e.g byte[])
63      * @return <code>ConcreteType</code> instance which represents programatic
64      *         construction with primitive JAVA type
65      */
66     public static Type primitiveType(final String primitiveType, final Restrictions restrictions) {
67         return new ConcreteTypeImpl("", primitiveType, restrictions);
68     }
69
70     /**
71      * Returns an instance of {@link ConcreteType} describing the class
72      *
73      * @param cls
74      *            Class to describe
75      * @return Description of class
76      */
77     public static ConcreteType typeForClass(Class<?> cls) {
78         return typeForClass(cls, null);
79     }
80
81     public static ConcreteType typeForClass(Class<?> cls, Restrictions restrictions) {
82         return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
83     }
84
85     /**
86      * Returns an instance of {@link ParameterizedType} describing the typed
87      * {@link Map}<K,V>
88      *
89      * @param keyType
90      *            Key Type
91      * @param valueType
92      *            Value Type
93      * @return Description of generic type instance
94      */
95     public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {
96         return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
97     }
98
99     /**
100      * Returns an instance of {@link ParameterizedType} describing the typed
101      * {@link Set}<V> with concrete type of value.
102      *
103      * @param valueType
104      *            Value Type
105      * @return Description of generic type instance of Set
106      */
107     public static ParameterizedType setTypeFor(Type valueType) {
108         return parameterizedTypeFor(SET_TYPE, valueType);
109     }
110
111     /**
112      * Returns an instance of {@link ParameterizedType} describing the typed
113      * {@link List}<V> with concrete type of value.
114      *
115      * @param valueType
116      *            Value Type
117      * @return Description of type instance of List
118      */
119     public static ParameterizedType listTypeFor(Type valueType) {
120         return parameterizedTypeFor(LIST_TYPE, valueType);
121     }
122
123     /**
124      * Creates generated transfer object for
125      * {@link org.opendaylight.yangtools.yang.binding.BaseIdentity BaseIdentity}
126      *
127      * @return generated transfer object which is used as extension when YANG
128      *         <code>identity</code> is mapped to generated TO
129      */
130     public static GeneratedTransferObject getBaseIdentityTO() {
131         Class<BaseIdentity> cls = BaseIdentity.class;
132         GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(cls.getPackage().getName(), cls.getSimpleName());
133         return gto.toInstance();
134     }
135
136     /**
137      * Creates instance of type
138      * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
139      * ParameterizedType}
140      *
141      * @param type
142      *            JAVA <code>Type</code> for raw type
143      * @param parameters
144      *            JAVA <code>Type</code>s for actual parameter types
145      * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
146      *         and its parameters <code>parameters</code>
147      */
148     public static ParameterizedType parameterizedTypeFor(Type type, Type... parameters) {
149         return new ParametrizedTypeImpl(type, parameters);
150     }
151
152     /**
153      * Creates instance of type
154      * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
155      * WildcardType}
156      *
157      * @param packageName
158      *            string with the package name
159      * @param typeName
160      *            string with the type name
161      * @return <code>WildcardType</code> reprezentation of
162      *         <code>packageName</code> and <code>typeName</code>
163      */
164     public static WildcardType wildcardTypeFor(String packageName, String typeName) {
165         return new WildcardTypeImpl(packageName, typeName);
166     }
167
168     /**
169      * Creates instance of
170      * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
171      * ParameterizedType} where raw type is
172      * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
173      * parameter is <code>valueType</code>.
174      *
175      * @param valueType
176      *            JAVA <code>Type</code> with actual parameter
177      * @return <code>ParametrizedType</code> reprezentation of raw type
178      *         <code>Augmentable</code> with actual parameter
179      *         <code>valueType</code>
180      */
181     public static ParameterizedType augmentableTypeFor(Type valueType) {
182         final Type augmentable = typeForClass(Augmentable.class);
183         return parameterizedTypeFor(augmentable, valueType);
184     }
185
186     /**
187      * Creates instance of
188      * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
189      * ParameterizedType} where raw type is
190      * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
191      * parameter is <code>valueType</code>.
192      *
193      * @param valueType
194      *            JAVA <code>Type</code> with actual parameter
195      * @return <code>ParametrizedType</code> reprezentation of raw type
196      *         <code>Augmentation</code> with actual parameter
197      *         <code>valueType</code>
198      */
199     public static ParameterizedType augmentationTypeFor(Type valueType) {
200         final Type augmentation = typeForClass(Augmentation.class);
201         return parameterizedTypeFor(augmentation, valueType);
202     }
203
204     /**
205      *
206      * Represents concrete JAVA type.
207      *
208      */
209     private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
210         private final Restrictions restrictions;
211
212         /**
213          * Creates instance of this class with package <code>pkName</code> and
214          * with the type name <code>name</code>.
215          *
216          * @param pkName
217          *            string with package name
218          * @param name
219          *            string with the name of the type
220          */
221         private ConcreteTypeImpl(String pkName, String name, Restrictions restrictions) {
222             super(pkName, name);
223             this.restrictions = restrictions;
224         }
225
226         @Override
227         public Restrictions getRestrictions() {
228             return restrictions;
229         }
230     }
231
232     /**
233      *
234      * Represents parametrized JAVA type.
235      *
236      */
237     private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
238         /**
239          * Array of JAVA actual type parameters.
240          */
241         private Type[] actualTypes;
242
243         /**
244          * JAVA raw type (like List, Set, Map...)
245          */
246         private Type rawType;
247
248         @Override
249         public Type[] getActualTypeArguments() {
250
251             return actualTypes;
252         }
253
254         @Override
255         public Type getRawType() {
256             return rawType;
257         }
258
259         /**
260          * Creates instance of this class with concrete rawType and array of
261          * actual parameters.
262          *
263          * @param rawType
264          *            JAVA <code>Type</code> for raw type
265          * @param actTypes
266          *            array of actual parameters
267          */
268         public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {
269             super(rawType.getPackageName(), rawType.getName());
270             this.rawType = rawType;
271             this.actualTypes = actTypes;
272         }
273
274     }
275
276     /**
277      *
278      * Represents JAVA bounded wildcard type.
279      *
280      */
281     private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
282         /**
283          * Creates instance of this class with concrete package and type name.
284          *
285          * @param packageName
286          *            string with the package name
287          * @param typeName
288          *            string with the name of type
289          */
290         public WildcardTypeImpl(String packageName, String typeName) {
291             super(packageName, typeName);
292         }
293     }
294
295 }