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