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