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.Preconditions;
14 import com.google.common.base.Splitter;
15 import com.google.common.cache.CacheBuilder;
16 import com.google.common.cache.CacheLoader;
17 import com.google.common.cache.LoadingCache;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableRangeSet;
20 import com.google.common.collect.Iterables;
21 import com.google.common.collect.Range;
22 import com.google.common.collect.RangeSet;
23 import java.util.List;
25 import java.util.Optional;
27 import javax.annotation.Nonnull;
28 import javax.annotation.Nullable;
29 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
30 import org.opendaylight.mdsal.binding.javav2.model.api.BaseTypeWithRestrictions;
31 import org.opendaylight.mdsal.binding.javav2.model.api.ConcreteType;
32 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
33 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
34 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
35 import org.opendaylight.mdsal.binding.javav2.model.api.WildcardType;
36 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
37 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
38 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
39 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
40 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
41 import org.w3c.dom.Document;
44 public final class Types {
45 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
46 new CacheLoader<Class<?>, ConcreteType>() {
49 public ConcreteType load(@Nonnull final Class<?> key) throws Exception {
50 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
54 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
55 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
57 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
58 public static final ConcreteType CLASS = typeForClass(Class.class);
59 public static final ConcreteType STRING = typeForClass(String.class);
60 public static final ConcreteType VOID = typeForClass(Void.class);
61 public static final ConcreteType DOCUMENT = typeForClass(Document.class);
63 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
64 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
66 private static final Splitter DOT_SPLITTER = Splitter.on('.');
67 private static final Type SET_TYPE = typeForClass(Set.class);
68 private static final Type LIST_TYPE = typeForClass(List.class);
69 private static final Type MAP_TYPE = typeForClass(Map.class);
72 throw new UnsupportedOperationException("Utility class");
76 * Creates the instance of type
78 * ConcreteType} which represents JAVA <code>void</code> type.
80 * @return <code>ConcreteType</code> instance which represents JAVA
83 public static ConcreteType voidType() {
88 * Creates the instance of type
90 * ConcreteType} which represents primitive JAVA type for which package
93 * @param primitiveType
94 * string containing programmatic construction based on
95 * primitive type (e.g byte[])
96 * @return <code>ConcreteType</code> instance which represents programmatic
97 * construction with primitive JAVA type
99 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
100 return new ConcreteTypeImpl("", primitiveType, restrictions);
104 * Returns an instance of {@link ConcreteType} describing the class
108 * @return Description of class
110 public static ConcreteType typeForClass(final Class<?> cls) {
111 return TYPE_CACHE.getUnchecked(cls);
115 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
116 if (restrictions == null) {
117 return typeForClass(cls);
119 if (restrictions instanceof DefaultRestrictions) {
120 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
122 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
126 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions,
127 final ModuleContext moduleContext) {
128 if (restrictions == null) {
129 return typeForClass(cls);
131 if (restrictions instanceof DefaultRestrictions) {
132 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
134 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions,
140 * Returns an instance of {@link ParameterizedType} describing the typed
141 * {@link Map}<K,V>
147 * @return Description of generic type instance
149 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
150 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
154 * Returns an instance of {@link ParameterizedType} describing the typed
155 * {@link Set}<V> with concrete type of value.
159 * @return Description of generic type instance of Set
161 public static ParameterizedType setTypeFor(final Type valueType) {
162 return parameterizedTypeFor(SET_TYPE, valueType);
166 * Returns an instance of {@link ParameterizedType} describing the typed
167 * {@link List}<V> with concrete type of value.
171 * @return Description of type instance of List
173 public static ParameterizedType listTypeFor(final Type valueType) {
174 return parameterizedTypeFor(LIST_TYPE, valueType);
178 * Creates instance of type
179 * {@link ParameterizedType
183 * JAVA <code>Type</code> for raw type
185 * JAVA <code>Type</code>s for actual parameter types
186 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
187 * and its parameters <code>parameters</code>
189 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
190 return new ParameterizedTypeImpl(type, parameters);
194 * Creates instance of type
195 * {@link WildcardType
199 * string with the package name
201 * string with the type name
202 * @return <code>WildcardType</code> representation of
203 * <code>packageName</code> and <code>typeName</code>
205 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
206 return new WildcardTypeImpl(packageName, typeName);
210 * Creates instance of type
211 * {@link WildcardType
215 * string with the package name
217 * string with the type name
218 * @param isPkNameNormalized
219 * if the package name has been normalized
220 * @param isTypeNormalized
221 * if the type name has been normalized
222 * @return <code>WildcardType</code> representation of
223 * <code>packageName</code> and <code>typeName</code>
225 public static WildcardType wildcardTypeFor(final String packageName, final String typeName,
226 final boolean isPkNameNormalized, final boolean isTypeNormalized, final ModuleContext context) {
227 return new WildcardTypeImpl(packageName, typeName, isPkNameNormalized, isTypeNormalized, context);
231 * Creates instance of
232 * {@link ParameterizedType
233 * ParameterizedType} where raw type is
234 * {@link Augmentable} and actual
235 * parameter is <code>valueType</code>.
238 * JAVA <code>Type</code> with actual parameter
239 * @return <code>ParametrizedType</code> representation of raw type
240 * <code>Augmentable</code> with actual parameter
241 * <code>valueType</code>
243 public static ParameterizedType augmentableTypeFor(final Type valueType) {
244 final Type augmentable = typeForClass(Augmentable.class);
245 return parameterizedTypeFor(augmentable, valueType);
249 * Creates instance of
250 * {@link ParameterizedType
251 * ParameterizedType} where raw type is
252 * {@link Augmentation} and actual
253 * parameter is <code>valueType</code>.
256 * JAVA <code>Type</code> with actual parameter
257 * @return <code>ParametrizedType</code> reprezentation of raw type
258 * <code>Augmentation</code> with actual parameter
259 * <code>valueType</code>
261 public static ParameterizedType augmentationTypeFor(final Type valueType) {
262 final Type augmentation = typeForClass(Augmentation.class);
263 return parameterizedTypeFor(augmentation, valueType);
267 public static String getOuterClassName(final Type valueType) {
268 final String pkgName = valueType.getPackageName();
269 final int index = CharMatcher.javaUpperCase().indexIn(pkgName);
271 // It is inner class.
272 return Iterables.getFirst(DOT_SPLITTER.split(pkgName.substring(index)), null);
278 public static String getOuterClassPackageName(final Type valueType) {
279 final String pkgName = valueType.getPackageName();
280 final int index = CharMatcher.javaUpperCase().indexIn(pkgName);
282 return pkgName.substring(0, index - 1);
289 * Represents concrete JAVA type.
292 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
294 private final Restrictions restrictions;
297 * Creates instance of this class with package <code>pkName</code> and
298 * with the type name <code>name</code>.
301 * string with package name
303 * string with the name of the type
305 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
306 super(pkName, name, true,null);
307 this.restrictions = restrictions;
311 public Restrictions getRestrictions() {
312 return this.restrictions;
318 * Represents concrete JAVA type with changed restriction values.
321 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
322 private final Restrictions restrictions;
325 * Creates instance of this class with package <code>pkName</code> and
326 * with the type name <code>name</code>.
329 * string with package name
331 * string with the name of the type
333 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
334 super(pkName, name, null);
335 this.restrictions = Preconditions.checkNotNull(restrictions);
338 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions,
339 final ModuleContext moduleContext) {
340 super(pkName, name, moduleContext);
341 this.restrictions = Preconditions.checkNotNull(restrictions);
345 public Restrictions getRestrictions() {
346 return this.restrictions;
352 * Represents parametrized JAVA type.
355 private static class ParameterizedTypeImpl extends AbstractBaseType implements ParameterizedType {
357 * Array of JAVA actual type parameters.
359 private final Type[] actualTypes;
362 * JAVA raw type (like List, Set, Map...)
364 private final Type rawType;
367 * Creates instance of this class with concrete rawType and array of
371 * JAVA <code>Type</code> for raw type
373 * array of actual parameters
375 public ParameterizedTypeImpl(final Type rawType, final Type[] actTypes) {
376 super(rawType.getPackageName(), rawType.getName(), true, null);
377 this.rawType = rawType;
378 this.actualTypes = actTypes.clone();
382 public Type[] getActualTypeArguments() {
384 return this.actualTypes;
388 public Type getRawType() {
395 * Represents JAVA bounded wildcard type.
398 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
400 * Creates instance of this class with concrete package and type name.
403 * string with the package name
405 * string with the name of type
407 //FIXME: doesn't seem to be called at all
408 public WildcardTypeImpl(final String packageName, final String typeName) {
409 super(packageName, typeName, null);
413 * Creates instance of this class with concrete package and type name.
416 * string with the package name
418 * string with the name of type
419 * @param isPkNameNormalized
420 * if the package name has been normalized
421 * @param isTypeNormalized
422 * if the type name has been normalized
424 public WildcardTypeImpl(final String packageName, final String typeName, final boolean isPkNameNormalized,
425 final boolean isTypeNormalized, final ModuleContext context) {
426 super(packageName, typeName, isPkNameNormalized, isTypeNormalized, context);
430 public static <T extends Number & Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
432 return new DefaultRestrictions<>(min, max);
435 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
436 private final RangeConstraint<T> rangeConstraint;
438 private DefaultRestrictions(final T min, final T max) {
439 Preconditions.checkNotNull(min);
440 Preconditions.checkNotNull(max);
441 this.rangeConstraint = new RangeConstraint<T>() {
444 public Optional<String> getErrorAppTag() {
445 return Optional.empty();
449 public Optional<String> getErrorMessage() {
450 return Optional.empty();
454 public Optional<String> getDescription() {
455 return Optional.empty();
459 public Optional<String> getReference() {
460 return Optional.empty();
464 public RangeSet<T> getAllowedRanges() {
465 return ImmutableRangeSet.of(Range.closed(min, max));
471 public boolean isEmpty() {
476 public Optional<RangeConstraint<?>> getRangeConstraint() {
477 return Optional.ofNullable(rangeConstraint);
481 public List<PatternConstraint> getPatternConstraints() {
482 return ImmutableList.of();
486 public Optional<LengthConstraint> getLengthConstraint() {
487 return Optional.empty();