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.ImmutableRangeSet;
17 import com.google.common.collect.Range;
18 import com.google.common.collect.RangeSet;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import java.io.Serializable;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
25 import java.util.Objects;
26 import java.util.Optional;
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.opendaylight.mdsal.binding.model.api.AbstractType;
31 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
32 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
33 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
34 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
35 import org.opendaylight.mdsal.binding.model.api.Restrictions;
36 import org.opendaylight.mdsal.binding.model.api.Type;
37 import org.opendaylight.mdsal.binding.model.api.WildcardType;
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;
42 public final class Types {
43 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER = new CacheLoader<>() {
45 public ConcreteType load(final Class<?> key) {
46 return new ConcreteTypeImpl(JavaTypeName.create(key), null);
49 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
50 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
52 public static final @NonNull ConcreteType BOOLEAN = typeForClass(Boolean.class);
53 public static final @NonNull ConcreteType BYTE_ARRAY = typeForClass(byte[].class);
54 public static final @NonNull ConcreteType CLASS = typeForClass(Class.class);
55 public static final @NonNull ConcreteType STRING = typeForClass(String.class);
56 public static final @NonNull ConcreteType VOID = typeForClass(Void.class);
58 private static final @NonNull ConcreteType LIST_TYPE = typeForClass(List.class);
59 private static final @NonNull ConcreteType LISTENABLE_FUTURE = typeForClass(ListenableFuture.class);
60 private static final @NonNull ConcreteType MAP_TYPE = typeForClass(Map.class);
61 private static final @NonNull ConcreteType OBJECT = typeForClass(Object.class);
62 private static final @NonNull ConcreteType PRIMITIVE_BOOLEAN = typeForClass(boolean.class);
63 private static final @NonNull ConcreteType PRIMITIVE_INT = typeForClass(int.class);
64 private static final @NonNull ConcreteType PRIMITIVE_VOID = typeForClass(void.class);
65 private static final @NonNull ConcreteType SERIALIZABLE = typeForClass(Serializable.class);
66 private static final @NonNull ConcreteType SET_TYPE = typeForClass(Set.class);
67 private static final @NonNull ParameterizedType LIST_TYPE_WILDCARD = parameterizedTypeFor(LIST_TYPE);
68 private static final @NonNull ParameterizedType SET_TYPE_WILDCARD = parameterizedTypeFor(SET_TYPE);
71 * It is not desirable to create instance of this class.
77 * Returns an instance of {@link ParameterizedType} which represents JAVA <code>java.lang.Class</code> type
78 * specialized to specified type.
80 * @param type Type for which to specialize
81 * @return A parameterized type corresponding to {@code Class<Type>}
82 * @throws NullPointerException if {@code type} is null
84 public static @NonNull ParameterizedType classType(final Type type) {
85 return parameterizedTypeFor(CLASS, type);
89 * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
91 * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
93 public static @NonNull ConcreteType voidType() {
98 * Returns an instance of {@link ConcreteType} which represents {@link Object} type.
100 * @return <code>ConcreteType</code> instance which represents {@link Object}
102 public static @NonNull ConcreteType objectType() {
107 * Returns an instance of {@link ConcreteType} which represents JAVA <code>boolean</code> type.
109 * @return <code>ConcreteType</code> instance which represents JAVA <code>boolean</code>
111 public static @NonNull ConcreteType primitiveBooleanType() {
112 return PRIMITIVE_BOOLEAN;
116 * Returns an instance of {@link ConcreteType} which represents JAVA <code>int</code> type.
118 * @return <code>ConcreteType</code> instance which represents JAVA <code>int</code>
120 public static @NonNull ConcreteType primitiveIntType() {
121 return PRIMITIVE_INT;
125 * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
127 * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
129 public static @NonNull ConcreteType primitiveVoidType() {
130 return PRIMITIVE_VOID;
134 * Returns an instance of {@link ConcreteType} which represents {@link Serializable} type.
136 * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
138 public static @NonNull ConcreteType serializableType() {
143 * Returns an instance of {@link ConcreteType} describing the class.
145 * @param cls Class to describe
146 * @return Description of class
148 public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls) {
149 return TYPE_CACHE.getUnchecked(cls);
152 public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls,
153 final @Nullable Restrictions restrictions) {
154 return restrictions == null ? typeForClass(cls) : restrictedType(JavaTypeName.create(cls), restrictions);
158 public static @NonNull ConcreteType restrictedType(final Type type, final @NonNull Restrictions restrictions) {
159 return restrictedType(type.getIdentifier(), restrictions);
162 private static @NonNull ConcreteType restrictedType(final JavaTypeName identifier,
163 final @NonNull Restrictions restrictions) {
164 return restrictions instanceof DefaultRestrictions ? new ConcreteTypeImpl(identifier, restrictions)
165 : new BaseTypeWithRestrictionsImpl(identifier, restrictions);
169 * Returns an instance of {@link ParameterizedType} describing the typed {@link Map}<K,V>.
171 * @param keyType Key Type
172 * @param valueType Value Type
173 * @return Description of generic type instance
175 public static @NonNull ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
176 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
179 public static boolean isMapType(final ParameterizedType type) {
180 return MAP_TYPE.equals(type.getRawType());
184 * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}<V> with concrete type
187 * @param valueType Value Type
188 * @return Description of generic type instance of Set
190 public static @NonNull ParameterizedType setTypeFor(final Type valueType) {
191 return parameterizedTypeFor(SET_TYPE, valueType);
195 * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}<?>.
197 * @return Description of type instance of Set
199 public static @NonNull ParameterizedType setTypeWildcard() {
200 return SET_TYPE_WILDCARD;
203 public static boolean isSetType(final ParameterizedType type) {
204 return SET_TYPE.equals(type.getRawType());
208 * Returns an instance of {@link ParameterizedType} describing the typed {@link List}<V> with concrete type
211 * @param valueType Value Type
212 * @return Description of type instance of List
214 public static @NonNull ParameterizedType listTypeFor(final Type valueType) {
215 return parameterizedTypeFor(LIST_TYPE, valueType);
219 * Returns an instance of {@link ParameterizedType} describing the typed {@link List}<?>.
221 * @return Description of type instance of List
223 public static @NonNull ParameterizedType listTypeWildcard() {
224 return LIST_TYPE_WILDCARD;
227 public static boolean isListType(final ParameterizedType type) {
228 return LIST_TYPE.equals(type.getRawType());
232 * Returns an instance of {@link ParameterizedType} describing the typed {@link ListenableFuture}<V>
233 * with concrete type of value.
235 * @param valueType Value Type
236 * @return Description of type instance of ListenableFuture
238 public static @NonNull ParameterizedType listenableFutureTypeFor(final Type valueType) {
239 return parameterizedTypeFor(LISTENABLE_FUTURE, valueType);
243 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType}.
245 * @param type JAVA <code>Type</code> for raw type
246 * @param parameters JAVA <code>Type</code>s for actual parameter types
247 * @return <code>ParametrizedType</code> representation of <code>type</code> and its <code>parameters</code>
248 * @throws NullPointerException if any argument or any member of {@code parameters} is null
250 public static @NonNull ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
251 return new ParametrizedTypeImpl(type, parameters);
255 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
257 * @param identifier JavaTypeName of the type
258 * @return <code>WildcardType</code> representation of specified identifier
260 public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
261 return new WildcardTypeImpl(identifier);
264 public static boolean strictTypeEquals(final Type type1, final Type type2) {
265 if (!type1.equals(type2)) {
268 if (type1 instanceof ParameterizedType) {
269 if (type2 instanceof ParameterizedType) {
270 return Arrays.equals(((ParameterizedType) type1).getActualTypeArguments(),
271 ((ParameterizedType) type2).getActualTypeArguments());
275 return !(type2 instanceof ParameterizedType);
278 public static @Nullable String getOuterClassName(final Type valueType) {
279 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
283 * Represents concrete JAVA type.
285 private static final class ConcreteTypeImpl extends AbstractType implements ConcreteType {
286 private final Restrictions restrictions;
289 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
291 * @param pkName string with package name
292 * @param name string with the name of the type
294 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
296 this.restrictions = restrictions;
300 public Restrictions getRestrictions() {
306 * Represents concrete JAVA type with changed restriction values.
308 private static final class BaseTypeWithRestrictionsImpl extends AbstractType implements
309 BaseTypeWithRestrictions {
310 private final Restrictions restrictions;
313 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
315 * @param pkName string with package name
316 * @param name string with the name of the type
318 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
320 this.restrictions = requireNonNull(restrictions);
324 public Restrictions getRestrictions() {
330 * Represents parametrized JAVA type.
332 private static class ParametrizedTypeImpl extends AbstractType implements ParameterizedType {
334 * Array of JAVA actual type parameters.
336 private final Type[] actualTypes;
339 * JAVA raw type (like List, Set, Map...).
341 private final Type rawType;
344 * Creates instance of this class with concrete rawType and array of actual parameters.
346 * @param rawType JAVA <code>Type</code> for raw type
347 * @param actTypes array of actual parameters
349 ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
350 super(rawType.getIdentifier());
351 this.rawType = requireNonNull(rawType);
352 actualTypes = actTypes.clone();
353 if (Arrays.stream(actualTypes).anyMatch(Objects::isNull)) {
354 throw new NullPointerException("actTypes contains a null");
359 public Type[] getActualTypeArguments() {
365 public Type getRawType() {
371 * Represents JAVA bounded wildcard type.
373 private static class WildcardTypeImpl extends AbstractType implements WildcardType {
375 * Creates instance of this class with concrete package and type name.
377 * @param packageName string with the package name
378 * @param typeName string with the name of type
380 WildcardTypeImpl(final JavaTypeName identifier) {
385 // FIXME: 9.0.0: remove this method
386 public static <T extends Number & Comparable<T>> Restrictions getDefaultRestrictions(final T min, final T max) {
387 return new DefaultRestrictions<>(min, max);
390 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
391 private final RangeConstraint<?> rangeConstraint;
393 DefaultRestrictions(final T min, final T max) {
394 this.rangeConstraint = new DefaultRangeConstraint<>(min, max);
398 public boolean isEmpty() {
403 public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
404 return Optional.of(rangeConstraint);
408 public List<PatternConstraint> getPatternConstraints() {
409 return Collections.emptyList();
413 public Optional<LengthConstraint> getLengthConstraint() {
414 return Optional.empty();
418 private static final class DefaultRangeConstraint<T extends Number & Comparable<T>> implements RangeConstraint<T> {
422 DefaultRangeConstraint(final T min, final T max) {
423 this.min = requireNonNull(min);
424 this.max = requireNonNull(max);
428 public Optional<String> getErrorAppTag() {
429 return Optional.empty();
433 public Optional<String> getErrorMessage() {
434 return Optional.empty();
438 public Optional<String> getDescription() {
439 return Optional.empty();
443 public Optional<String> getReference() {
444 return Optional.empty();
448 public RangeSet<@NonNull T> getAllowedRanges() {
449 return ImmutableRangeSet.of(Range.closed(min, max));