Bug 706: - Missing support for anyxml statement in java generator and mapping service
[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 import org.w3c.dom.Document;
40
41 @Beta
42 public final class Types {
43     private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
44             new CacheLoader<Class<?>, ConcreteType>() {
45
46                 @Override
47                 public ConcreteType load(@Nonnull final Class<?> key) throws Exception {
48                     return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
49                 }
50             };
51
52     private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
53             CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
54
55     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
56     public static final ConcreteType CLASS = typeForClass(Class.class);
57     public static final ConcreteType STRING = typeForClass(String.class);
58     public static final ConcreteType VOID = typeForClass(Void.class);
59     public static final ConcreteType DOCUMENT = typeForClass(Document.class);
60
61     public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
62     public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
63
64     private static final Splitter DOT_SPLITTER = Splitter.on('.');
65     private static final Type SET_TYPE = typeForClass(Set.class);
66     private static final Type LIST_TYPE = typeForClass(List.class);
67     private static final Type MAP_TYPE = typeForClass(Map.class);
68
69     private Types() {
70         throw new UnsupportedOperationException("Utility class");
71     }
72
73     /**
74      * Creates the instance of type
75      * {@link ConcreteType
76      * ConcreteType} which represents JAVA <code>void</code> type.
77      *
78      * @return <code>ConcreteType</code> instance which represents JAVA
79      *         <code>void</code>
80      */
81     public static ConcreteType voidType() {
82         return VOID;
83     }
84
85     /**
86      * Creates the instance of type
87      * {@link ConcreteType
88      * ConcreteType} which represents primitive JAVA type for which package
89      * doesn't exist.
90      *
91      * @param primitiveType
92      *            string containing programmatic construction based on
93      *            primitive type (e.g byte[])
94      * @return <code>ConcreteType</code> instance which represents programmatic
95      *         construction with primitive JAVA type
96      */
97     public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
98         return new ConcreteTypeImpl("", primitiveType, restrictions);
99     }
100
101     /**
102      * Returns an instance of {@link ConcreteType} describing the class
103      *
104      * @param cls
105      *            Class to describe
106      * @return Description of class
107      */
108     public static ConcreteType typeForClass(final Class<?> cls) {
109         return TYPE_CACHE.getUnchecked(cls);
110     }
111
112
113     public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
114         if (restrictions != null) {
115             if (restrictions instanceof DefaultRestrictions) {
116                 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
117             } else {
118                 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
119             }
120         } else {
121             return typeForClass(cls);
122         }
123     }
124
125     /**
126      * Returns an instance of {@link ParameterizedType} describing the typed
127      * {@link Map}&lt;K,V&gt;
128      *
129      * @param keyType
130      *            Key Type
131      * @param valueType
132      *            Value Type
133      * @return Description of generic type instance
134      */
135     public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
136         return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
137     }
138
139     /**
140      * Returns an instance of {@link ParameterizedType} describing the typed
141      * {@link Set}&lt;V&gt; with concrete type of value.
142      *
143      * @param valueType
144      *            Value Type
145      * @return Description of generic type instance of Set
146      */
147     public static ParameterizedType setTypeFor(final Type valueType) {
148         return parameterizedTypeFor(SET_TYPE, valueType);
149     }
150
151     /**
152      * Returns an instance of {@link ParameterizedType} describing the typed
153      * {@link List}&lt;V&gt; with concrete type of value.
154      *
155      * @param valueType
156      *            Value Type
157      * @return Description of type instance of List
158      */
159     public static ParameterizedType listTypeFor(final Type valueType) {
160         return parameterizedTypeFor(LIST_TYPE, valueType);
161     }
162
163     /**
164      * Creates instance of type
165      * {@link ParameterizedType
166      * ParameterizedType}
167      *
168      * @param type
169      *            JAVA <code>Type</code> for raw type
170      * @param parameters
171      *            JAVA <code>Type</code>s for actual parameter types
172      * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
173      *         and its parameters <code>parameters</code>
174      */
175     public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
176         return new ParameterizedTypeImpl(type, parameters);
177     }
178
179     /**
180      * Creates instance of type
181      * {@link WildcardType
182      * WildcardType}
183      *
184      * @param packageName
185      *            string with the package name
186      * @param typeName
187      *            string with the type name
188      * @return <code>WildcardType</code> representation of
189      *         <code>packageName</code> and <code>typeName</code>
190      */
191     public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
192         return new WildcardTypeImpl(packageName, typeName);
193     }
194
195     /**
196      * Creates instance of
197      * {@link ParameterizedType
198      * ParameterizedType} where raw type is
199      * {@link Augmentable} and actual
200      * parameter is <code>valueType</code>.
201      *
202      * @param valueType
203      *            JAVA <code>Type</code> with actual parameter
204      * @return <code>ParametrizedType</code> representation of raw type
205      *         <code>Augmentable</code> with actual parameter
206      *         <code>valueType</code>
207      */
208     public static ParameterizedType augmentableTypeFor(final Type valueType) {
209         final Type augmentable = typeForClass(Augmentable.class);
210         return parameterizedTypeFor(augmentable, valueType);
211     }
212
213     /**
214      * Creates instance of
215      * {@link ParameterizedType
216      * ParameterizedType} where raw type is
217      * {@link Augmentation} and actual
218      * parameter is <code>valueType</code>.
219      *
220      * @param valueType
221      *            JAVA <code>Type</code> with actual parameter
222      * @return <code>ParametrizedType</code> reprezentation of raw type
223      *         <code>Augmentation</code> with actual parameter
224      *         <code>valueType</code>
225      */
226     public static ParameterizedType augmentationTypeFor(final Type valueType) {
227         final Type augmentation = typeForClass(Augmentation.class);
228         return parameterizedTypeFor(augmentation, valueType);
229     }
230
231     @Nullable
232     public static 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 this.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 this.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         /**
315          * Creates instance of this class with concrete rawType and array of
316          * actual parameters.
317          *
318          * @param rawType
319          *            JAVA <code>Type</code> for raw type
320          * @param actTypes
321          *            array of actual parameters
322          */
323         public ParameterizedTypeImpl(final Type rawType, final Type[] actTypes) {
324             super(rawType.getPackageName(), rawType.getName(), true);
325             this.rawType = rawType;
326             this.actualTypes = actTypes.clone();
327         }
328
329         @Override
330         public Type[] getActualTypeArguments() {
331
332             return this.actualTypes;
333         }
334
335         @Override
336         public Type getRawType() {
337             return this.rawType;
338         }
339     }
340
341     /**
342      *
343      * Represents JAVA bounded wildcard type.
344      *
345      */
346     private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
347         /**
348          * Creates instance of this class with concrete package and type name.
349          *
350          * @param packageName
351          *            string with the package name
352          * @param typeName
353          *            string with the name of type
354          */
355         public WildcardTypeImpl(final String packageName, final String typeName) {
356             super(packageName, typeName);
357         }
358     }
359
360     public static <T extends Number> DefaultRestrictions<T> getDefaultRestrictions(final T min, final T max) {
361         return new DefaultRestrictions<>(min, max);
362     }
363
364     private static final class DefaultRestrictions<T extends Number> implements Restrictions {
365         private final List<RangeConstraint> rangeConstraints;
366
367         private DefaultRestrictions(final T min, final T max) {
368             Preconditions.checkNotNull(min);
369             Preconditions.checkNotNull(max);
370             this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
371                     .absent(), Optional.absent()));
372         }
373
374         @Override
375         public boolean isEmpty() {
376             return false;
377         }
378
379         @Override
380         public List<RangeConstraint> getRangeConstraints() {
381             return this.rangeConstraints;
382         }
383
384         @Override
385         public List<PatternConstraint> getPatternConstraints() {
386             return ImmutableList.of();
387         }
388
389         @Override
390         public List<LengthConstraint> getLengthConstraints() {
391             return ImmutableList.of();
392         }
393     }
394 }