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.util;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import com.google.common.collect.ImmutableRangeSet;
16 import com.google.common.collect.Range;
17 import com.google.common.collect.RangeSet;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.io.Serializable;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
24 import java.util.Objects;
25 import java.util.Optional;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
30 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
31 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
32 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
33 import org.opendaylight.mdsal.binding.model.api.Restrictions;
34 import org.opendaylight.mdsal.binding.model.api.Type;
35 import org.opendaylight.mdsal.binding.model.api.WildcardType;
36 import org.opendaylight.yangtools.concepts.Builder;
37 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
38 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
39 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
41 public final class Types {
42 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER = new CacheLoader<>() {
44 public ConcreteType load(final Class<?> key) {
45 return new ConcreteTypeImpl(JavaTypeName.create(key), null);
48 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
49 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
51 public static final @NonNull ConcreteType BOOLEAN = typeForClass(Boolean.class);
52 public static final @NonNull ConcreteType STRING = typeForClass(String.class);
53 public static final @NonNull ConcreteType VOID = typeForClass(Void.class);
54 public static final @NonNull ConcreteType BYTE_ARRAY = typeForClass(byte[].class);
56 private static final @NonNull ConcreteType BUILDER = typeForClass(Builder.class);
57 private static final @NonNull ConcreteType CLASS = typeForClass(Class.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_VOID = typeForClass(void.class);
63 private static final @NonNull ConcreteType SERIALIZABLE = typeForClass(Serializable.class);
64 private static final @NonNull ConcreteType SET_TYPE = typeForClass(Set.class);
67 * It is not desirable to create instance of this class.
73 * Returns an instance of {@link ParameterizedType} which represents JAVA <code>java.lang.Class</code> type
74 * specialized to specified type.
76 * @param type Type for which to specialize
77 * @return A parameterized type corresponding to {@code Class<Type>}
78 * @throws NullPointerException if {@code type} is null
80 public static @NonNull ParameterizedType classType(final Type type) {
81 return parameterizedTypeFor(CLASS, type);
85 * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
87 * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
89 public static @NonNull ConcreteType voidType() {
94 * Returns an instance of {@link ConcreteType} which represents {@link Object} type.
96 * @return <code>ConcreteType</code> instance which represents {@link Object}
98 public static @NonNull ConcreteType objectType() {
103 * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
105 * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
107 public static @NonNull ConcreteType primitiveVoidType() {
108 return PRIMITIVE_VOID;
112 * Returns an instance of {@link ConcreteType} which represents {@link Serializable} type.
114 * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
116 public static @NonNull ConcreteType serializableType() {
121 * Returns an instance of {@link ConcreteType} describing the class.
123 * @param cls Class to describe
124 * @return Description of class
126 public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls) {
127 return TYPE_CACHE.getUnchecked(cls);
130 public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls,
131 final @Nullable Restrictions restrictions) {
132 if (restrictions == null) {
133 return typeForClass(cls);
136 final JavaTypeName identifier = JavaTypeName.create(cls);
137 if (restrictions instanceof DefaultRestrictions) {
138 return new ConcreteTypeImpl(identifier, restrictions);
140 return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
144 * Returns an instance of {@link ParameterizedType} describing the typed {@link Map}<K,V>.
146 * @param keyType Key Type
147 * @param valueType Value Type
148 * @return Description of generic type instance
150 public static @NonNull ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
151 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
154 public static boolean isMapType(final ParameterizedType type) {
155 return MAP_TYPE.equals(type.getRawType());
158 public static boolean isMapType(final Type type) {
159 return type instanceof ParameterizedType && isMapType((ParameterizedType) type);
163 * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}<V> with concrete type
166 * @param valueType Value Type
167 * @return Description of generic type instance of Set
169 public static @NonNull ParameterizedType setTypeFor(final Type valueType) {
170 return parameterizedTypeFor(SET_TYPE, valueType);
174 * Returns an instance of {@link ParameterizedType} describing the typed {@link List}<V> with concrete type
177 * @param valueType Value Type
178 * @return Description of type instance of List
180 public static @NonNull ParameterizedType listTypeFor(final Type valueType) {
181 return parameterizedTypeFor(LIST_TYPE, valueType);
184 public static boolean isListType(final ParameterizedType type) {
185 return LIST_TYPE.equals(type.getRawType());
188 public static boolean isListType(final Type type) {
189 return type instanceof ParameterizedType && isListType((ParameterizedType) type);
193 * Returns an instance of {@link ParameterizedType} describing the typed {@link ListenableFuture}<V>
194 * with concrete type of value.
196 * @param valueType Value Type
197 * @return Description of type instance of ListenableFuture
199 public static @NonNull ParameterizedType listenableFutureTypeFor(final Type valueType) {
200 return parameterizedTypeFor(LISTENABLE_FUTURE, valueType);
204 * Returns an instance of {@link ParameterizedType} describing the typed
205 * {@link Builder}<V> with concrete type of value.
207 * @param valueType Value Type
208 * @return Description of type instance of Builder
210 public static @NonNull ParameterizedType builderTypeFor(final Type valueType) {
211 return parameterizedTypeFor(BUILDER, valueType);
215 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType}.
217 * @param type JAVA <code>Type</code> for raw type
218 * @param parameters JAVA <code>Type</code>s for actual parameter types
219 * @return <code>ParametrizedType</code> representation of <code>type</code> and its <code>parameters</code>
220 * @throws NullPointerException if any argument or any member of {@code parameters} is null
222 public static @NonNull ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
223 return new ParametrizedTypeImpl(type, parameters);
227 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
229 * @param identifier JavaTypeName of the type
230 * @return <code>WildcardType</code> representation of specified identifier
232 public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
233 return new WildcardTypeImpl(identifier);
237 * Creates instance of
238 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
239 * ParameterizedType} where raw type is
240 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
241 * parameter is <code>valueType</code>.
243 * @param valueType JAVA <code>Type</code> with actual parameter
244 * @return <code>ParametrizedType</code> representation of raw type
245 * <code>Augmentable</code> with actual parameter
246 * <code>valueType</code>
247 * @deprecated Use {@link BindingTypes#augmentable(Type)} instead.
249 @Deprecated(forRemoval = true)
250 public static @NonNull ParameterizedType augmentableTypeFor(final Type valueType) {
251 return BindingTypes.augmentable(valueType);
255 * Creates instance of {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType} where
256 * raw type is {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual parameter
257 * is <code>valueType</code>.
259 * @param valueType JAVA <code>Type</code> with actual parameter
260 * @return <code>ParametrizedType</code> representation of raw type
261 * <code>Augmentation</code> with actual parameter
262 * <code>valueType</code>
263 * @deprecated Use {@link BindingTypes#augmentation(Type)} instead.
265 @Deprecated(forRemoval = true)
266 public static @NonNull ParameterizedType augmentationTypeFor(final Type valueType) {
267 return BindingTypes.augmentation(valueType);
270 public static @Nullable String getOuterClassName(final Type valueType) {
271 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
275 * Represents concrete JAVA type.
277 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
278 private final Restrictions restrictions;
281 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
283 * @param pkName string with package name
284 * @param name string with the name of the type
286 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
288 this.restrictions = restrictions;
292 public Restrictions getRestrictions() {
293 return this.restrictions;
298 * Represents concrete JAVA type with changed restriction values.
300 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements
301 BaseTypeWithRestrictions {
302 private final Restrictions restrictions;
305 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
307 * @param pkName string with package name
308 * @param name string with the name of the type
310 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
312 this.restrictions = requireNonNull(restrictions);
316 public Restrictions getRestrictions() {
317 return this.restrictions;
322 * Represents parametrized JAVA type.
324 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
326 * Array of JAVA actual type parameters.
328 private final Type[] actualTypes;
331 * JAVA raw type (like List, Set, Map...).
333 private final Type rawType;
336 * Creates instance of this class with concrete rawType and array of actual parameters.
338 * @param rawType JAVA <code>Type</code> for raw type
339 * @param actTypes array of actual parameters
341 ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
342 super(rawType.getIdentifier());
343 this.rawType = requireNonNull(rawType);
344 actualTypes = actTypes.clone();
345 if (Arrays.stream(actualTypes).anyMatch(Objects::isNull)) {
346 throw new NullPointerException("actTypes contains a null");
351 public Type[] getActualTypeArguments() {
353 return this.actualTypes;
357 public Type getRawType() {
363 * Represents JAVA bounded wildcard type.
365 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
367 * Creates instance of this class with concrete package and type name.
369 * @param packageName string with the package name
370 * @param typeName string with the name of type
372 WildcardTypeImpl(final JavaTypeName identifier) {
377 public static <T extends Number & Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
379 return new DefaultRestrictions<>(min, max);
382 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
383 private final RangeConstraint<?> rangeConstraint;
385 DefaultRestrictions(final T min, final T max) {
386 this.rangeConstraint = new DefaultRangeConstraint<>(min, max);
390 public boolean isEmpty() {
395 public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
396 return Optional.of(rangeConstraint);
400 public List<PatternConstraint> getPatternConstraints() {
401 return Collections.emptyList();
405 public Optional<LengthConstraint> getLengthConstraint() {
406 return Optional.empty();
410 private static final class DefaultRangeConstraint<T extends Number & Comparable<T>> implements RangeConstraint<T> {
414 DefaultRangeConstraint(final T min, final T max) {
415 this.min = requireNonNull(min);
416 this.max = requireNonNull(max);
420 public Optional<String> getErrorAppTag() {
421 return Optional.empty();
425 public Optional<String> getErrorMessage() {
426 return Optional.empty();
430 public Optional<String> getDescription() {
431 return Optional.empty();
435 public Optional<String> getReference() {
436 return Optional.empty();
440 public RangeSet<T> getAllowedRanges() {
441 return ImmutableRangeSet.of(Range.closed(min, max));