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;
39 import org.w3c.dom.Document;
42 public final class Types {
43 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
44 new CacheLoader<Class<?>, ConcreteType>() {
47 public ConcreteType load(@Nonnull final Class<?> key) throws Exception {
48 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
52 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
53 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
55 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
56 public static final ConcreteType CLASS = typeForClass(Class.class);
57 public static final ConcreteType STRING = typeForClass(String.class);
58 public static final ConcreteType VOID = typeForClass(Void.class);
59 public static final ConcreteType DOCUMENT = typeForClass(Document.class);
61 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
62 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
64 private static final Splitter DOT_SPLITTER = Splitter.on('.');
65 private static final Type SET_TYPE = typeForClass(Set.class);
66 private static final Type LIST_TYPE = typeForClass(List.class);
67 private static final Type MAP_TYPE = typeForClass(Map.class);
70 throw new UnsupportedOperationException("Utility class");
74 * Creates the instance of type
76 * ConcreteType} which represents JAVA <code>void</code> type.
78 * @return <code>ConcreteType</code> instance which represents JAVA
81 public static ConcreteType voidType() {
86 * Creates the instance of type
88 * ConcreteType} which represents primitive JAVA type for which package
91 * @param primitiveType
92 * string containing programmatic construction based on
93 * primitive type (e.g byte[])
94 * @return <code>ConcreteType</code> instance which represents programmatic
95 * construction with primitive JAVA type
97 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
98 return new ConcreteTypeImpl("", primitiveType, restrictions);
102 * Returns an instance of {@link ConcreteType} describing the class
106 * @return Description of class
108 public static ConcreteType typeForClass(final Class<?> cls) {
109 return TYPE_CACHE.getUnchecked(cls);
113 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
114 if (restrictions != null) {
115 if (restrictions instanceof DefaultRestrictions) {
116 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
118 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
121 return typeForClass(cls);
126 * Returns an instance of {@link ParameterizedType} describing the typed
127 * {@link Map}<K,V>
133 * @return Description of generic type instance
135 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
136 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
140 * Returns an instance of {@link ParameterizedType} describing the typed
141 * {@link Set}<V> with concrete type of value.
145 * @return Description of generic type instance of Set
147 public static ParameterizedType setTypeFor(final Type valueType) {
148 return parameterizedTypeFor(SET_TYPE, valueType);
152 * Returns an instance of {@link ParameterizedType} describing the typed
153 * {@link List}<V> with concrete type of value.
157 * @return Description of type instance of List
159 public static ParameterizedType listTypeFor(final Type valueType) {
160 return parameterizedTypeFor(LIST_TYPE, valueType);
164 * Creates instance of type
165 * {@link ParameterizedType
169 * JAVA <code>Type</code> for raw type
171 * JAVA <code>Type</code>s for actual parameter types
172 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
173 * and its parameters <code>parameters</code>
175 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
176 return new ParameterizedTypeImpl(type, parameters);
180 * Creates instance of type
181 * {@link WildcardType
185 * string with the package name
187 * string with the type name
188 * @return <code>WildcardType</code> representation of
189 * <code>packageName</code> and <code>typeName</code>
191 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
192 return new WildcardTypeImpl(packageName, typeName);
196 * Creates instance of
197 * {@link ParameterizedType
198 * ParameterizedType} where raw type is
199 * {@link Augmentable} and actual
200 * parameter is <code>valueType</code>.
203 * JAVA <code>Type</code> with actual parameter
204 * @return <code>ParametrizedType</code> representation of raw type
205 * <code>Augmentable</code> with actual parameter
206 * <code>valueType</code>
208 public static ParameterizedType augmentableTypeFor(final Type valueType) {
209 final Type augmentable = typeForClass(Augmentable.class);
210 return parameterizedTypeFor(augmentable, valueType);
214 * Creates instance of
215 * {@link ParameterizedType
216 * ParameterizedType} where raw type is
217 * {@link Augmentation} and actual
218 * parameter is <code>valueType</code>.
221 * JAVA <code>Type</code> with actual parameter
222 * @return <code>ParametrizedType</code> reprezentation of raw type
223 * <code>Augmentation</code> with actual parameter
224 * <code>valueType</code>
226 public static ParameterizedType augmentationTypeFor(final Type valueType) {
227 final Type augmentation = typeForClass(Augmentation.class);
228 return parameterizedTypeFor(augmentation, valueType);
232 public static String getOuterClassName(final Type valueType) {
233 final String pkgName = valueType.getPackageName();
234 final int index = CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName);
236 // It is inner class.
237 return Iterables.getFirst(DOT_SPLITTER.split(pkgName.substring(index)), null);
243 public static String getOuterClassPackageName(final Type valueType) {
244 final String pkgName = valueType.getPackageName();
245 final int index = CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName);
247 return pkgName.substring(0, index - 1);
254 * Represents concrete JAVA type.
257 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
259 private final Restrictions restrictions;
262 * Creates instance of this class with package <code>pkName</code> and
263 * with the type name <code>name</code>.
266 * string with package name
268 * string with the name of the type
270 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
272 this.restrictions = restrictions;
276 public Restrictions getRestrictions() {
277 return this.restrictions;
283 * Represents concrete JAVA type with changed restriction values.
286 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
287 private final Restrictions restrictions;
290 * Creates instance of this class with package <code>pkName</code> and
291 * with the type name <code>name</code>.
294 * string with package name
296 * string with the name of the type
298 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
300 this.restrictions = Preconditions.checkNotNull(restrictions);
304 public Restrictions getRestrictions() {
305 return this.restrictions;
311 * Represents parametrized JAVA type.
314 private static class ParameterizedTypeImpl extends AbstractBaseType implements ParameterizedType {
316 * Array of JAVA actual type parameters.
318 private final Type[] actualTypes;
321 * JAVA raw type (like List, Set, Map...)
323 private final Type rawType;
326 * Creates instance of this class with concrete rawType and array of
330 * JAVA <code>Type</code> for raw type
332 * array of actual parameters
334 public ParameterizedTypeImpl(final Type rawType, final Type[] actTypes) {
335 super(rawType.getPackageName(), rawType.getName(), true);
336 this.rawType = rawType;
337 this.actualTypes = actTypes.clone();
341 public Type[] getActualTypeArguments() {
343 return this.actualTypes;
347 public Type getRawType() {
354 * Represents JAVA bounded wildcard type.
357 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
359 * Creates instance of this class with concrete package and type name.
362 * string with the package name
364 * string with the name of type
366 public WildcardTypeImpl(final String packageName, final String typeName) {
367 super(packageName, typeName);
371 public static <T extends Number> DefaultRestrictions<T> getDefaultRestrictions(final T min, final T max) {
372 return new DefaultRestrictions<>(min, max);
375 private static final class DefaultRestrictions<T extends Number> implements Restrictions {
376 private final List<RangeConstraint> rangeConstraints;
378 private DefaultRestrictions(final T min, final T max) {
379 Preconditions.checkNotNull(min);
380 Preconditions.checkNotNull(max);
381 this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
382 .absent(), Optional.absent()));
386 public boolean isEmpty() {
391 public List<RangeConstraint> getRangeConstraints() {
392 return this.rangeConstraints;
396 public List<PatternConstraint> getPatternConstraints() {
397 return ImmutableList.of();
401 public List<LengthConstraint> getLengthConstraints() {
402 return ImmutableList.of();