2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.model.ri;
10 import static java.util.Objects.requireNonNull;
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;
22 import java.util.Objects;
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;
35 public final class Types {
36 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE = CacheBuilder.newBuilder().weakKeys()
37 .build(new CacheLoader<>() {
39 public ConcreteType load(final Class<?> key) {
40 return new ConcreteTypeImpl(JavaTypeName.create(key), null);
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);
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);
64 * It is not desirable to create instance of this class.
70 * Returns an instance of {@link ParameterizedType} which represents JAVA <code>java.lang.Class</code> type
71 * specialized to specified type.
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
77 public static @NonNull ParameterizedType classType(final Type type) {
78 return parameterizedTypeFor(CLASS, type);
82 * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
84 * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
86 public static @NonNull ConcreteType voidType() {
91 * Returns an instance of {@link ConcreteType} which represents {@link Object} type.
93 * @return <code>ConcreteType</code> instance which represents {@link Object}
95 public static @NonNull ConcreteType objectType() {
100 * Returns an instance of {@link ConcreteType} which represents JAVA <code>boolean</code> type.
102 * @return <code>ConcreteType</code> instance which represents JAVA <code>boolean</code>
104 public static @NonNull ConcreteType primitiveBooleanType() {
105 return PRIMITIVE_BOOLEAN;
109 * Returns an instance of {@link ConcreteType} which represents JAVA <code>int</code> type.
111 * @return <code>ConcreteType</code> instance which represents JAVA <code>int</code>
113 public static @NonNull ConcreteType primitiveIntType() {
114 return PRIMITIVE_INT;
118 * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
120 * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
122 public static @NonNull ConcreteType primitiveVoidType() {
123 return PRIMITIVE_VOID;
127 * Returns an instance of {@link ConcreteType} which represents {@link Serializable} type.
129 * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
131 public static @NonNull ConcreteType serializableType() {
136 * Returns an instance of {@link ConcreteType} describing the class.
138 * @param cls Class to describe
139 * @return Description of class
141 public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls) {
142 return TYPE_CACHE.getUnchecked(cls);
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);
151 public static @NonNull ConcreteType restrictedType(final Type type, final @NonNull Restrictions restrictions) {
152 return restrictedType(type.getIdentifier(), restrictions);
155 private static @NonNull ConcreteType restrictedType(final JavaTypeName identifier,
156 final @NonNull Restrictions restrictions) {
157 return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
161 * Returns an instance of {@link ParameterizedType} describing the typed {@link Map}<K,V>.
163 * @param keyType Key Type
164 * @param valueType Value Type
165 * @return Description of generic type instance
167 public static @NonNull ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
168 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
171 public static boolean isMapType(final ParameterizedType type) {
172 return MAP_TYPE.equals(type.getRawType());
176 * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}<V> with concrete type
179 * @param valueType Value Type
180 * @return Description of generic type instance of Set
182 public static @NonNull ParameterizedType setTypeFor(final Type valueType) {
183 return parameterizedTypeFor(SET_TYPE, valueType);
187 * Returns an instance of {@link ParameterizedType} describing the typed {@link ImmutableSet}<V> with concrete
190 * @param valueType Value Type
191 * @return Description of generic type instance of ImmutableSet
193 public static @NonNull ParameterizedType immutableSetTypeFor(final Type valueType) {
194 return parameterizedTypeFor(IMMUTABLE_SET_TYPE, valueType);
198 * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}<?>.
200 * @return Description of type instance of Set
202 public static @NonNull ParameterizedType setTypeWildcard() {
203 return SET_TYPE_WILDCARD;
206 public static boolean isSetType(final ParameterizedType type) {
207 return SET_TYPE.equals(type.getRawType());
211 * Returns an instance of {@link ParameterizedType} describing the typed {@link List}<V> with concrete type
214 * @param valueType Value Type
215 * @return Description of type instance of List
217 public static @NonNull ParameterizedType listTypeFor(final Type valueType) {
218 return parameterizedTypeFor(LIST_TYPE, valueType);
222 * Returns an instance of {@link ParameterizedType} describing the typed {@link List}<?>.
224 * @return Description of type instance of List
226 public static @NonNull ParameterizedType listTypeWildcard() {
227 return LIST_TYPE_WILDCARD;
230 public static boolean isListType(final ParameterizedType type) {
231 return LIST_TYPE.equals(type.getRawType());
235 * Returns an instance of {@link ParameterizedType} describing the typed {@link ListenableFuture}<V>
236 * with concrete type of value.
238 * @param valueType Value Type
239 * @return Description of type instance of ListenableFuture
241 public static @NonNull ParameterizedType listenableFutureTypeFor(final Type valueType) {
242 return parameterizedTypeFor(LISTENABLE_FUTURE, valueType);
246 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType}.
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
253 public static @NonNull ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
254 return new ParametrizedTypeImpl(type, parameters);
258 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
260 * @param identifier JavaTypeName of the type
261 * @return <code>WildcardType</code> representation of specified identifier
263 public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
264 return new WildcardTypeImpl(identifier);
267 public static boolean strictTypeEquals(final Type type1, final Type type2) {
268 if (!type1.equals(type2)) {
271 if (type1 instanceof ParameterizedType param1) {
272 if (type2 instanceof ParameterizedType param2) {
273 return Arrays.equals(param1.getActualTypeArguments(), param2.getActualTypeArguments());
277 return !(type2 instanceof ParameterizedType);
280 public static @Nullable String getOuterClassName(final Type valueType) {
281 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
285 * Represents concrete JAVA type.
287 private static final class ConcreteTypeImpl extends AbstractType implements ConcreteType {
288 private final Restrictions restrictions;
291 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
293 * @param pkName string with package name
294 * @param name string with the name of the type
296 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
298 this.restrictions = restrictions;
302 public Restrictions getRestrictions() {
308 * Represents concrete JAVA type with changed restriction values.
310 private static final class BaseTypeWithRestrictionsImpl extends AbstractType implements
311 BaseTypeWithRestrictions {
312 private final Restrictions restrictions;
315 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
317 * @param pkName string with package name
318 * @param name string with the name of the type
320 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
322 this.restrictions = requireNonNull(restrictions);
326 public Restrictions getRestrictions() {
332 * Represents parametrized JAVA type.
334 private static class ParametrizedTypeImpl extends AbstractType implements ParameterizedType {
336 * Array of JAVA actual type parameters.
338 private final Type[] actualTypes;
341 * JAVA raw type (like List, Set, Map...).
343 private final Type rawType;
346 * Creates instance of this class with concrete rawType and array of actual parameters.
348 * @param rawType JAVA <code>Type</code> for raw type
349 * @param actTypes array of actual parameters
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");
361 public Type[] getActualTypeArguments() {
367 public Type getRawType() {
373 * Represents JAVA bounded wildcard type.
375 private static class WildcardTypeImpl extends AbstractType implements WildcardType {
377 * Creates instance of this class with concrete package and type name.
379 * @param packageName string with the package name
380 * @param typeName string with the name of type
382 WildcardTypeImpl(final JavaTypeName identifier) {