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 com.google.common.base.Preconditions;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14 import com.google.common.collect.ImmutableRangeSet;
15 import com.google.common.collect.Range;
16 import com.google.common.collect.RangeSet;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.io.Serializable;
19 import java.util.Collections;
20 import java.util.List;
22 import java.util.Optional;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
26 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
27 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
28 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
29 import org.opendaylight.mdsal.binding.model.api.Restrictions;
30 import org.opendaylight.mdsal.binding.model.api.Type;
31 import org.opendaylight.mdsal.binding.model.api.WildcardType;
32 import org.opendaylight.yangtools.yang.binding.Augmentable;
33 import org.opendaylight.yangtools.yang.binding.Augmentation;
34 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
35 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
36 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
38 public final class Types {
39 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
40 new CacheLoader<Class<?>, ConcreteType>() {
42 public ConcreteType load(final Class<?> key) {
43 return new ConcreteTypeImpl(JavaTypeName.create(key), null);
46 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
47 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
49 public static final Type SET_TYPE = typeForClass(Set.class);
50 public static final Type LIST_TYPE = typeForClass(List.class);
51 public static final Type MAP_TYPE = typeForClass(Map.class);
53 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
54 public static final ConcreteType FUTURE = typeForClass(ListenableFuture.class);
55 public static final ConcreteType STRING = typeForClass(String.class);
56 public static final ConcreteType VOID = typeForClass(Void.class);
57 public static final ConcreteType BYTE_ARRAY = typeForClass(byte[].class);
58 public static final ConcreteType CHAR_ARRAY = typeForClass(char[].class);
60 private static final ConcreteType CLASS = typeForClass(Class.class);
61 private static final ConcreteType OBJECT = typeForClass(Object.class);
62 private static final ConcreteType PRIMITIVE_VOID = typeForClass(void.class);
63 private static final ConcreteType SERIALIZABLE = typeForClass(Serializable.class);
66 * It is not desirable to create instance of this class
72 * Returns an instance of {@link ParameterizedType} which represents JAVA <code>java.lang.Class</code> type
73 * specialized to specified type.
75 * @param type Type for which to specialize
76 * @return A parameterized type corresponding to {@code Class<Type>}
77 * @throws NullPointerException if {@code type} is null
79 public static ParameterizedType classType(final Type type) {
80 return parameterizedTypeFor(CLASS, type);
84 * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
86 * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
88 public static ConcreteType voidType() {
93 * Returns an instance of {@link ConcreteType} which represents {@link Object} type.
95 * @return <code>ConcreteType</code> instance which represents {@link Object}
97 public static ConcreteType objectType() {
102 * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
104 * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
106 public static ConcreteType primitiveVoidType() {
107 return PRIMITIVE_VOID;
111 * Returns an instance of {@link ConcreteType} which represents {@link Serializable} type.
113 * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
115 public static ConcreteType serializableType() {
120 * Returns an instance of {@link ConcreteType} describing the class
124 * @return Description of class
126 public static ConcreteType typeForClass(final Class<?> cls) {
127 return TYPE_CACHE.getUnchecked(cls);
130 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
131 if (restrictions == null) {
132 return typeForClass(cls);
135 final JavaTypeName identifier = JavaTypeName.create(cls);
136 if (restrictions instanceof DefaultRestrictions) {
137 return new ConcreteTypeImpl(identifier, restrictions);
139 return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
143 * Returns an instance of {@link ParameterizedType} describing the typed
144 * {@link Map}<K,V>
150 * @return Description of generic type instance
152 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
153 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
157 * Returns an instance of {@link ParameterizedType} describing the typed
158 * {@link Set}<V> with concrete type of value.
162 * @return Description of generic type instance of Set
164 public static ParameterizedType setTypeFor(final Type valueType) {
165 return parameterizedTypeFor(SET_TYPE, valueType);
169 * Returns an instance of {@link ParameterizedType} describing the typed
170 * {@link List}<V> with concrete type of value.
174 * @return Description of type instance of List
176 public static ParameterizedType listTypeFor(final Type valueType) {
177 return parameterizedTypeFor(LIST_TYPE, valueType);
181 * Creates instance of type
182 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
186 * JAVA <code>Type</code> for raw type
188 * JAVA <code>Type</code>s for actual parameter types
189 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
190 * and its parameters <code>parameters</code>
191 * @throws NullPointerException if any argument is null
193 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
194 return new ParametrizedTypeImpl(type, parameters);
198 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
200 * @param identifier JavaTypeName of the type
201 * @return <code>WildcardType</code> representation of specified identifier
203 public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
204 return new WildcardTypeImpl(identifier);
208 * Creates instance of
209 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
210 * ParameterizedType} where raw type is
211 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
212 * parameter is <code>valueType</code>.
215 * JAVA <code>Type</code> with actual parameter
216 * @return <code>ParametrizedType</code> representation of raw type
217 * <code>Augmentable</code> with actual parameter
218 * <code>valueType</code>
220 public static ParameterizedType augmentableTypeFor(final Type valueType) {
221 final Type augmentable = typeForClass(Augmentable.class);
222 return parameterizedTypeFor(augmentable, valueType);
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.Augmentation} and actual
230 * parameter is <code>valueType</code>.
233 * JAVA <code>Type</code> with actual parameter
234 * @return <code>ParametrizedType</code> reprezentation of raw type
235 * <code>Augmentation</code> with actual parameter
236 * <code>valueType</code>
238 public static ParameterizedType augmentationTypeFor(final Type valueType) {
239 final Type augmentation = typeForClass(Augmentation.class);
240 return parameterizedTypeFor(augmentation, valueType);
243 public static @Nullable String getOuterClassName(final Type valueType) {
244 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
249 * Represents concrete JAVA type.
252 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
253 private final Restrictions restrictions;
256 * Creates instance of this class with package <code>pkName</code> and
257 * with the type name <code>name</code>.
260 * string with package name
262 * string with the name of the type
264 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
266 this.restrictions = restrictions;
270 public Restrictions getRestrictions() {
271 return this.restrictions;
276 * Represents concrete JAVA type with changed restriction values.
278 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
279 private final Restrictions restrictions;
282 * Creates instance of this class with package <code>pkName</code> and
283 * with the type name <code>name</code>.
286 * string with package name
288 * string with the name of the type
290 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
292 this.restrictions = Preconditions.checkNotNull(restrictions);
296 public Restrictions getRestrictions() {
297 return this.restrictions;
302 * Represents parametrized JAVA type.
304 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
306 * Array of JAVA actual type parameters.
308 private final Type[] actualTypes;
311 * JAVA raw type (like List, Set, Map...)
313 private final Type rawType;
316 public Type[] getActualTypeArguments() {
318 return this.actualTypes;
322 public Type getRawType() {
327 * Creates instance of this class with concrete rawType and array of
331 * JAVA <code>Type</code> for raw type
333 * array of actual parameters
335 public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
336 super(rawType.getIdentifier());
337 this.rawType = rawType;
338 this.actualTypes = actTypes.clone();
343 * Represents JAVA bounded wildcard type.
345 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
347 * Creates instance of this class with concrete package and type name.
350 * string with the package name
352 * string with the name of type
354 WildcardTypeImpl(final JavaTypeName identifier) {
359 public static <T extends Number& Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
361 return new DefaultRestrictions<>(min, max);
364 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
367 private final RangeConstraint<?> rangeConstraint;
369 private DefaultRestrictions(final T min, final T max) {
370 this.min = Preconditions.checkNotNull(min);
371 this.max = Preconditions.checkNotNull(max);
373 this.rangeConstraint = new RangeConstraint<T>() {
376 public Optional<String> getErrorAppTag() {
377 return Optional.empty();
381 public Optional<String> getErrorMessage() {
382 return Optional.empty();
386 public Optional<String> getDescription() {
387 return Optional.empty();
391 public Optional<String> getReference() {
392 return Optional.empty();
396 public RangeSet<T> getAllowedRanges() {
397 return ImmutableRangeSet.of(Range.closed(min, max));
403 public boolean isEmpty() {
408 public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
409 return Optional.of(rangeConstraint);
413 public List<PatternConstraint> getPatternConstraints() {
414 return Collections.emptyList();
418 public Optional<LengthConstraint> getLengthConstraint() {
419 return Optional.empty();