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