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