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.AbstractBaseType;
30 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
31 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
32 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
33 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
34 import org.opendaylight.mdsal.binding.model.api.Restrictions;
35 import org.opendaylight.mdsal.binding.model.api.Type;
36 import org.opendaylight.mdsal.binding.model.api.WildcardType;
37 import org.opendaylight.yangtools.concepts.Builder;
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 STRING = typeForClass(String.class);
54 public static final @NonNull ConcreteType VOID = typeForClass(Void.class);
55 public static final @NonNull ConcreteType BYTE_ARRAY = typeForClass(byte[].class);
57 private static final @NonNull ConcreteType BUILDER = typeForClass(Builder.class);
58 private static final @NonNull ConcreteType CLASS = typeForClass(Class.class);
59 private static final @NonNull ConcreteType LIST_TYPE = typeForClass(List.class);
60 private static final @NonNull ConcreteType LISTENABLE_FUTURE = typeForClass(ListenableFuture.class);
61 private static final @NonNull ConcreteType MAP_TYPE = typeForClass(Map.class);
62 private static final @NonNull ConcreteType OBJECT = typeForClass(Object.class);
63 private static final @NonNull ConcreteType PRIMITIVE_VOID = typeForClass(void.class);
64 private static final @NonNull ConcreteType SERIALIZABLE = typeForClass(Serializable.class);
65 private static final @NonNull 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 @NonNull 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 @NonNull 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 @NonNull 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 @NonNull 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 @NonNull ConcreteType serializableType() {
122 * Returns an instance of {@link ConcreteType} describing the class.
124 * @param cls Class to describe
125 * @return Description of class
127 public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls) {
128 return TYPE_CACHE.getUnchecked(cls);
131 public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls,
132 final @Nullable 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 {@link Map}<K,V>.
147 * @param keyType Key Type
148 * @param valueType Value Type
149 * @return Description of generic type instance
151 public static @NonNull ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
152 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
155 public static boolean isMapType(final ParameterizedType type) {
156 return MAP_TYPE.equals(type.getRawType());
159 public static boolean isMapType(final Type type) {
160 return type instanceof ParameterizedType && isMapType((ParameterizedType) type);
164 * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}<V> with concrete type
167 * @param valueType Value Type
168 * @return Description of generic type instance of Set
170 public static @NonNull ParameterizedType setTypeFor(final Type valueType) {
171 return parameterizedTypeFor(SET_TYPE, valueType);
175 * Returns an instance of {@link ParameterizedType} describing the typed {@link List}<V> with concrete type
178 * @param valueType Value Type
179 * @return Description of type instance of List
181 public static @NonNull ParameterizedType listTypeFor(final Type valueType) {
182 return parameterizedTypeFor(LIST_TYPE, valueType);
185 public static boolean isListType(final ParameterizedType type) {
186 return LIST_TYPE.equals(type.getRawType());
189 public static boolean isListType(final Type type) {
190 return type instanceof ParameterizedType && isListType((ParameterizedType) type);
194 * Returns an instance of {@link ParameterizedType} describing the typed {@link ListenableFuture}<V>
195 * with concrete type of value.
197 * @param valueType Value Type
198 * @return Description of type instance of ListenableFuture
200 public static @NonNull ParameterizedType listenableFutureTypeFor(final Type valueType) {
201 return parameterizedTypeFor(LISTENABLE_FUTURE, valueType);
205 * Returns an instance of {@link ParameterizedType} describing the typed
206 * {@link Builder}<V> with concrete type of value.
208 * @param valueType Value Type
209 * @return Description of type instance of Builder
211 public static @NonNull ParameterizedType builderTypeFor(final Type valueType) {
212 return parameterizedTypeFor(BUILDER, valueType);
216 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType}.
218 * @param type JAVA <code>Type</code> for raw type
219 * @param parameters JAVA <code>Type</code>s for actual parameter types
220 * @return <code>ParametrizedType</code> representation of <code>type</code> and its <code>parameters</code>
221 * @throws NullPointerException if any argument or any member of {@code parameters} is null
223 public static @NonNull ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
224 return new ParametrizedTypeImpl(type, parameters);
228 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
230 * @param identifier JavaTypeName of the type
231 * @return <code>WildcardType</code> representation of specified identifier
233 public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
234 return new WildcardTypeImpl(identifier);
237 public static @Nullable String getOuterClassName(final Type valueType) {
238 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
242 * Represents concrete JAVA type.
244 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
245 private final Restrictions restrictions;
248 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
250 * @param pkName string with package name
251 * @param name string with the name of the type
253 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
255 this.restrictions = restrictions;
259 public Restrictions getRestrictions() {
260 return this.restrictions;
265 * Represents concrete JAVA type with changed restriction values.
267 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements
268 BaseTypeWithRestrictions {
269 private final Restrictions restrictions;
272 * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
274 * @param pkName string with package name
275 * @param name string with the name of the type
277 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
279 this.restrictions = requireNonNull(restrictions);
283 public Restrictions getRestrictions() {
284 return this.restrictions;
289 * Represents parametrized JAVA type.
291 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
293 * Array of JAVA actual type parameters.
295 private final Type[] actualTypes;
298 * JAVA raw type (like List, Set, Map...).
300 private final Type rawType;
303 * Creates instance of this class with concrete rawType and array of actual parameters.
305 * @param rawType JAVA <code>Type</code> for raw type
306 * @param actTypes array of actual parameters
308 ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
309 super(rawType.getIdentifier());
310 this.rawType = requireNonNull(rawType);
311 actualTypes = actTypes.clone();
312 if (Arrays.stream(actualTypes).anyMatch(Objects::isNull)) {
313 throw new NullPointerException("actTypes contains a null");
318 public Type[] getActualTypeArguments() {
320 return this.actualTypes;
324 public Type getRawType() {
330 * Represents JAVA bounded wildcard type.
332 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
334 * Creates instance of this class with concrete package and type name.
336 * @param packageName string with the package name
337 * @param typeName string with the name of type
339 WildcardTypeImpl(final JavaTypeName identifier) {
344 public static <T extends Number & Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
346 return new DefaultRestrictions<>(min, max);
349 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
350 private final RangeConstraint<?> rangeConstraint;
352 DefaultRestrictions(final T min, final T max) {
353 this.rangeConstraint = new DefaultRangeConstraint<>(min, max);
357 public boolean isEmpty() {
362 public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
363 return Optional.of(rangeConstraint);
367 public List<PatternConstraint> getPatternConstraints() {
368 return Collections.emptyList();
372 public Optional<LengthConstraint> getLengthConstraint() {
373 return Optional.empty();
377 private static final class DefaultRangeConstraint<T extends Number & Comparable<T>> implements RangeConstraint<T> {
381 DefaultRangeConstraint(final T min, final T max) {
382 this.min = requireNonNull(min);
383 this.max = requireNonNull(max);
387 public Optional<String> getErrorAppTag() {
388 return Optional.empty();
392 public Optional<String> getErrorMessage() {
393 return Optional.empty();
397 public Optional<String> getDescription() {
398 return Optional.empty();
402 public Optional<String> getReference() {
403 return Optional.empty();
407 public RangeSet<T> getAllowedRanges() {
408 return ImmutableRangeSet.of(Range.closed(min, max));