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