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.util.concurrent.ListenableFuture;
17 import java.io.Serializable;
18 import java.util.Arrays;
19 import java.util.List;
21 import java.util.Objects;
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;
34 public final class Types {
35 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE = CacheBuilder.newBuilder().weakKeys()
36 .build(new CacheLoader<>() {
38 public ConcreteType load(final Class<?> key) {
39 return new ConcreteTypeImpl(JavaTypeName.create(key), null);
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);
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);
62 * It is not desirable to create instance of this class.
68 * Returns an instance of {@link ParameterizedType} which represents JAVA <code>java.lang.Class</code> type
69 * specialized to specified type.
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
75 public static @NonNull ParameterizedType classType(final Type type) {
76 return parameterizedTypeFor(CLASS, type);
80 * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
82 * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
84 public static @NonNull ConcreteType voidType() {
89 * Returns an instance of {@link ConcreteType} which represents {@link Object} type.
91 * @return <code>ConcreteType</code> instance which represents {@link Object}
93 public static @NonNull ConcreteType objectType() {
98 * Returns an instance of {@link ConcreteType} which represents JAVA <code>boolean</code> type.
100 * @return <code>ConcreteType</code> instance which represents JAVA <code>boolean</code>
102 public static @NonNull ConcreteType primitiveBooleanType() {
103 return PRIMITIVE_BOOLEAN;
107 * Returns an instance of {@link ConcreteType} which represents JAVA <code>int</code> type.
109 * @return <code>ConcreteType</code> instance which represents JAVA <code>int</code>
111 public static @NonNull ConcreteType primitiveIntType() {
112 return PRIMITIVE_INT;
116 * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
118 * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
120 public static @NonNull ConcreteType primitiveVoidType() {
121 return PRIMITIVE_VOID;
125 * Returns an instance of {@link ConcreteType} which represents {@link Serializable} type.
127 * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
129 public static @NonNull ConcreteType serializableType() {
134 * Returns an instance of {@link ConcreteType} describing the class.
136 * @param cls Class to describe
137 * @return Description of class
139 public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls) {
140 return TYPE_CACHE.getUnchecked(cls);
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);
149 public static @NonNull ConcreteType restrictedType(final Type type, final @NonNull Restrictions restrictions) {
150 return restrictedType(type.getIdentifier(), restrictions);
153 private static @NonNull ConcreteType restrictedType(final JavaTypeName identifier,
154 final @NonNull Restrictions restrictions) {
155 return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
159 * Returns an instance of {@link ParameterizedType} describing the typed {@link Map}<K,V>.
161 * @param keyType Key Type
162 * @param valueType Value Type
163 * @return Description of generic type instance
165 public static @NonNull ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
166 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
169 public static boolean isMapType(final ParameterizedType type) {
170 return MAP_TYPE.equals(type.getRawType());
174 * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}<V> with concrete type
177 * @param valueType Value Type
178 * @return Description of generic type instance of Set
180 public static @NonNull ParameterizedType setTypeFor(final Type valueType) {
181 return parameterizedTypeFor(SET_TYPE, valueType);
185 * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}<?>.
187 * @return Description of type instance of Set
189 public static @NonNull ParameterizedType setTypeWildcard() {
190 return SET_TYPE_WILDCARD;
193 public static boolean isSetType(final ParameterizedType type) {
194 return SET_TYPE.equals(type.getRawType());
198 * Returns an instance of {@link ParameterizedType} describing the typed {@link List}<V> with concrete type
201 * @param valueType Value Type
202 * @return Description of type instance of List
204 public static @NonNull ParameterizedType listTypeFor(final Type valueType) {
205 return parameterizedTypeFor(LIST_TYPE, valueType);
209 * Returns an instance of {@link ParameterizedType} describing the typed {@link List}<?>.
211 * @return Description of type instance of List
213 public static @NonNull ParameterizedType listTypeWildcard() {
214 return LIST_TYPE_WILDCARD;
217 public static boolean isListType(final ParameterizedType type) {
218 return LIST_TYPE.equals(type.getRawType());
222 * Returns an instance of {@link ParameterizedType} describing the typed {@link ListenableFuture}<V>
223 * with concrete type of value.
225 * @param valueType Value Type
226 * @return Description of type instance of ListenableFuture
228 public static @NonNull ParameterizedType listenableFutureTypeFor(final Type valueType) {
229 return parameterizedTypeFor(LISTENABLE_FUTURE, valueType);
233 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType}.
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
240 public static @NonNull ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
241 return new ParametrizedTypeImpl(type, parameters);
245 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
247 * @param identifier JavaTypeName of the type
248 * @return <code>WildcardType</code> representation of specified identifier
250 public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
251 return new WildcardTypeImpl(identifier);
254 public static boolean strictTypeEquals(final Type type1, final Type type2) {
255 if (!type1.equals(type2)) {
258 if (type1 instanceof ParameterizedType param1) {
259 if (type2 instanceof ParameterizedType param2) {
260 return Arrays.equals(param1.getActualTypeArguments(), param2.getActualTypeArguments());
264 return !(type2 instanceof ParameterizedType);
267 public static @Nullable String getOuterClassName(final Type valueType) {
268 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
272 * Represents concrete JAVA type.
274 private static final class ConcreteTypeImpl extends AbstractType implements ConcreteType {
275 private final Restrictions restrictions;
278 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
280 * @param pkName string with package name
281 * @param name string with the name of the type
283 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
285 this.restrictions = restrictions;
289 public Restrictions getRestrictions() {
295 * Represents concrete JAVA type with changed restriction values.
297 private static final class BaseTypeWithRestrictionsImpl extends AbstractType implements
298 BaseTypeWithRestrictions {
299 private final Restrictions restrictions;
302 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
304 * @param pkName string with package name
305 * @param name string with the name of the type
307 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
309 this.restrictions = requireNonNull(restrictions);
313 public Restrictions getRestrictions() {
319 * Represents parametrized JAVA type.
321 private static class ParametrizedTypeImpl extends AbstractType implements ParameterizedType {
323 * Array of JAVA actual type parameters.
325 private final Type[] actualTypes;
328 * JAVA raw type (like List, Set, Map...).
330 private final Type rawType;
333 * Creates instance of this class with concrete rawType and array of actual parameters.
335 * @param rawType JAVA <code>Type</code> for raw type
336 * @param actTypes array of actual parameters
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");
348 public Type[] getActualTypeArguments() {
354 public Type getRawType() {
360 * Represents JAVA bounded wildcard type.
362 private static class WildcardTypeImpl extends AbstractType implements WildcardType {
364 * Creates instance of this class with concrete package and type name.
366 * @param packageName string with the package name
367 * @param typeName string with the name of type
369 WildcardTypeImpl(final JavaTypeName identifier) {