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>
158 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
159 return new ParametrizedTypeImpl(type, parameters);
163 * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
165 * @param identifier JavaTypeName of the type
166 * @return <code>WildcardType</code> representation of specified identifier
168 public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
169 return new WildcardTypeImpl(identifier);
173 * Creates instance of
174 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
175 * ParameterizedType} where raw type is
176 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
177 * parameter is <code>valueType</code>.
180 * JAVA <code>Type</code> with actual parameter
181 * @return <code>ParametrizedType</code> representation of raw type
182 * <code>Augmentable</code> with actual parameter
183 * <code>valueType</code>
185 public static ParameterizedType augmentableTypeFor(final Type valueType) {
186 final Type augmentable = typeForClass(Augmentable.class);
187 return parameterizedTypeFor(augmentable, valueType);
191 * Creates instance of
192 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
193 * ParameterizedType} where raw type is
194 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
195 * parameter is <code>valueType</code>.
198 * JAVA <code>Type</code> with actual parameter
199 * @return <code>ParametrizedType</code> reprezentation of raw type
200 * <code>Augmentation</code> with actual parameter
201 * <code>valueType</code>
203 public static ParameterizedType augmentationTypeFor(final Type valueType) {
204 final Type augmentation = typeForClass(Augmentation.class);
205 return parameterizedTypeFor(augmentation, valueType);
208 public static @Nullable String getOuterClassName(final Type valueType) {
209 return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
214 * Represents concrete JAVA type.
217 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
218 private final Restrictions restrictions;
221 * Creates instance of this class with package <code>pkName</code> and
222 * with the type name <code>name</code>.
225 * string with package name
227 * string with the name of the type
229 ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
231 this.restrictions = restrictions;
235 public Restrictions getRestrictions() {
236 return this.restrictions;
241 * Represents concrete JAVA type with changed restriction values.
243 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
244 private final Restrictions restrictions;
247 * Creates instance of this class with package <code>pkName</code> and
248 * with the type name <code>name</code>.
251 * string with package name
253 * string with the name of the type
255 BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
257 this.restrictions = Preconditions.checkNotNull(restrictions);
261 public Restrictions getRestrictions() {
262 return this.restrictions;
267 * Represents parametrized JAVA type.
269 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
271 * Array of JAVA actual type parameters.
273 private final Type[] actualTypes;
276 * JAVA raw type (like List, Set, Map...)
278 private final Type rawType;
281 public Type[] getActualTypeArguments() {
283 return this.actualTypes;
287 public Type getRawType() {
292 * Creates instance of this class with concrete rawType and array of
296 * JAVA <code>Type</code> for raw type
298 * array of actual parameters
300 public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
301 super(rawType.getIdentifier());
302 this.rawType = rawType;
303 this.actualTypes = actTypes.clone();
308 * Represents JAVA bounded wildcard type.
310 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
312 * Creates instance of this class with concrete package and type name.
315 * string with the package name
317 * string with the name of type
319 WildcardTypeImpl(final JavaTypeName identifier) {
324 public static <T extends Number& Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
326 return new DefaultRestrictions<>(min, max);
329 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
332 private final RangeConstraint<?> rangeConstraint;
334 private DefaultRestrictions(final T min, final T max) {
335 this.min = Preconditions.checkNotNull(min);
336 this.max = Preconditions.checkNotNull(max);
338 this.rangeConstraint = new RangeConstraint<T>() {
341 public Optional<String> getErrorAppTag() {
342 return Optional.empty();
346 public Optional<String> getErrorMessage() {
347 return Optional.empty();
351 public Optional<String> getDescription() {
352 return Optional.empty();
356 public Optional<String> getReference() {
357 return Optional.empty();
361 public RangeSet<T> getAllowedRanges() {
362 return ImmutableRangeSet.of(Range.closed(min, max));
368 public boolean isEmpty() {
373 public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
374 return Optional.of(rangeConstraint);
378 public List<PatternConstraint> getPatternConstraints() {
379 return Collections.emptyList();
383 public Optional<LengthConstraint> getLengthConstraint() {
384 return Optional.empty();