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.Nullable;
26 import org.opendaylight.mdsal.binding.javav2.model.api.BaseTypeWithRestrictions;
27 import org.opendaylight.mdsal.binding.javav2.model.api.ConcreteType;
28 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
29 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
30 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
31 import org.opendaylight.mdsal.binding.javav2.model.api.WildcardType;
32 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
33 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
34 import org.opendaylight.mdsal.binding.javav2.spec.base.RpcCallback;
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(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 RPC_CALLBACK = typeForClass(RpcCallback.class);
56 public static final ConcreteType STRING = typeForClass(String.class);
57 public static final ConcreteType VOID = typeForClass(Void.class);
58 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
59 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
61 private static final Splitter DOT_SPLITTER = Splitter.on('.');
62 private static final Type SET_TYPE = typeForClass(Set.class);
63 private static final Type LIST_TYPE = typeForClass(List.class);
64 private static final Type MAP_TYPE = typeForClass(Map.class);
67 throw new UnsupportedOperationException("Utility class");
71 * Creates the instance of type
73 * ConcreteType} which represents JAVA <code>void</code> type.
75 * @return <code>ConcreteType</code> instance which represents JAVA
78 public static ConcreteType voidType() {
83 * Creates the instance of type
85 * ConcreteType} which represents primitive JAVA type for which package
88 * @param primitiveType
89 * string containing programmatic construction based on
90 * primitive type (e.g byte[])
91 * @return <code>ConcreteType</code> instance which represents programmatic
92 * construction with primitive JAVA type
94 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
95 return new ConcreteTypeImpl("", primitiveType, restrictions);
99 * Returns an instance of {@link ConcreteType} describing the class
103 * @return Description of class
105 public static ConcreteType typeForClass(final Class<?> cls) {
106 return TYPE_CACHE.getUnchecked(cls);
110 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
111 if (restrictions != null) {
112 if (restrictions instanceof DefaultRestrictions) {
113 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
115 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
118 return typeForClass(cls);
123 * Returns an instance of {@link ParameterizedType} describing the typed
124 * {@link Map}<K,V>
130 * @return Description of generic type instance
132 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
133 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
137 * Returns an instance of {@link ParameterizedType} describing the typed
138 * {@link Set}<V> with concrete type of value.
142 * @return Description of generic type instance of Set
144 public static ParameterizedType setTypeFor(final Type valueType) {
145 return parameterizedTypeFor(SET_TYPE, valueType);
149 * Returns an instance of {@link ParameterizedType} describing the typed
150 * {@link List}<V> with concrete type of value.
154 * @return Description of type instance of List
156 public static ParameterizedType listTypeFor(final Type valueType) {
157 return parameterizedTypeFor(LIST_TYPE, valueType);
161 * Creates instance of type
162 * {@link ParameterizedType
166 * JAVA <code>Type</code> for raw type
168 * JAVA <code>Type</code>s for actual parameter types
169 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
170 * and its parameters <code>parameters</code>
172 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
173 return new ParameterizedTypeImpl(type, parameters);
177 * Creates instance of type
178 * {@link WildcardType
182 * string with the package name
184 * string with the type name
185 * @return <code>WildcardType</code> representation of
186 * <code>packageName</code> and <code>typeName</code>
188 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
189 return new WildcardTypeImpl(packageName, typeName);
193 * Creates instance of
194 * {@link ParameterizedType
195 * ParameterizedType} where raw type is
196 * {@link Augmentable} and actual
197 * parameter is <code>valueType</code>.
200 * JAVA <code>Type</code> with actual parameter
201 * @return <code>ParametrizedType</code> representation of raw type
202 * <code>Augmentable</code> with actual parameter
203 * <code>valueType</code>
205 public static ParameterizedType augmentableTypeFor(final Type valueType) {
206 final Type augmentable = typeForClass(Augmentable.class);
207 return parameterizedTypeFor(augmentable, valueType);
211 * Creates instance of
212 * {@link ParameterizedType
213 * ParameterizedType} where raw type is
214 * {@link Augmentation} and actual
215 * parameter is <code>valueType</code>.
218 * JAVA <code>Type</code> with actual parameter
219 * @return <code>ParametrizedType</code> reprezentation of raw type
220 * <code>Augmentation</code> with actual parameter
221 * <code>valueType</code>
223 public static ParameterizedType augmentationTypeFor(final Type valueType) {
224 final Type augmentation = typeForClass(Augmentation.class);
225 return parameterizedTypeFor(augmentation, valueType);
229 public static String getOuterClassName(final Type valueType) {
230 final String pkgName = valueType.getPackageName();
231 if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) {
232 // It is inner class.
233 return Iterables.getLast(DOT_SPLITTER.split(pkgName));
240 * Represents concrete JAVA type.
243 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
249 * with the type name <code>name</code>.
252 * string with package name
254 * string with the name of the type
256 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
258 this.restrictions = restrictions;
262 public Restrictions getRestrictions() {
269 * Represents concrete JAVA type with changed restriction values.
272 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
273 private final Restrictions restrictions;
276 * Creates instance of this class with package <code>pkName</code> and
277 * with the type name <code>name</code>.
280 * string with package name
282 * string with the name of the type
284 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
286 this.restrictions = Preconditions.checkNotNull(restrictions);
290 public Restrictions getRestrictions() {
297 * Represents parametrized JAVA type.
300 private static class ParameterizedTypeImpl extends AbstractBaseType implements ParameterizedType {
302 * Array of JAVA actual type parameters.
304 private final Type[] actualTypes;
307 * JAVA raw type (like List, Set, Map...)
309 private final Type rawType;
312 * Creates instance of this class with concrete rawType and array of
316 * JAVA <code>Type</code> for raw type
318 * array of actual parameters
320 public ParameterizedTypeImpl(final Type rawType, final Type[] actTypes) {
321 super(rawType.getPackageName(), rawType.getName());
322 this.rawType = rawType;
323 this.actualTypes = actTypes.clone();
327 public Type[] getActualTypeArguments() {
333 public Type getRawType() {
340 * Represents JAVA bounded wildcard type.
343 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
345 * Creates instance of this class with concrete package and type name.
348 * string with the package name
350 * string with the name of type
352 public WildcardTypeImpl(final String packageName, final String typeName) {
353 super(packageName, typeName);
357 public static <T extends Number> DefaultRestrictions<T> getDefaultRestrictions(final T min, final T max) {
358 return new DefaultRestrictions<>(min, max);
361 private static final class DefaultRestrictions<T extends Number> implements Restrictions {
362 private final List<RangeConstraint> rangeConstraints;
364 private DefaultRestrictions(final T min, final T max) {
365 Preconditions.checkNotNull(min);
366 Preconditions.checkNotNull(max);
367 this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
368 .absent(), Optional.absent()));
372 public boolean isEmpty() {
377 public List<RangeConstraint> getRangeConstraints() {
378 return rangeConstraints;
382 public List<PatternConstraint> getPatternConstraints() {
383 return ImmutableList.of();
387 public List<LengthConstraint> getLengthConstraints() {
388 return ImmutableList.of();