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