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