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