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 type
197 * {@link WildcardType
201 * string with the package name
203 * string with the type name
204 * @param isPkNameNormalized
205 * if the package name has been normalized
206 * @param isTypeNormalized
207 * if the type name has been normalized
208 * @return <code>WildcardType</code> representation of
209 * <code>packageName</code> and <code>typeName</code>
211 public static WildcardType wildcardTypeFor(final String packageName, final String typeName,
212 final boolean isPkNameNormalized, final boolean isTypeNormalized) {
213 return new WildcardTypeImpl(packageName, typeName, isPkNameNormalized, isTypeNormalized);
217 * Creates instance of
218 * {@link ParameterizedType
219 * ParameterizedType} where raw type is
220 * {@link Augmentable} and actual
221 * parameter is <code>valueType</code>.
224 * JAVA <code>Type</code> with actual parameter
225 * @return <code>ParametrizedType</code> representation of raw type
226 * <code>Augmentable</code> with actual parameter
227 * <code>valueType</code>
229 public static ParameterizedType augmentableTypeFor(final Type valueType) {
230 final Type augmentable = typeForClass(Augmentable.class);
231 return parameterizedTypeFor(augmentable, valueType);
235 * Creates instance of
236 * {@link ParameterizedType
237 * ParameterizedType} where raw type is
238 * {@link Augmentation} and actual
239 * parameter is <code>valueType</code>.
242 * JAVA <code>Type</code> with actual parameter
243 * @return <code>ParametrizedType</code> reprezentation of raw type
244 * <code>Augmentation</code> with actual parameter
245 * <code>valueType</code>
247 public static ParameterizedType augmentationTypeFor(final Type valueType) {
248 final Type augmentation = typeForClass(Augmentation.class);
249 return parameterizedTypeFor(augmentation, valueType);
253 public static String getOuterClassName(final Type valueType) {
254 final String pkgName = valueType.getPackageName();
255 final int index = CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName);
257 // It is inner class.
258 return Iterables.getFirst(DOT_SPLITTER.split(pkgName.substring(index)), null);
264 public static String getOuterClassPackageName(final Type valueType) {
265 final String pkgName = valueType.getPackageName();
266 final int index = CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName);
268 return pkgName.substring(0, index - 1);
275 * Represents concrete JAVA type.
278 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
280 private final Restrictions restrictions;
283 * Creates instance of this class with package <code>pkName</code> and
284 * with the type name <code>name</code>.
287 * string with package name
289 * string with the name of the type
291 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
293 this.restrictions = restrictions;
297 public Restrictions getRestrictions() {
298 return this.restrictions;
304 * Represents concrete JAVA type with changed restriction values.
307 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
308 private final Restrictions restrictions;
311 * Creates instance of this class with package <code>pkName</code> and
312 * with the type name <code>name</code>.
315 * string with package name
317 * string with the name of the type
319 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
321 this.restrictions = Preconditions.checkNotNull(restrictions);
325 public Restrictions getRestrictions() {
326 return this.restrictions;
332 * Represents parametrized JAVA type.
335 private static class ParameterizedTypeImpl extends AbstractBaseType implements ParameterizedType {
337 * Array of JAVA actual type parameters.
339 private final Type[] actualTypes;
342 * JAVA raw type (like List, Set, Map...)
344 private final Type rawType;
347 * Creates instance of this class with concrete rawType and array of
351 * JAVA <code>Type</code> for raw type
353 * array of actual parameters
355 public ParameterizedTypeImpl(final Type rawType, final Type[] actTypes) {
356 super(rawType.getPackageName(), rawType.getName(), true);
357 this.rawType = rawType;
358 this.actualTypes = actTypes.clone();
362 public Type[] getActualTypeArguments() {
364 return this.actualTypes;
368 public Type getRawType() {
375 * Represents JAVA bounded wildcard type.
378 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
380 * Creates instance of this class with concrete package and type name.
383 * string with the package name
385 * string with the name of type
387 public WildcardTypeImpl(final String packageName, final String typeName) {
388 super(packageName, typeName);
392 * Creates instance of this class with concrete package and type name.
395 * string with the package name
397 * string with the name of type
398 * @param isPkNameNormalized
399 * if the package name has been normalized
400 * @param isTypeNormalized
401 * if the type name has been normalized
403 public WildcardTypeImpl(final String packageName, final String typeName, final boolean isPkNameNormalized,
404 final boolean isTypeNormalized) {
405 super(packageName, typeName, isPkNameNormalized, isTypeNormalized);
409 public static <T extends Number> DefaultRestrictions<T> getDefaultRestrictions(final T min, final T max) {
410 return new DefaultRestrictions<>(min, max);
413 private static final class DefaultRestrictions<T extends Number> implements Restrictions {
414 private final List<RangeConstraint> rangeConstraints;
416 private DefaultRestrictions(final T min, final T max) {
417 Preconditions.checkNotNull(min);
418 Preconditions.checkNotNull(max);
419 this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
420 .absent(), Optional.absent()));
424 public boolean isEmpty() {
429 public List<RangeConstraint> getRangeConstraints() {
430 return this.rangeConstraints;
434 public List<PatternConstraint> getPatternConstraints() {
435 return ImmutableList.of();
439 public List<LengthConstraint> getLengthConstraints() {
440 return ImmutableList.of();