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.base.Preconditions;
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.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.yang.binding.Augmentable;
37 import org.opendaylight.yangtools.yang.binding.Augmentation;
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<Class<?>, ConcreteType>() {
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);
53 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
54 public static final ConcreteType STRING = typeForClass(String.class);
55 public static final ConcreteType VOID = typeForClass(Void.class);
56 public static final ConcreteType BYTE_ARRAY = typeForClass(byte[].class);
58 private static final ConcreteType CLASS = typeForClass(Class.class);
59 private static final ConcreteType LIST_TYPE = typeForClass(List.class);
60 private static final ConcreteType LISTENABLE_FUTURE = typeForClass(ListenableFuture.class);
61 private static final ConcreteType MAP_TYPE = typeForClass(Map.class);
62 private static final ConcreteType OBJECT = typeForClass(Object.class);
63 private static final ConcreteType PRIMITIVE_VOID = typeForClass(void.class);
64 private static final ConcreteType SERIALIZABLE = typeForClass(Serializable.class);
65 private static final ConcreteType SET_TYPE = typeForClass(Set.class);
68 * It is not desirable to create instance of this class
74 * Returns an instance of {@link ParameterizedType} which represents JAVA <code>java.lang.Class</code> type
75 * specialized to specified type.
77 * @param type Type for which to specialize
78 * @return A parameterized type corresponding to {@code Class<Type>}
79 * @throws NullPointerException if {@code type} is null
81 public static ParameterizedType classType(final Type type) {
82 return parameterizedTypeFor(CLASS, type);
86 * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
88 * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
90 public static ConcreteType voidType() {
95 * Returns an instance of {@link ConcreteType} which represents {@link Object} type.
97 * @return <code>ConcreteType</code> instance which represents {@link Object}
99 public static ConcreteType objectType() {
104 * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
106 * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
108 public static ConcreteType primitiveVoidType() {
109 return PRIMITIVE_VOID;
113 * Returns an instance of {@link ConcreteType} which represents {@link Serializable} type.
115 * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
117 public static ConcreteType serializableType() {
122 * Returns an instance of {@link ConcreteType} describing the class
126 * @return Description of class
128 public static ConcreteType typeForClass(final Class<?> cls) {
129 return TYPE_CACHE.getUnchecked(cls);
132 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
133 if (restrictions == null) {
134 return typeForClass(cls);
137 final JavaTypeName identifier = JavaTypeName.create(cls);
138 if (restrictions instanceof DefaultRestrictions) {
139 return new ConcreteTypeImpl(identifier, restrictions);
141 return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
145 * Returns an instance of {@link ParameterizedType} describing the typed
146 * {@link Map}<K,V>
152 * @return Description of generic type instance
154 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
155 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
159 * Returns an instance of {@link ParameterizedType} describing the typed
160 * {@link Set}<V> with concrete type of value.
164 * @return Description of generic type instance of Set
166 public static ParameterizedType setTypeFor(final Type valueType) {
167 return parameterizedTypeFor(SET_TYPE, valueType);
171 * Returns an instance of {@link ParameterizedType} describing the typed
172 * {@link List}<V> with concrete type of value.
176 * @return Description of type instance of List
178 public static ParameterizedType listTypeFor(final Type valueType) {
179 return parameterizedTypeFor(LIST_TYPE, valueType);
182 public static boolean isListType(final Type type) {
183 return type instanceof ParameterizedType && LIST_TYPE.equals(((ParameterizedType) type).getRawType());
187 * Returns an instance of {@link ParameterizedType} describing the typed
188 * {@link ListenableFuture}<V> with concrete type of value.
192 * @return Description of type instance of ListenableFuture
194 public static ParameterizedType listenableFutureTypeFor(final Type valueType) {
195 return parameterizedTypeFor(LISTENABLE_FUTURE, valueType);
199 * Creates instance of type
200 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
204 * JAVA <code>Type</code> for raw type
206 * JAVA <code>Type</code>s for actual parameter types
207 * @return <code>ParametrizedType</code> representation of <code>type</code>
208 * and its parameters <code>parameters</code>
209 * @throws NullPointerException if any argument or any member of {@code parameters} is null
211 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
212 return new ParametrizedTypeImpl(type, parameters);
216 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
218 * @param identifier JavaTypeName of the type
219 * @return <code>WildcardType</code> representation of specified identifier
221 public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
222 return new WildcardTypeImpl(identifier);
226 * Creates instance of
227 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
228 * ParameterizedType} where raw type is
229 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
230 * parameter is <code>valueType</code>.
233 * JAVA <code>Type</code> with actual parameter
234 * @return <code>ParametrizedType</code> representation of raw type
235 * <code>Augmentable</code> with actual parameter
236 * <code>valueType</code>
238 public static ParameterizedType augmentableTypeFor(final Type valueType) {
239 final Type augmentable = typeForClass(Augmentable.class);
240 return parameterizedTypeFor(augmentable, valueType);
244 * Creates instance of
245 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
246 * ParameterizedType} where raw type is
247 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
248 * parameter is <code>valueType</code>.
251 * JAVA <code>Type</code> with actual parameter
252 * @return <code>ParametrizedType</code> reprezentation of raw type
253 * <code>Augmentation</code> with actual parameter
254 * <code>valueType</code>
256 public static ParameterizedType augmentationTypeFor(final Type valueType) {
257 final Type augmentation = typeForClass(Augmentation.class);
258 return parameterizedTypeFor(augmentation, valueType);
261 public static @Nullable String getOuterClassName(final Type valueType) {
262 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
267 * 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
275 * with the type name <code>name</code>.
278 * string with package name
280 * string with the name of the type
282 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
284 this.restrictions = restrictions;
288 public Restrictions getRestrictions() {
289 return this.restrictions;
294 * Represents concrete JAVA type with changed restriction values.
296 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
297 private final Restrictions restrictions;
300 * Creates instance of this class with package <code>pkName</code> and
301 * with the type name <code>name</code>.
304 * string with package name
306 * string with the name of the type
308 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
310 this.restrictions = Preconditions.checkNotNull(restrictions);
314 public Restrictions getRestrictions() {
315 return this.restrictions;
320 * Represents parametrized JAVA type.
322 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
324 * Array of JAVA actual type parameters.
326 private final Type[] actualTypes;
329 * JAVA raw type (like List, Set, Map...)
331 private final Type rawType;
334 public Type[] getActualTypeArguments() {
336 return this.actualTypes;
340 public Type getRawType() {
345 * Creates instance of this class with concrete rawType and array of
349 * JAVA <code>Type</code> for raw type
351 * array of actual parameters
353 public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
354 super(rawType.getIdentifier());
355 this.rawType = requireNonNull(rawType);
356 actualTypes = actTypes.clone();
357 if (Arrays.stream(actualTypes).anyMatch(Objects::isNull)) {
358 throw new NullPointerException("actTypes contains a null");
364 * Represents JAVA bounded wildcard type.
366 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
368 * Creates instance of this class with concrete package and type name.
371 * string with the package name
373 * string with the name of type
375 WildcardTypeImpl(final JavaTypeName identifier) {
380 public static <T extends Number& Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
382 return new DefaultRestrictions<>(min, max);
385 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
388 private final RangeConstraint<?> rangeConstraint;
390 private DefaultRestrictions(final T min, final T max) {
391 this.min = Preconditions.checkNotNull(min);
392 this.max = Preconditions.checkNotNull(max);
394 this.rangeConstraint = new RangeConstraint<T>() {
397 public Optional<String> getErrorAppTag() {
398 return Optional.empty();
402 public Optional<String> getErrorMessage() {
403 return Optional.empty();
407 public Optional<String> getDescription() {
408 return Optional.empty();
412 public Optional<String> getReference() {
413 return Optional.empty();
417 public RangeSet<T> getAllowedRanges() {
418 return ImmutableRangeSet.of(Range.closed(min, max));
424 public boolean isEmpty() {
429 public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
430 return Optional.of(rangeConstraint);
434 public List<PatternConstraint> getPatternConstraints() {
435 return Collections.emptyList();
439 public Optional<LengthConstraint> getLengthConstraint() {
440 return Optional.empty();