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.binding.Augmentable;
38 import org.opendaylight.yangtools.yang.binding.Augmentation;
39 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
40 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
41 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
43 public final class Types {
44 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER = new CacheLoader<>() {
46 public ConcreteType load(final Class<?> key) {
47 return new ConcreteTypeImpl(JavaTypeName.create(key), null);
50 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
51 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
53 public static final @NonNull ConcreteType BOOLEAN = typeForClass(Boolean.class);
54 public static final @NonNull ConcreteType STRING = typeForClass(String.class);
55 public static final @NonNull ConcreteType VOID = typeForClass(Void.class);
56 public static final @NonNull ConcreteType BYTE_ARRAY = typeForClass(byte[].class);
58 private static final @NonNull ConcreteType BUILDER = typeForClass(Builder.class);
59 private static final @NonNull ConcreteType CLASS = typeForClass(Class.class);
60 private static final @NonNull ConcreteType LIST_TYPE = typeForClass(List.class);
61 private static final @NonNull ConcreteType LISTENABLE_FUTURE = typeForClass(ListenableFuture.class);
62 private static final @NonNull ConcreteType MAP_TYPE = typeForClass(Map.class);
63 private static final @NonNull ConcreteType OBJECT = typeForClass(Object.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);
68 private static final @NonNull ConcreteType AUGMENTABLE = typeForClass(Augmentable.class);
69 private static final @NonNull ConcreteType AUGMENTATION = typeForClass(Augmentation.class);
72 * It is not desirable to create instance of this class.
78 * Returns an instance of {@link ParameterizedType} which represents JAVA <code>java.lang.Class</code> type
79 * specialized to specified type.
81 * @param type Type for which to specialize
82 * @return A parameterized type corresponding to {@code Class<Type>}
83 * @throws NullPointerException if {@code type} is null
85 public static @NonNull ParameterizedType classType(final Type type) {
86 return parameterizedTypeFor(CLASS, type);
90 * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
92 * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
94 public static @NonNull ConcreteType voidType() {
99 * Returns an instance of {@link ConcreteType} which represents {@link Object} type.
101 * @return <code>ConcreteType</code> instance which represents {@link Object}
103 public static @NonNull ConcreteType objectType() {
108 * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
110 * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
112 public static @NonNull ConcreteType primitiveVoidType() {
113 return PRIMITIVE_VOID;
117 * Returns an instance of {@link ConcreteType} which represents {@link Serializable} type.
119 * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
121 public static @NonNull ConcreteType serializableType() {
126 * Returns an instance of {@link ConcreteType} describing the class.
128 * @param cls Class to describe
129 * @return Description of class
131 public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls) {
132 return TYPE_CACHE.getUnchecked(cls);
135 public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls,
136 final @Nullable Restrictions restrictions) {
137 if (restrictions == null) {
138 return typeForClass(cls);
141 final JavaTypeName identifier = JavaTypeName.create(cls);
142 if (restrictions instanceof DefaultRestrictions) {
143 return new ConcreteTypeImpl(identifier, restrictions);
145 return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
149 * Returns an instance of {@link ParameterizedType} describing the typed {@link Map}<K,V>.
151 * @param keyType Key Type
152 * @param valueType Value Type
153 * @return Description of generic type instance
155 public static @NonNull ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
156 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
160 * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}<V> with concrete type
163 * @param valueType Value Type
164 * @return Description of generic type instance of Set
166 public static @NonNull ParameterizedType setTypeFor(final Type valueType) {
167 return parameterizedTypeFor(SET_TYPE, valueType);
171 * Returns an instance of {@link ParameterizedType} describing the typed {@link List}<V> with concrete type
174 * @param valueType Value Type
175 * @return Description of type instance of List
177 public static @NonNull ParameterizedType listTypeFor(final Type valueType) {
178 return parameterizedTypeFor(LIST_TYPE, valueType);
181 public static boolean isListType(final ParameterizedType type) {
182 return LIST_TYPE.equals(type.getRawType());
185 public static boolean isListType(final Type type) {
186 return type instanceof ParameterizedType && isListType((ParameterizedType) type);
190 * Returns an instance of {@link ParameterizedType} describing the typed {@link ListenableFuture}<V>
191 * with concrete type of value.
193 * @param valueType Value Type
194 * @return Description of type instance of ListenableFuture
196 public static @NonNull ParameterizedType listenableFutureTypeFor(final Type valueType) {
197 return parameterizedTypeFor(LISTENABLE_FUTURE, valueType);
201 * Returns an instance of {@link ParameterizedType} describing the typed
202 * {@link Builder}<V> with concrete type of value.
204 * @param valueType Value Type
205 * @return Description of type instance of Builder
207 public static @NonNull ParameterizedType builderTypeFor(final Type valueType) {
208 return parameterizedTypeFor(BUILDER, valueType);
212 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType}.
214 * @param type JAVA <code>Type</code> for raw type
215 * @param parameters JAVA <code>Type</code>s for actual parameter types
216 * @return <code>ParametrizedType</code> representation of <code>type</code> and its <code>parameters</code>
217 * @throws NullPointerException if any argument or any member of {@code parameters} is null
219 public static @NonNull ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
220 return new ParametrizedTypeImpl(type, parameters);
224 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
226 * @param identifier JavaTypeName of the type
227 * @return <code>WildcardType</code> representation of specified identifier
229 public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
230 return new WildcardTypeImpl(identifier);
234 * Creates instance of
235 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
236 * ParameterizedType} where raw type is
237 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
238 * parameter is <code>valueType</code>.
240 * @param valueType JAVA <code>Type</code> with actual parameter
241 * @return <code>ParametrizedType</code> representation of raw type
242 * <code>Augmentable</code> with actual parameter
243 * <code>valueType</code>
245 public static @NonNull ParameterizedType augmentableTypeFor(final Type valueType) {
246 return parameterizedTypeFor(AUGMENTABLE, valueType);
250 * Creates instance of {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType} where
251 * raw type is {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual parameter
252 * is <code>valueType</code>.
254 * @param valueType JAVA <code>Type</code> with actual parameter
255 * @return <code>ParametrizedType</code> reprezentation of raw type
256 * <code>Augmentation</code> with actual parameter
257 * <code>valueType</code>
259 public static @NonNull ParameterizedType augmentationTypeFor(final Type valueType) {
260 return parameterizedTypeFor(AUGMENTATION, valueType);
263 public static @Nullable String getOuterClassName(final Type valueType) {
264 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
268 * Represents concrete JAVA type.
270 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
271 private final Restrictions restrictions;
274 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
276 * @param pkName string with package name
277 * @param name string with the name of the type
279 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
281 this.restrictions = restrictions;
285 public Restrictions getRestrictions() {
286 return this.restrictions;
291 * Represents concrete JAVA type with changed restriction values.
293 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements
294 BaseTypeWithRestrictions {
295 private final Restrictions restrictions;
298 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
300 * @param pkName string with package name
301 * @param name string with the name of the type
303 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
305 this.restrictions = requireNonNull(restrictions);
309 public Restrictions getRestrictions() {
310 return this.restrictions;
315 * Represents parametrized JAVA type.
317 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
319 * Array of JAVA actual type parameters.
321 private final Type[] actualTypes;
324 * JAVA raw type (like List, Set, Map...).
326 private final Type rawType;
329 * Creates instance of this class with concrete rawType and array of actual parameters.
331 * @param rawType JAVA <code>Type</code> for raw type
332 * @param actTypes array of actual parameters
334 ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
335 super(rawType.getIdentifier());
336 this.rawType = requireNonNull(rawType);
337 actualTypes = actTypes.clone();
338 if (Arrays.stream(actualTypes).anyMatch(Objects::isNull)) {
339 throw new NullPointerException("actTypes contains a null");
344 public Type[] getActualTypeArguments() {
346 return this.actualTypes;
350 public Type getRawType() {
356 * Represents JAVA bounded wildcard type.
358 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
360 * Creates instance of this class with concrete package and type name.
362 * @param packageName string with the package name
363 * @param typeName string with the name of type
365 WildcardTypeImpl(final JavaTypeName identifier) {
370 public static <T extends Number & Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
372 return new DefaultRestrictions<>(min, max);
375 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
376 private final RangeConstraint<?> rangeConstraint;
378 DefaultRestrictions(final T min, final T max) {
379 this.rangeConstraint = new DefaultRangeConstraint<>(min, max);
383 public boolean isEmpty() {
388 public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
389 return Optional.of(rangeConstraint);
393 public List<PatternConstraint> getPatternConstraints() {
394 return Collections.emptyList();
398 public Optional<LengthConstraint> getLengthConstraint() {
399 return Optional.empty();
403 private static final class DefaultRangeConstraint<T extends Number & Comparable<T>> implements RangeConstraint<T> {
407 DefaultRangeConstraint(final T min, final T max) {
408 this.min = requireNonNull(min);
409 this.max = requireNonNull(max);
413 public Optional<String> getErrorAppTag() {
414 return Optional.empty();
418 public Optional<String> getErrorMessage() {
419 return Optional.empty();
423 public Optional<String> getDescription() {
424 return Optional.empty();
428 public Optional<String> getReference() {
429 return Optional.empty();
433 public RangeSet<T> getAllowedRanges() {
434 return ImmutableRangeSet.of(Range.closed(min, max));