2 * Copyright (c) 2017 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
9 package org.opendaylight.mdsal.binding.javav2.generator.util;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.CharMatcher;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Splitter;
16 import com.google.common.cache.CacheBuilder;
17 import com.google.common.cache.CacheLoader;
18 import com.google.common.cache.LoadingCache;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Iterables;
21 import java.util.Collections;
22 import java.util.List;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.opendaylight.mdsal.binding.javav2.model.api.BaseTypeWithRestrictions;
28 import org.opendaylight.mdsal.binding.javav2.model.api.ConcreteType;
29 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
30 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
31 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
32 import org.opendaylight.mdsal.binding.javav2.model.api.WildcardType;
33 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
34 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
35 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
36 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
37 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
38 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
41 public final class Types {
42 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
43 new CacheLoader<Class<?>, ConcreteType>() {
46 public ConcreteType load(@Nonnull final Class<?> key) throws Exception {
47 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
51 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
52 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
54 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
55 public static final ConcreteType CLASS = typeForClass(Class.class);
56 public static final ConcreteType STRING = typeForClass(String.class);
57 public static final ConcreteType VOID = typeForClass(Void.class);
59 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
60 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
62 private static final Splitter DOT_SPLITTER = Splitter.on('.');
63 private static final Type SET_TYPE = typeForClass(Set.class);
64 private static final Type LIST_TYPE = typeForClass(List.class);
65 private static final Type MAP_TYPE = typeForClass(Map.class);
68 throw new UnsupportedOperationException("Utility class");
72 * Creates the instance of type
74 * ConcreteType} which represents JAVA <code>void</code> type.
76 * @return <code>ConcreteType</code> instance which represents JAVA
79 public static ConcreteType voidType() {
84 * Creates the instance of type
86 * ConcreteType} which represents primitive JAVA type for which package
89 * @param primitiveType
90 * string containing programmatic construction based on
91 * primitive type (e.g byte[])
92 * @return <code>ConcreteType</code> instance which represents programmatic
93 * construction with primitive JAVA type
95 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
96 return new ConcreteTypeImpl("", primitiveType, restrictions);
100 * Returns an instance of {@link ConcreteType} describing the class
104 * @return Description of class
106 public static ConcreteType typeForClass(final Class<?> cls) {
107 return TYPE_CACHE.getUnchecked(cls);
111 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
112 if (restrictions != null) {
113 if (restrictions instanceof DefaultRestrictions) {
114 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
116 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
119 return typeForClass(cls);
124 * Returns an instance of {@link ParameterizedType} describing the typed
125 * {@link Map}<K,V>
131 * @return Description of generic type instance
133 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
134 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
138 * Returns an instance of {@link ParameterizedType} describing the typed
139 * {@link Set}<V> with concrete type of value.
143 * @return Description of generic type instance of Set
145 public static ParameterizedType setTypeFor(final Type valueType) {
146 return parameterizedTypeFor(SET_TYPE, valueType);
150 * Returns an instance of {@link ParameterizedType} describing the typed
151 * {@link List}<V> with concrete type of value.
155 * @return Description of type instance of List
157 public static ParameterizedType listTypeFor(final Type valueType) {
158 return parameterizedTypeFor(LIST_TYPE, valueType);
162 * Creates instance of type
163 * {@link ParameterizedType
167 * JAVA <code>Type</code> for raw type
169 * JAVA <code>Type</code>s for actual parameter types
170 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
171 * and its parameters <code>parameters</code>
173 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
174 return new ParameterizedTypeImpl(type, parameters);
178 * Creates instance of type
179 * {@link WildcardType
183 * string with the package name
185 * string with the type name
186 * @return <code>WildcardType</code> representation of
187 * <code>packageName</code> and <code>typeName</code>
189 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
190 return new WildcardTypeImpl(packageName, typeName);
194 * Creates instance of
195 * {@link ParameterizedType
196 * ParameterizedType} where raw type is
197 * {@link Augmentable} and actual
198 * parameter is <code>valueType</code>.
201 * JAVA <code>Type</code> with actual parameter
202 * @return <code>ParametrizedType</code> representation of raw type
203 * <code>Augmentable</code> with actual parameter
204 * <code>valueType</code>
206 public static ParameterizedType augmentableTypeFor(final Type valueType) {
207 final Type augmentable = typeForClass(Augmentable.class);
208 return parameterizedTypeFor(augmentable, valueType);
212 * Creates instance of
213 * {@link ParameterizedType
214 * ParameterizedType} where raw type is
215 * {@link Augmentation} and actual
216 * parameter is <code>valueType</code>.
219 * JAVA <code>Type</code> with actual parameter
220 * @return <code>ParametrizedType</code> reprezentation of raw type
221 * <code>Augmentation</code> with actual parameter
222 * <code>valueType</code>
224 public static ParameterizedType augmentationTypeFor(final Type valueType) {
225 final Type augmentation = typeForClass(Augmentation.class);
226 return parameterizedTypeFor(augmentation, valueType);
230 public static String getOuterClassName(final Type valueType) {
231 final String pkgName = valueType.getPackageName();
232 if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) {
233 // It is inner class.
234 return Iterables.getLast(DOT_SPLITTER.split(pkgName));
241 * Represents concrete JAVA type.
244 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
246 private final Restrictions restrictions;
249 * Creates instance of this class with package <code>pkName</code> and
250 * with the type name <code>name</code>.
253 * string with package name
255 * string with the name of the type
257 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
259 this.restrictions = restrictions;
263 public Restrictions getRestrictions() {
264 return this.restrictions;
270 * Represents concrete JAVA type with changed restriction values.
273 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
274 private final Restrictions restrictions;
277 * Creates instance of this class with package <code>pkName</code> and
278 * with the type name <code>name</code>.
281 * string with package name
283 * string with the name of the type
285 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
287 this.restrictions = Preconditions.checkNotNull(restrictions);
291 public Restrictions getRestrictions() {
292 return this.restrictions;
298 * Represents parametrized JAVA type.
301 private static class ParameterizedTypeImpl extends AbstractBaseType implements ParameterizedType {
303 * Array of JAVA actual type parameters.
305 private final Type[] actualTypes;
308 * JAVA raw type (like List, Set, Map...)
310 private final Type rawType;
313 * Creates instance of this class with concrete rawType and array of
317 * JAVA <code>Type</code> for raw type
319 * array of actual parameters
321 public ParameterizedTypeImpl(final Type rawType, final Type[] actTypes) {
322 super(rawType.getPackageName(), rawType.getName(), true);
323 this.rawType = rawType;
324 this.actualTypes = actTypes.clone();
328 public Type[] getActualTypeArguments() {
330 return this.actualTypes;
334 public Type getRawType() {
341 * Represents JAVA bounded wildcard type.
344 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
346 * Creates instance of this class with concrete package and type name.
349 * string with the package name
351 * string with the name of type
353 public WildcardTypeImpl(final String packageName, final String typeName) {
354 super(packageName, typeName);
358 public static <T extends Number> DefaultRestrictions<T> getDefaultRestrictions(final T min, final T max) {
359 return new DefaultRestrictions<>(min, max);
362 private static final class DefaultRestrictions<T extends Number> implements Restrictions {
363 private final List<RangeConstraint> rangeConstraints;
365 private DefaultRestrictions(final T min, final T max) {
366 Preconditions.checkNotNull(min);
367 Preconditions.checkNotNull(max);
368 this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
369 .absent(), Optional.absent()));
373 public boolean isEmpty() {
378 public List<RangeConstraint> getRangeConstraints() {
379 return this.rangeConstraints;
383 public List<PatternConstraint> getPatternConstraints() {
384 return ImmutableList.of();
388 public List<LengthConstraint> getLengthConstraints() {
389 return ImmutableList.of();