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