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.util.Collections;
19 import java.util.List;
21 import java.util.Optional;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
25 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
26 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
27 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
28 import org.opendaylight.mdsal.binding.model.api.Restrictions;
29 import org.opendaylight.mdsal.binding.model.api.Type;
30 import org.opendaylight.mdsal.binding.model.api.WildcardType;
31 import org.opendaylight.yangtools.yang.binding.Augmentable;
32 import org.opendaylight.yangtools.yang.binding.Augmentation;
33 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
34 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
35 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
37 public final class Types {
38 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
39 new CacheLoader<Class<?>, ConcreteType>() {
41 public ConcreteType load(final Class<?> key) {
42 return new ConcreteTypeImpl(JavaTypeName.create(key), null);
45 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
46 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
48 public static final Type SET_TYPE = typeForClass(Set.class);
49 public static final Type LIST_TYPE = typeForClass(List.class);
50 public static final Type MAP_TYPE = typeForClass(Map.class);
52 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
53 public static final ConcreteType FUTURE = typeForClass(ListenableFuture.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);
57 public static final ConcreteType CHAR_ARRAY = typeForClass(char[].class);
59 private static final ConcreteType PRIMITIVE_VOID = typeForClass(void.class);
62 * It is not desirable to create instance of this class
68 * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
70 * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
72 public static ConcreteType voidType() {
77 * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
79 * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
81 public static ConcreteType primitiveVoidType() {
82 return PRIMITIVE_VOID;
86 * Returns an instance of {@link ConcreteType} describing the class
90 * @return Description of class
92 public static ConcreteType typeForClass(final Class<?> cls) {
93 return TYPE_CACHE.getUnchecked(cls);
96 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
97 if (restrictions == null) {
98 return typeForClass(cls);
101 final JavaTypeName identifier = JavaTypeName.create(cls);
102 if (restrictions instanceof DefaultRestrictions) {
103 return new ConcreteTypeImpl(identifier, restrictions);
105 return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
109 * Returns an instance of {@link ParameterizedType} describing the typed
110 * {@link Map}<K,V>
116 * @return Description of generic type instance
118 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
119 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
123 * Returns an instance of {@link ParameterizedType} describing the typed
124 * {@link Set}<V> with concrete type of value.
128 * @return Description of generic type instance of Set
130 public static ParameterizedType setTypeFor(final Type valueType) {
131 return parameterizedTypeFor(SET_TYPE, valueType);
135 * Returns an instance of {@link ParameterizedType} describing the typed
136 * {@link List}<V> with concrete type of value.
140 * @return Description of type instance of List
142 public static ParameterizedType listTypeFor(final Type valueType) {
143 return parameterizedTypeFor(LIST_TYPE, valueType);
147 * Creates instance of type
148 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
152 * JAVA <code>Type</code> for raw type
154 * JAVA <code>Type</code>s for actual parameter types
155 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
156 * and its parameters <code>parameters</code>
157 * @throws NullPointerException if any argument is null
159 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
160 return new ParametrizedTypeImpl(type, parameters);
164 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
166 * @param identifier JavaTypeName of the type
167 * @return <code>WildcardType</code> representation of specified identifier
169 public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
170 return new WildcardTypeImpl(identifier);
174 * Creates instance of
175 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
176 * ParameterizedType} where raw type is
177 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
178 * parameter is <code>valueType</code>.
181 * JAVA <code>Type</code> with actual parameter
182 * @return <code>ParametrizedType</code> representation of raw type
183 * <code>Augmentable</code> with actual parameter
184 * <code>valueType</code>
186 public static ParameterizedType augmentableTypeFor(final Type valueType) {
187 final Type augmentable = typeForClass(Augmentable.class);
188 return parameterizedTypeFor(augmentable, valueType);
192 * Creates instance of
193 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
194 * ParameterizedType} where raw type is
195 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
196 * parameter is <code>valueType</code>.
199 * JAVA <code>Type</code> with actual parameter
200 * @return <code>ParametrizedType</code> reprezentation of raw type
201 * <code>Augmentation</code> with actual parameter
202 * <code>valueType</code>
204 public static ParameterizedType augmentationTypeFor(final Type valueType) {
205 final Type augmentation = typeForClass(Augmentation.class);
206 return parameterizedTypeFor(augmentation, valueType);
209 public static @Nullable String getOuterClassName(final Type valueType) {
210 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
215 * Represents concrete JAVA type.
218 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
219 private final Restrictions restrictions;
222 * Creates instance of this class with package <code>pkName</code> and
223 * with the type name <code>name</code>.
226 * string with package name
228 * string with the name of the type
230 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
232 this.restrictions = restrictions;
236 public Restrictions getRestrictions() {
237 return this.restrictions;
242 * Represents concrete JAVA type with changed restriction values.
244 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
245 private final Restrictions restrictions;
248 * Creates instance of this class with package <code>pkName</code> and
249 * with the type name <code>name</code>.
252 * string with package name
254 * string with the name of the type
256 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
258 this.restrictions = Preconditions.checkNotNull(restrictions);
262 public Restrictions getRestrictions() {
263 return this.restrictions;
268 * Represents parametrized JAVA type.
270 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
272 * Array of JAVA actual type parameters.
274 private final Type[] actualTypes;
277 * JAVA raw type (like List, Set, Map...)
279 private final Type rawType;
282 public Type[] getActualTypeArguments() {
284 return this.actualTypes;
288 public Type getRawType() {
293 * Creates instance of this class with concrete rawType and array of
297 * JAVA <code>Type</code> for raw type
299 * array of actual parameters
301 public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
302 super(rawType.getIdentifier());
303 this.rawType = rawType;
304 this.actualTypes = actTypes.clone();
309 * Represents JAVA bounded wildcard type.
311 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
313 * Creates instance of this class with concrete package and type name.
316 * string with the package name
318 * string with the name of type
320 WildcardTypeImpl(final JavaTypeName identifier) {
325 public static <T extends Number& Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
327 return new DefaultRestrictions<>(min, max);
330 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
333 private final RangeConstraint<?> rangeConstraint;
335 private DefaultRestrictions(final T min, final T max) {
336 this.min = Preconditions.checkNotNull(min);
337 this.max = Preconditions.checkNotNull(max);
339 this.rangeConstraint = new RangeConstraint<T>() {
342 public Optional<String> getErrorAppTag() {
343 return Optional.empty();
347 public Optional<String> getErrorMessage() {
348 return Optional.empty();
352 public Optional<String> getDescription() {
353 return Optional.empty();
357 public Optional<String> getReference() {
358 return Optional.empty();
362 public RangeSet<T> getAllowedRanges() {
363 return ImmutableRangeSet.of(Range.closed(min, max));
369 public boolean isEmpty() {
374 public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
375 return Optional.of(rangeConstraint);
379 public List<PatternConstraint> getPatternConstraints() {
380 return Collections.emptyList();
384 public Optional<LengthConstraint> getLengthConstraint() {
385 return Optional.empty();