Add Types.isListType(ParameterizedType)
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import com.google.common.collect.ImmutableRangeSet;
16 import com.google.common.collect.Range;
17 import com.google.common.collect.RangeSet;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.io.Serializable;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.Optional;
26 import java.util.Set;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
29 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
30 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
31 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
32 import org.opendaylight.mdsal.binding.model.api.Restrictions;
33 import org.opendaylight.mdsal.binding.model.api.Type;
34 import org.opendaylight.mdsal.binding.model.api.WildcardType;
35 import org.opendaylight.yangtools.concepts.Builder;
36 import org.opendaylight.yangtools.yang.binding.Augmentable;
37 import org.opendaylight.yangtools.yang.binding.Augmentation;
38 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
39 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
40 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
41
42 public final class Types {
43     private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER = new CacheLoader<Class<?>, ConcreteType>() {
44         @Override
45         public ConcreteType load(final Class<?> key) {
46             return new ConcreteTypeImpl(JavaTypeName.create(key), null);
47         }
48     };
49     private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
50             CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
51
52
53     public static final ConcreteType BOOLEAN = typeForClass(Boolean.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 = typeForClass(byte[].class);
57
58     private static final ConcreteType BUILDER = typeForClass(Builder.class);
59     private static final ConcreteType CLASS = typeForClass(Class.class);
60     private static final ConcreteType LIST_TYPE = typeForClass(List.class);
61     private static final ConcreteType LISTENABLE_FUTURE = typeForClass(ListenableFuture.class);
62     private static final ConcreteType MAP_TYPE = typeForClass(Map.class);
63     private static final ConcreteType OBJECT = typeForClass(Object.class);
64     private static final ConcreteType PRIMITIVE_VOID = typeForClass(void.class);
65     private static final ConcreteType SERIALIZABLE = typeForClass(Serializable.class);
66     private static final ConcreteType SET_TYPE = typeForClass(Set.class);
67
68     /**
69      * It is not desirable to create instance of this class.
70      */
71     private Types() {
72     }
73
74     /**
75      * Returns an instance of {@link ParameterizedType} which represents JAVA <code>java.lang.Class</code> type
76      * specialized to specified type.
77      *
78      * @param type Type for which to specialize
79      * @return A parameterized type corresponding to {@code Class<Type>}
80      * @throws NullPointerException if {@code type} is null
81      */
82     public static ParameterizedType classType(final Type type) {
83         return parameterizedTypeFor(CLASS, type);
84     }
85
86     /**
87      * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
88      *
89      * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
90      */
91     public static ConcreteType voidType() {
92         return VOID;
93     }
94
95     /**
96      * Returns an instance of {@link ConcreteType} which represents {@link Object} type.
97      *
98      * @return <code>ConcreteType</code> instance which represents {@link Object}
99      */
100     public static ConcreteType objectType() {
101         return OBJECT;
102     }
103
104     /**
105      * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
106      *
107      * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
108      */
109     public static ConcreteType primitiveVoidType() {
110         return PRIMITIVE_VOID;
111     }
112
113     /**
114      * Returns an instance of {@link ConcreteType} which represents {@link Serializable} type.
115      *
116      * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
117      */
118     public static ConcreteType serializableType() {
119         return SERIALIZABLE;
120     }
121
122     /**
123      * Returns an instance of {@link ConcreteType} describing the class.
124      *
125      * @param cls Class to describe
126      * @return Description of class
127      */
128     public static ConcreteType typeForClass(final Class<?> cls) {
129         return TYPE_CACHE.getUnchecked(cls);
130     }
131
132     public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
133         if (restrictions == null) {
134             return typeForClass(cls);
135         }
136
137         final JavaTypeName identifier = JavaTypeName.create(cls);
138         if (restrictions instanceof DefaultRestrictions) {
139             return new ConcreteTypeImpl(identifier, restrictions);
140         }
141         return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
142     }
143
144     /**
145      * Returns an instance of {@link ParameterizedType} describing the typed {@link Map}&lt;K,V&gt;.
146      *
147      * @param keyType Key Type
148      * @param valueType Value Type
149      * @return Description of generic type instance
150      */
151     public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
152         return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
153     }
154
155     /**
156      * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}&lt;V&gt; with concrete type
157      * of value.
158      *
159      * @param valueType Value Type
160      * @return Description of generic type instance of Set
161      */
162     public static ParameterizedType setTypeFor(final Type valueType) {
163         return parameterizedTypeFor(SET_TYPE, valueType);
164     }
165
166     /**
167      * Returns an instance of {@link ParameterizedType} describing the typed {@link List}&lt;V&gt; with concrete type
168      * of value.
169      *
170      * @param valueType Value Type
171      * @return Description of type instance of List
172      */
173     public static ParameterizedType listTypeFor(final Type valueType) {
174         return parameterizedTypeFor(LIST_TYPE, valueType);
175     }
176
177     public static boolean isListType(final ParameterizedType type) {
178         return LIST_TYPE.equals(type.getRawType());
179     }
180
181     public static boolean isListType(final Type type) {
182         return type instanceof ParameterizedType && isListType((ParameterizedType) type);
183     }
184
185     /**
186      * Returns an instance of {@link ParameterizedType} describing the typed {@link ListenableFuture}&lt;V&gt;
187      * with concrete type of value.
188      *
189      * @param valueType Value Type
190      * @return Description of type instance of ListenableFuture
191      */
192     public static ParameterizedType listenableFutureTypeFor(final Type valueType) {
193         return parameterizedTypeFor(LISTENABLE_FUTURE, valueType);
194     }
195
196     /**
197      * Returns an instance of {@link ParameterizedType} describing the typed
198      * {@link Builder}&lt;V&gt; with concrete type of value.
199      *
200      * @param valueType Value Type
201      * @return Description of type instance of Builder
202      */
203     public static ParameterizedType builderTypeFor(final Type valueType) {
204         return parameterizedTypeFor(BUILDER, valueType);
205     }
206
207     /**
208      * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType}.
209      *
210      * @param type JAVA <code>Type</code> for raw type
211      * @param parameters JAVA <code>Type</code>s for actual parameter types
212      * @return <code>ParametrizedType</code> representation of <code>type</code> and its <code>parameters</code>
213      * @throws NullPointerException if any argument or any member of {@code parameters} is null
214      */
215     public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
216         return new ParametrizedTypeImpl(type, parameters);
217     }
218
219     /**
220      * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
221      *
222      * @param identifier JavaTypeName of the type
223      * @return <code>WildcardType</code> representation of specified identifier
224      */
225     public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
226         return new WildcardTypeImpl(identifier);
227     }
228
229     /**
230      * Creates instance of
231      * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
232      * ParameterizedType} where raw type is
233      * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
234      * parameter is <code>valueType</code>.
235      *
236      * @param valueType JAVA <code>Type</code> with actual parameter
237      * @return <code>ParametrizedType</code> representation of raw type
238      *         <code>Augmentable</code> with actual parameter
239      *         <code>valueType</code>
240      */
241     public static ParameterizedType augmentableTypeFor(final Type valueType) {
242         final Type augmentable = typeForClass(Augmentable.class);
243         return parameterizedTypeFor(augmentable, valueType);
244     }
245
246     /**
247      * Creates instance of {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType} where
248      * raw type is {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual parameter
249      * is <code>valueType</code>.
250      *
251      * @param valueType JAVA <code>Type</code> with actual parameter
252      * @return <code>ParametrizedType</code> reprezentation of raw type
253      *         <code>Augmentation</code> with actual parameter
254      *         <code>valueType</code>
255      */
256     public static ParameterizedType augmentationTypeFor(final Type valueType) {
257         final Type augmentation = typeForClass(Augmentation.class);
258         return parameterizedTypeFor(augmentation, valueType);
259     }
260
261     public static @Nullable String getOuterClassName(final Type valueType) {
262         return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
263     }
264
265     /**
266      * Represents concrete JAVA type.
267      */
268     private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
269         private final Restrictions restrictions;
270
271         /**
272          * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
273          *
274          * @param pkName string with package name
275          * @param name string with the name of the type
276          */
277         ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
278             super(identifier);
279             this.restrictions = restrictions;
280         }
281
282         @Override
283         public Restrictions getRestrictions() {
284             return this.restrictions;
285         }
286     }
287
288     /**
289      * Represents concrete JAVA type with changed restriction values.
290      */
291     private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements
292             BaseTypeWithRestrictions {
293         private final Restrictions restrictions;
294
295         /**
296          * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
297          *
298          * @param pkName string with package name
299          * @param name string with the name of the type
300          */
301         BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
302             super(identifier);
303             this.restrictions = requireNonNull(restrictions);
304         }
305
306         @Override
307         public Restrictions getRestrictions() {
308             return this.restrictions;
309         }
310     }
311
312     /**
313      * Represents parametrized JAVA type.
314      */
315     private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
316         /**
317          * Array of JAVA actual type parameters.
318          */
319         private final Type[] actualTypes;
320
321         /**
322          * JAVA raw type (like List, Set, Map...).
323          */
324         private final Type rawType;
325
326         /**
327          * Creates instance of this class with concrete rawType and array of actual parameters.
328          *
329          * @param rawType JAVA <code>Type</code> for raw type
330          * @param actTypes array of actual parameters
331          */
332         ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
333             super(rawType.getIdentifier());
334             this.rawType = requireNonNull(rawType);
335             actualTypes = actTypes.clone();
336             if (Arrays.stream(actualTypes).anyMatch(Objects::isNull)) {
337                 throw new NullPointerException("actTypes contains a null");
338             }
339         }
340
341         @Override
342         public Type[] getActualTypeArguments() {
343
344             return this.actualTypes;
345         }
346
347         @Override
348         public Type getRawType() {
349             return this.rawType;
350         }
351     }
352
353     /**
354      * Represents JAVA bounded wildcard type.
355      */
356     private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
357         /**
358          * Creates instance of this class with concrete package and type name.
359          *
360          * @param packageName string with the package name
361          * @param typeName string with the name of type
362          */
363         WildcardTypeImpl(final JavaTypeName identifier) {
364             super(identifier);
365         }
366     }
367
368     public static <T extends Number & Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
369             final T max) {
370         return new DefaultRestrictions<>(min, max);
371     }
372
373     private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
374         private final RangeConstraint<?> rangeConstraint;
375
376         DefaultRestrictions(final T min, final T max) {
377             this.rangeConstraint = new DefaultRangeConstraint<>(min, max);
378         }
379
380         @Override
381         public boolean isEmpty() {
382             return false;
383         }
384
385         @Override
386         public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
387             return Optional.of(rangeConstraint);
388         }
389
390         @Override
391         public List<PatternConstraint> getPatternConstraints() {
392             return Collections.emptyList();
393         }
394
395         @Override
396         public Optional<LengthConstraint> getLengthConstraint() {
397             return Optional.empty();
398         }
399     }
400
401     private static final class DefaultRangeConstraint<T extends Number & Comparable<T>> implements RangeConstraint<T> {
402         private final T min;
403         private final T max;
404
405         DefaultRangeConstraint(final T min, final T max) {
406             this.min = requireNonNull(min);
407             this.max = requireNonNull(max);
408         }
409
410         @Override
411         public Optional<String> getErrorAppTag() {
412             return Optional.empty();
413         }
414
415         @Override
416         public Optional<String> getErrorMessage() {
417             return Optional.empty();
418         }
419
420         @Override
421         public Optional<String> getDescription() {
422             return Optional.empty();
423         }
424
425         @Override
426         public Optional<String> getReference() {
427             return Optional.empty();
428         }
429
430         @Override
431         public RangeSet<T> getAllowedRanges() {
432             return ImmutableRangeSet.of(Range.closed(min, max));
433         }
434     }
435 }