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