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