Fix add imports of nested classes
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / mdsal / binding / model / 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.mdsal.binding.model.util;
9
10 import com.google.common.base.CharMatcher;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Splitter;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.cache.CacheLoader;
15 import com.google.common.cache.LoadingCache;
16 import com.google.common.collect.ImmutableRangeSet;
17 import com.google.common.collect.Iterables;
18 import com.google.common.collect.Range;
19 import com.google.common.collect.RangeSet;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.Set;
25 import java.util.concurrent.Future;
26 import javax.annotation.Nullable;
27 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
28 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
29 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
30 import org.opendaylight.mdsal.binding.model.api.Restrictions;
31 import org.opendaylight.mdsal.binding.model.api.Type;
32 import org.opendaylight.mdsal.binding.model.api.WildcardType;
33 import org.opendaylight.yangtools.yang.binding.Augmentable;
34 import org.opendaylight.yangtools.yang.binding.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
39 public final class Types {
40     private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
41             new CacheLoader<Class<?>, ConcreteType>() {
42                 @Override
43                 public ConcreteType load(final Class<?> key) {
44                     //Nested class
45                     if (key.getEnclosingClass() != null) {
46                         return new ConcreteTypeImpl(key.getEnclosingClass().getName(), key.getSimpleName(), null);
47                     } else {
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 Type SET_TYPE = typeForClass(Set.class);
56     public static final Type LIST_TYPE = typeForClass(List.class);
57     public static final Type MAP_TYPE = typeForClass(Map.class);
58
59     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
60     public static final ConcreteType FUTURE = typeForClass(Future.class);
61     public static final ConcreteType STRING = typeForClass(String.class);
62     public static final ConcreteType VOID = typeForClass(Void.class);
63     public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
64     public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
65     private static final Splitter DOT_SPLITTER = Splitter.on('.');
66
67     /**
68      * It is not desirable to create instance of this class
69      */
70     private Types() {
71     }
72
73     /**
74      * Creates the instance of type
75      * {@link org.opendaylight.mdsal.binding.model.api.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 org.opendaylight.mdsal.binding.model.api.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      * @param restrictions
95      *            restrictions object
96      * @return <code>ConcreteType</code> instance which represents programmatic
97      *         construction with primitive JAVA type
98      */
99     public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
100         return new ConcreteTypeImpl("", primitiveType, restrictions);
101     }
102
103     /**
104      * Returns an instance of {@link ConcreteType} describing the class
105      *
106      * @param cls
107      *            Class to describe
108      * @return Description of class
109      */
110     public static ConcreteType typeForClass(final Class<?> cls) {
111         return TYPE_CACHE.getUnchecked(cls);
112     }
113
114     public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
115         if (restrictions == null) {
116             return typeForClass(cls);
117         }
118         if (restrictions instanceof DefaultRestrictions) {
119             return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
120         }
121         return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
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.binding.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 ParametrizedTypeImpl(type, parameters);
176     }
177
178     /**
179      * Creates instance of type
180      * {@link org.opendaylight.mdsal.binding.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.binding.model.api.ParameterizedType
197      * ParameterizedType} where raw type is
198      * {@link org.opendaylight.yangtools.yang.binding.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.binding.model.api.ParameterizedType
215      * ParameterizedType} where raw type is
216      * {@link org.opendaylight.yangtools.yang.binding.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 String getOuterClassName(final Type valueType) {
232         final String pkgName = valueType.getPackageName();
233         if (CharMatcher.javaUpperCase().indexIn(pkgName) >= 0) {
234             // It is inner class.
235             return Iterables.getLast(DOT_SPLITTER.split(pkgName));
236         }
237         return null;
238     }
239
240     /**
241      *
242      * Represents concrete JAVA type.
243      *
244      */
245     private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
246         private final Restrictions restrictions;
247
248         /**
249          * Creates instance of this class with package <code>pkName</code> and
250          * with the type name <code>name</code>.
251          *
252          * @param pkName
253          *            string with package name
254          * @param name
255          *            string with the name of the type
256          */
257         private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
258             super(pkName, name);
259             this.restrictions = restrictions;
260         }
261
262         @Override
263         public Restrictions getRestrictions() {
264             return this.restrictions;
265         }
266     }
267
268     /**
269      * Represents concrete JAVA type with changed restriction values.
270      */
271     private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
272         private final Restrictions restrictions;
273
274         /**
275          * Creates instance of this class with package <code>pkName</code> and
276          * with the type name <code>name</code>.
277          *
278          * @param pkName
279          *            string with package name
280          * @param name
281          *            string with the name of the type
282          */
283         private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
284             super(pkName, name);
285             this.restrictions = Preconditions.checkNotNull(restrictions);
286         }
287
288         @Override
289         public Restrictions getRestrictions() {
290             return this.restrictions;
291         }
292     }
293
294     /**
295      * Represents parametrized JAVA type.
296      */
297     private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
298         /**
299          * Array of JAVA actual type parameters.
300          */
301         private final Type[] actualTypes;
302
303         /**
304          * JAVA raw type (like List, Set, Map...)
305          */
306         private final Type rawType;
307
308         @Override
309         public Type[] getActualTypeArguments() {
310
311             return this.actualTypes;
312         }
313
314         @Override
315         public Type getRawType() {
316             return this.rawType;
317         }
318
319         /**
320          * Creates instance of this class with concrete rawType and array of
321          * actual parameters.
322          *
323          * @param rawType
324          *            JAVA <code>Type</code> for raw type
325          * @param actTypes
326          *            array of actual parameters
327          */
328         public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
329             super(rawType.getPackageName(), rawType.getName());
330             this.rawType = rawType;
331             this.actualTypes = actTypes.clone();
332         }
333
334     }
335
336     /**
337      * Represents JAVA bounded wildcard type.
338      */
339     private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
340         /**
341          * Creates instance of this class with concrete package and type name.
342          *
343          * @param packageName
344          *            string with the package name
345          * @param typeName
346          *            string with the name of type
347          */
348         public WildcardTypeImpl(final String packageName, final String typeName) {
349             super(packageName, typeName);
350         }
351     }
352
353     public static <T extends Number& Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
354             final T max) {
355         return new DefaultRestrictions<>(min, max);
356     }
357
358     private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
359         private final T min;
360         private final T max;
361         private final RangeConstraint<?> rangeConstraint;
362
363         private DefaultRestrictions(final T min, final T max) {
364             this.min = Preconditions.checkNotNull(min);
365             this.max = Preconditions.checkNotNull(max);
366
367             this.rangeConstraint = new RangeConstraint<T>() {
368
369                 @Override
370                 public Optional<String> getErrorAppTag() {
371                     return Optional.empty();
372                 }
373
374                 @Override
375                 public Optional<String> getErrorMessage() {
376                     return Optional.empty();
377                 }
378
379                 @Override
380                 public Optional<String> getDescription() {
381                     return Optional.empty();
382                 }
383
384                 @Override
385                 public Optional<String> getReference() {
386                     return Optional.empty();
387                 }
388
389                 @Override
390                 public RangeSet<T> getAllowedRanges() {
391                     return ImmutableRangeSet.of(Range.closed(min, max));
392                 }
393             };
394         }
395
396         @Override
397         public boolean isEmpty() {
398             return false;
399         }
400
401         @Override
402         public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
403             return Optional.of(rangeConstraint);
404         }
405
406         @Override
407         public List<PatternConstraint> getPatternConstraints() {
408             return Collections.emptyList();
409         }
410
411         @Override
412         public Optional<LengthConstraint> getLengthConstraint() {
413             return Optional.empty();
414         }
415     }
416 }