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