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);
236 public static @Nullable String getOuterClassName(final Type valueType) {
237 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
241 * Represents concrete JAVA type.
243 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
244 private final Restrictions restrictions;
247 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
249 * @param pkName string with package name
250 * @param name string with the name of the type
252 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
254 this.restrictions = restrictions;
258 public Restrictions getRestrictions() {
259 return this.restrictions;
264 * Represents concrete JAVA type with changed restriction values.
266 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements
267 BaseTypeWithRestrictions {
268 private final Restrictions restrictions;
271 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
273 * @param pkName string with package name
274 * @param name string with the name of the type
276 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
278 this.restrictions = requireNonNull(restrictions);
282 public Restrictions getRestrictions() {
283 return this.restrictions;
288 * Represents parametrized JAVA type.
290 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
292 * Array of JAVA actual type parameters.
294 private final Type[] actualTypes;
297 * JAVA raw type (like List, Set, Map...).
299 private final Type rawType;
302 * Creates instance of this class with concrete rawType and array of actual parameters.
304 * @param rawType JAVA <code>Type</code> for raw type
305 * @param actTypes array of actual parameters
307 ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
308 super(rawType.getIdentifier());
309 this.rawType = requireNonNull(rawType);
310 actualTypes = actTypes.clone();
311 if (Arrays.stream(actualTypes).anyMatch(Objects::isNull)) {
312 throw new NullPointerException("actTypes contains a null");
317 public Type[] getActualTypeArguments() {
319 return this.actualTypes;
323 public Type getRawType() {
329 * Represents JAVA bounded wildcard type.
331 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
333 * Creates instance of this class with concrete package and type name.
335 * @param packageName string with the package name
336 * @param typeName string with the name of type
338 WildcardTypeImpl(final JavaTypeName identifier) {
343 public static <T extends Number & Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
345 return new DefaultRestrictions<>(min, max);
348 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
349 private final RangeConstraint<?> rangeConstraint;
351 DefaultRestrictions(final T min, final T max) {
352 this.rangeConstraint = new DefaultRangeConstraint<>(min, max);
356 public boolean isEmpty() {
361 public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
362 return Optional.of(rangeConstraint);
366 public List<PatternConstraint> getPatternConstraints() {
367 return Collections.emptyList();
371 public Optional<LengthConstraint> getLengthConstraint() {
372 return Optional.empty();
376 private static final class DefaultRangeConstraint<T extends Number & Comparable<T>> implements RangeConstraint<T> {
380 DefaultRangeConstraint(final T min, final T max) {
381 this.min = requireNonNull(min);
382 this.max = requireNonNull(max);
386 public Optional<String> getErrorAppTag() {
387 return Optional.empty();
391 public Optional<String> getErrorMessage() {
392 return Optional.empty();
396 public Optional<String> getDescription() {
397 return Optional.empty();
401 public Optional<String> getReference() {
402 return Optional.empty();
406 public RangeSet<T> getAllowedRanges() {
407 return ImmutableRangeSet.of(Range.closed(min, max));