Bug 6859: Cleanup package names for mdsal-binding-generator-util module
[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 /**
38  * @deprecated Use {@link org.opendaylight.mdsal.binding.generator.util.Types} instead.
39  */
40 @Deprecated
41 public final class Types {
42     private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
43             new CacheLoader<Class<?>, ConcreteType>() {
44                 @Override
45                 public ConcreteType load(final Class<?> key) {
46                     return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
47                 }
48     };
49     private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
50             CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
51
52     private static final Type SET_TYPE = typeForClass(Set.class);
53     private static final Type LIST_TYPE = typeForClass(List.class);
54     private static final Type MAP_TYPE = typeForClass(Map.class);
55
56     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
57     public static final ConcreteType FUTURE = typeForClass(Future.class);
58     public static final ConcreteType STRING = typeForClass(String.class);
59     public static final ConcreteType VOID = typeForClass(Void.class);
60     public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
61     public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
62     private static final Splitter DOT_SPLITTER = Splitter.on('.');
63
64     /**
65      * It is not desirable to create instance of this class
66      */
67     private Types() {
68     }
69
70     /**
71      * Creates the instance of type
72      * {@link org.opendaylight.yangtools.sal.binding.model.api.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 org.opendaylight.yangtools.sal.binding.model.api.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      * @param restrictions
92      *            restrictions object
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     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 org.opendaylight.yangtools.sal.binding.model.api.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 ParametrizedTypeImpl(type, parameters);
175     }
176
177     /**
178      * Creates instance of type
179      * {@link org.opendaylight.yangtools.sal.binding.model.api.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 org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
196      * ParameterizedType} where raw type is
197      * {@link org.opendaylight.yangtools.yang.binding.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 org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
214      * ParameterizedType} where raw type is
215      * {@link org.opendaylight.yangtools.yang.binding.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
230     public static  @Nullable 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         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 ParametrizedTypeImpl 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         @Override
312         public Type[] getActualTypeArguments() {
313
314             return actualTypes;
315         }
316
317         @Override
318         public Type getRawType() {
319             return rawType;
320         }
321
322         /**
323          * Creates instance of this class with concrete rawType and array of
324          * actual parameters.
325          *
326          * @param rawType
327          *            JAVA <code>Type</code> for raw type
328          * @param actTypes
329          *            array of actual parameters
330          */
331         public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
332             super(rawType.getPackageName(), rawType.getName());
333             this.rawType = rawType;
334             this.actualTypes = actTypes.clone();
335         }
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 T min;
364         private final T max;
365         private final List<RangeConstraint> rangeConstraints;
366
367         private DefaultRestrictions(final T min, final T max) {
368             this.min = Preconditions.checkNotNull(min);
369             this.max = Preconditions.checkNotNull(max);
370             this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
371                     .<String>absent(), Optional.<String>absent()));
372         }
373
374         @Override
375         public boolean isEmpty() {
376             return false;
377         }
378
379         @Override
380         public List<RangeConstraint> getRangeConstraints() {
381             return rangeConstraints;
382         }
383
384         @Override
385         public List<PatternConstraint> getPatternConstraints() {
386             return Collections.emptyList();
387         }
388
389         @Override
390         public List<LengthConstraint> getLengthConstraints() {
391             return Collections.emptyList();
392         }
393     }
394 }