Bug 7603 Actions & RPCs
[mdsal.git] / binding2 / mdsal-binding2-generator-util / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / util / Types.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.mdsal.binding.javav2.generator.util;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.CharMatcher;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Splitter;
16 import com.google.common.cache.CacheBuilder;
17 import com.google.common.cache.CacheLoader;
18 import com.google.common.cache.LoadingCache;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Iterables;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.opendaylight.mdsal.binding.javav2.model.api.BaseTypeWithRestrictions;
28 import org.opendaylight.mdsal.binding.javav2.model.api.ConcreteType;
29 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
30 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
31 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
32 import org.opendaylight.mdsal.binding.javav2.model.api.WildcardType;
33 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
34 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
35 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
36 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
37 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
38 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
39
40 @Beta
41 public final class Types {
42     private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
43             new CacheLoader<Class<?>, ConcreteType>() {
44
45                 @Override
46                 public ConcreteType load(@Nonnull final Class<?> key) throws Exception {
47                     return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
48                 }
49             };
50
51     private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
52             CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
53
54     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
55     public static final ConcreteType CLASS = typeForClass(Class.class);
56     public static final ConcreteType STRING = typeForClass(String.class);
57     public static final ConcreteType VOID = typeForClass(Void.class);
58
59     public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
60     public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
61
62     private static final Splitter DOT_SPLITTER = Splitter.on('.');
63     private static final Type SET_TYPE = typeForClass(Set.class);
64     private static final Type LIST_TYPE = typeForClass(List.class);
65     private static final Type MAP_TYPE = typeForClass(Map.class);
66
67     private Types() {
68         throw new UnsupportedOperationException("Utility class");
69     }
70
71     /**
72      * Creates the instance of type
73      * {@link ConcreteType
74      * ConcreteType} which represents JAVA <code>void</code> type.
75      *
76      * @return <code>ConcreteType</code> instance which represents JAVA
77      *         <code>void</code>
78      */
79     public static ConcreteType voidType() {
80         return VOID;
81     }
82
83     /**
84      * Creates the instance of type
85      * {@link ConcreteType
86      * ConcreteType} which represents primitive JAVA type for which package
87      * doesn't exist.
88      *
89      * @param primitiveType
90      *            string containing programmatic construction based on
91      *            primitive type (e.g byte[])
92      * @return <code>ConcreteType</code> instance which represents programmatic
93      *         construction with primitive JAVA type
94      */
95     public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
96         return new ConcreteTypeImpl("", primitiveType, restrictions);
97     }
98
99     /**
100      * Returns an instance of {@link ConcreteType} describing the class
101      *
102      * @param cls
103      *            Class to describe
104      * @return Description of class
105      */
106     public static ConcreteType typeForClass(final Class<?> cls) {
107         return TYPE_CACHE.getUnchecked(cls);
108     }
109
110
111     public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
112         if (restrictions != null) {
113             if (restrictions instanceof DefaultRestrictions) {
114                 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
115             } else {
116                 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
117             }
118         } else {
119             return typeForClass(cls);
120         }
121     }
122
123     /**
124      * Returns an instance of {@link ParameterizedType} describing the typed
125      * {@link Map}&lt;K,V&gt;
126      *
127      * @param keyType
128      *            Key Type
129      * @param valueType
130      *            Value Type
131      * @return Description of generic type instance
132      */
133     public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
134         return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
135     }
136
137     /**
138      * Returns an instance of {@link ParameterizedType} describing the typed
139      * {@link Set}&lt;V&gt; with concrete type of value.
140      *
141      * @param valueType
142      *            Value Type
143      * @return Description of generic type instance of Set
144      */
145     public static ParameterizedType setTypeFor(final Type valueType) {
146         return parameterizedTypeFor(SET_TYPE, valueType);
147     }
148
149     /**
150      * Returns an instance of {@link ParameterizedType} describing the typed
151      * {@link List}&lt;V&gt; with concrete type of value.
152      *
153      * @param valueType
154      *            Value Type
155      * @return Description of type instance of List
156      */
157     public static ParameterizedType listTypeFor(final Type valueType) {
158         return parameterizedTypeFor(LIST_TYPE, valueType);
159     }
160
161     /**
162      * Creates instance of type
163      * {@link ParameterizedType
164      * ParameterizedType}
165      *
166      * @param type
167      *            JAVA <code>Type</code> for raw type
168      * @param parameters
169      *            JAVA <code>Type</code>s for actual parameter types
170      * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
171      *         and its parameters <code>parameters</code>
172      */
173     public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
174         return new ParameterizedTypeImpl(type, parameters);
175     }
176
177     /**
178      * Creates instance of type
179      * {@link WildcardType
180      * WildcardType}
181      *
182      * @param packageName
183      *            string with the package name
184      * @param typeName
185      *            string with the type name
186      * @return <code>WildcardType</code> representation of
187      *         <code>packageName</code> and <code>typeName</code>
188      */
189     public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
190         return new WildcardTypeImpl(packageName, typeName);
191     }
192
193     /**
194      * Creates instance of
195      * {@link ParameterizedType
196      * ParameterizedType} where raw type is
197      * {@link Augmentable} 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> representation of raw type
203      *         <code>Augmentable</code> with actual parameter
204      *         <code>valueType</code>
205      */
206     public static ParameterizedType augmentableTypeFor(final Type valueType) {
207         final Type augmentable = typeForClass(Augmentable.class);
208         return parameterizedTypeFor(augmentable, valueType);
209     }
210
211     /**
212      * Creates instance of
213      * {@link ParameterizedType
214      * ParameterizedType} where raw type is
215      * {@link Augmentation} and actual
216      * parameter is <code>valueType</code>.
217      *
218      * @param valueType
219      *            JAVA <code>Type</code> with actual parameter
220      * @return <code>ParametrizedType</code> reprezentation of raw type
221      *         <code>Augmentation</code> with actual parameter
222      *         <code>valueType</code>
223      */
224     public static ParameterizedType augmentationTypeFor(final Type valueType) {
225         final Type augmentation = typeForClass(Augmentation.class);
226         return parameterizedTypeFor(augmentation, valueType);
227     }
228
229     @Nullable
230     public static String getOuterClassName(final Type valueType) {
231         final String pkgName = valueType.getPackageName();
232         if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) {
233             // It is inner class.
234             return Iterables.getLast(DOT_SPLITTER.split(pkgName));
235         }
236         return null;
237     }
238
239     /**
240      *
241      * Represents concrete JAVA type.
242      *
243      */
244     private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
245
246         private final Restrictions restrictions;
247
248         /**
249          * Creates instance of this class with package <code>pkName</code> and
250          * with the type name <code>name</code>.
251          *
252          * @param pkName
253          *            string with package name
254          * @param name
255          *            string with the name of the type
256          */
257         private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
258             super(pkName, name);
259             this.restrictions = restrictions;
260         }
261
262         @Override
263         public Restrictions getRestrictions() {
264             return this.restrictions;
265         }
266     }
267
268     /**
269      *
270      * Represents concrete JAVA type with changed restriction values.
271      *
272      */
273     private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
274         private final Restrictions restrictions;
275
276         /**
277          * Creates instance of this class with package <code>pkName</code> and
278          * with the type name <code>name</code>.
279          *
280          * @param pkName
281          *            string with package name
282          * @param name
283          *            string with the name of the type
284          */
285         private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
286             super(pkName, name);
287             this.restrictions = Preconditions.checkNotNull(restrictions);
288         }
289
290         @Override
291         public Restrictions getRestrictions() {
292             return this.restrictions;
293         }
294     }
295
296     /**
297      *
298      * Represents parametrized JAVA type.
299      *
300      */
301     private static class ParameterizedTypeImpl extends AbstractBaseType implements ParameterizedType {
302         /**
303          * Array of JAVA actual type parameters.
304          */
305         private final Type[] actualTypes;
306
307         /**
308          * JAVA raw type (like List, Set, Map...)
309          */
310         private final Type rawType;
311
312         /**
313          * Creates instance of this class with concrete rawType and array of
314          * actual parameters.
315          *
316          * @param rawType
317          *            JAVA <code>Type</code> for raw type
318          * @param actTypes
319          *            array of actual parameters
320          */
321         public ParameterizedTypeImpl(final Type rawType, final Type[] actTypes) {
322             super(rawType.getPackageName(), rawType.getName(), true);
323             this.rawType = rawType;
324             this.actualTypes = actTypes.clone();
325         }
326
327         @Override
328         public Type[] getActualTypeArguments() {
329
330             return this.actualTypes;
331         }
332
333         @Override
334         public Type getRawType() {
335             return this.rawType;
336         }
337     }
338
339     /**
340      *
341      * Represents JAVA bounded wildcard type.
342      *
343      */
344     private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
345         /**
346          * Creates instance of this class with concrete package and type name.
347          *
348          * @param packageName
349          *            string with the package name
350          * @param typeName
351          *            string with the name of type
352          */
353         public WildcardTypeImpl(final String packageName, final String typeName) {
354             super(packageName, typeName);
355         }
356     }
357
358     public static <T extends Number> DefaultRestrictions<T> getDefaultRestrictions(final T min, final T max) {
359         return new DefaultRestrictions<>(min, max);
360     }
361
362     private static final class DefaultRestrictions<T extends Number> implements Restrictions {
363         private final List<RangeConstraint> rangeConstraints;
364
365         private DefaultRestrictions(final T min, final T max) {
366             Preconditions.checkNotNull(min);
367             Preconditions.checkNotNull(max);
368             this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
369                     .absent(), Optional.absent()));
370         }
371
372         @Override
373         public boolean isEmpty() {
374             return false;
375         }
376
377         @Override
378         public List<RangeConstraint> getRangeConstraints() {
379             return this.rangeConstraints;
380         }
381
382         @Override
383         public List<PatternConstraint> getPatternConstraints() {
384             return ImmutableList.of();
385         }
386
387         @Override
388         public List<LengthConstraint> getLengthConstraints() {
389             return ImmutableList.of();
390         }
391     }
392 }