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