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 static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.CharMatcher;
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.ImmutableRangeSet;
21 import com.google.common.collect.Iterables;
22 import com.google.common.collect.Range;
23 import com.google.common.collect.RangeSet;
25 import java.util.List;
27 import java.util.Optional;
29 import javax.annotation.Nonnull;
30 import javax.annotation.Nullable;
32 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
33 import org.opendaylight.mdsal.binding.javav2.model.api.BaseTypeWithRestrictions;
34 import org.opendaylight.mdsal.binding.javav2.model.api.ConcreteType;
35 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
36 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
37 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
38 import org.opendaylight.mdsal.binding.javav2.model.api.WildcardType;
39 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
40 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
41 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
42 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
43 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
44 import org.w3c.dom.Document;
47 public final class Types {
48 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
49 new CacheLoader<Class<?>, ConcreteType>() {
52 public ConcreteType load(@Nonnull final Class<?> key) throws Exception {
53 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
57 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
58 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
60 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
61 public static final ConcreteType CLASS = typeForClass(Class.class);
62 public static final ConcreteType STRING = typeForClass(String.class);
63 public static final ConcreteType VOID = typeForClass(Void.class);
64 public static final ConcreteType DOCUMENT = typeForClass(Document.class);
66 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
67 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
69 private static final Splitter DOT_SPLITTER = Splitter.on('.');
70 private static final Type SET_TYPE = typeForClass(Set.class);
71 private static final Type LIST_TYPE = typeForClass(List.class);
72 private static final Type MAP_TYPE = typeForClass(Map.class);
75 throw new UnsupportedOperationException("Utility class");
79 * Creates the instance of type
81 * ConcreteType} which represents JAVA <code>void</code> type.
83 * @return <code>ConcreteType</code> instance which represents JAVA
86 public static ConcreteType voidType() {
91 * Creates the instance of type
93 * ConcreteType} which represents primitive JAVA type for which package
96 * @param primitiveType string containing programmatic construction based on
97 * primitive type (e.g byte[])
98 * @return <code>ConcreteType</code> instance which represents programmatic
99 * construction with primitive JAVA type
101 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
102 return new ConcreteTypeImpl("", primitiveType, restrictions);
106 * Returns an instance of {@link ConcreteType} describing the class.
108 * @param cls Class to describe
109 * @return Description of class
111 public static ConcreteType typeForClass(final Class<?> cls) {
112 return TYPE_CACHE.getUnchecked(cls);
116 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
117 if (restrictions == null) {
118 return typeForClass(cls);
120 if (restrictions instanceof DefaultRestrictions) {
121 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
123 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
127 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions,
128 final ModuleContext moduleContext) {
129 if (restrictions == null) {
130 return typeForClass(cls);
132 if (restrictions instanceof DefaultRestrictions) {
133 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
135 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions,
141 * Returns an instance of {@link ParameterizedType} describing the typed.
142 * {@link Map}<K,V>
144 * @param keyType Key Type
145 * @param valueType Value Type
146 * @return Description of generic type instance
148 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
149 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
153 * Returns an instance of {@link ParameterizedType} describing the typed
154 * {@link Set}<V> with concrete type of value.
156 * @param valueType Value Type
157 * @return Description of generic type instance of Set
159 public static ParameterizedType setTypeFor(final Type valueType) {
160 return parameterizedTypeFor(SET_TYPE, valueType);
164 * Returns an instance of {@link ParameterizedType} describing the typed
165 * {@link List}<V> with concrete type of value.
167 * @param valueType Value Type
168 * @return Description of type instance of List
170 public static ParameterizedType listTypeFor(final Type valueType) {
171 return parameterizedTypeFor(LIST_TYPE, valueType);
175 * Creates instance of type {@link ParameterizedType ParameterizedType}.
177 * @param type JAVA <code>Type</code> for raw type
178 * @param parameters JAVA <code>Type</code>s for actual parameter types
179 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
180 * and its parameters <code>parameters</code>
182 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
183 return new ParameterizedTypeImpl(type, parameters);
187 * Creates instance of type {@link WildcardType WildcardType}.
189 * @param packageName string with the package name
190 * @param typeName string with the type name
191 * @return <code>WildcardType</code> representation of
192 * <code>packageName</code> and <code>typeName</code>
194 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
195 return new WildcardTypeImpl(packageName, typeName);
199 * Creates instance of type {@link WildcardType WildcardType}.
201 * @param packageName string with the package name
202 * @param typeName string with the type name
203 * @param isPkNameNormalized if the package name has been normalized
204 * @param isTypeNormalized if the type name has been normalized
205 * @return <code>WildcardType</code> representation of
206 * <code>packageName</code> and <code>typeName</code>
208 public static WildcardType wildcardTypeFor(final String packageName, final String typeName,
209 final boolean isPkNameNormalized, final boolean isTypeNormalized,
210 final ModuleContext context) {
211 return new WildcardTypeImpl(packageName, typeName, isPkNameNormalized, isTypeNormalized, context);
215 * Creates instance of
216 * {@link ParameterizedType
217 * ParameterizedType} where raw type is
218 * {@link Augmentable} and actual
219 * parameter is <code>valueType</code>.
221 * @param valueType JAVA <code>Type</code> with actual parameter
222 * @return <code>ParametrizedType</code> representation of raw type
223 * <code>Augmentable</code> with actual parameter
224 * <code>valueType</code>
226 public static ParameterizedType augmentableTypeFor(final Type valueType) {
227 final Type augmentable = typeForClass(Augmentable.class);
228 return parameterizedTypeFor(augmentable, valueType);
232 * Creates instance of
233 * {@link ParameterizedType
234 * ParameterizedType} where raw type is
235 * {@link Augmentation} and actual
236 * parameter is <code>valueType</code>.
238 * @param valueType JAVA <code>Type</code> with actual parameter
239 * @return <code>ParametrizedType</code> reprezentation of raw type
240 * <code>Augmentation</code> with actual parameter
241 * <code>valueType</code>
243 public static ParameterizedType augmentationTypeFor(final Type valueType) {
244 final Type augmentation = typeForClass(Augmentation.class);
245 return parameterizedTypeFor(augmentation, valueType);
249 public static String getOuterClassName(final Type valueType) {
250 final String pkgName = valueType.getPackageName();
251 final int index = CharMatcher.javaUpperCase().indexIn(pkgName);
253 // It is inner class.
254 return Iterables.getFirst(DOT_SPLITTER.split(pkgName.substring(index)), null);
260 public static String getOuterClassPackageName(final Type valueType) {
261 final String pkgName = valueType.getPackageName();
262 final int index = CharMatcher.javaUpperCase().indexIn(pkgName);
264 return pkgName.substring(0, index - 1);
270 * Represents concrete JAVA type.
272 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
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>.
280 * @param pkName string with package name
281 * @param name string with the name of the type
283 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
284 super(pkName, name, true, null);
285 this.restrictions = restrictions;
289 public Restrictions getRestrictions() {
290 return this.restrictions;
295 * Represents concrete JAVA type with changed restriction values.
297 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements
298 BaseTypeWithRestrictions {
299 private final Restrictions restrictions;
302 * Creates instance of this class with package <code>pkName</code> and
303 * with the type name <code>name</code>.
305 * @param pkName string with package name
306 * @param name string with the name of the type
308 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
309 super(pkName, name, null);
310 this.restrictions = requireNonNull(restrictions);
313 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions,
314 final ModuleContext moduleContext) {
315 super(pkName, name, moduleContext);
316 this.restrictions = requireNonNull(restrictions);
320 public Restrictions getRestrictions() {
321 return this.restrictions;
326 * Represents parametrized JAVA type.
328 private static class ParameterizedTypeImpl extends AbstractBaseType implements ParameterizedType {
330 * Array of JAVA actual type parameters.
332 private final Type[] actualTypes;
335 * JAVA raw type (like List, Set, Map...).
337 private final Type rawType;
340 * Creates instance of this class with concrete rawType and array of
343 * @param rawType JAVA <code>Type</code> for raw type
344 * @param actTypes array of actual parameters
346 ParameterizedTypeImpl(final Type rawType, final Type[] actTypes) {
347 super(rawType.getPackageName(), rawType.getName(), true, null);
348 this.rawType = rawType;
349 this.actualTypes = actTypes.clone();
353 public Type[] getActualTypeArguments() {
355 return this.actualTypes;
359 public Type getRawType() {
365 * Represents JAVA bounded wildcard type.
367 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
369 * Creates instance of this class with concrete package and type name.
371 * @param packageName string with the package name
372 * @param typeName string with the name of type
374 //FIXME: doesn't seem to be called at all
375 WildcardTypeImpl(final String packageName, final String typeName) {
376 super(packageName, typeName, null);
380 * Creates instance of this class with concrete package and type name.
382 * @param packageName string with the package name
383 * @param typeName string with the name of type
384 * @param isPkNameNormalized if the package name has been normalized
385 * @param isTypeNormalized if the type name has been normalized
387 WildcardTypeImpl(final String packageName, final String typeName, final boolean isPkNameNormalized,
388 final boolean isTypeNormalized, final ModuleContext context) {
389 super(packageName, typeName, isPkNameNormalized, isTypeNormalized, context);
393 public static <T extends Number & Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
395 return new DefaultRestrictions<>(min, max);
398 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
399 private final RangeConstraint<T> rangeConstraint;
401 private DefaultRestrictions(final T min, final T max) {
404 this.rangeConstraint = new RangeConstraint<T>() {
407 public Optional<String> getErrorAppTag() {
408 return Optional.empty();
412 public Optional<String> getErrorMessage() {
413 return Optional.empty();
417 public Optional<String> getDescription() {
418 return Optional.empty();
422 public Optional<String> getReference() {
423 return Optional.empty();
427 public RangeSet<T> getAllowedRanges() {
428 return ImmutableRangeSet.of(Range.closed(min, max));
434 public boolean isEmpty() {
439 public Optional<RangeConstraint<?>> getRangeConstraint() {
440 return Optional.ofNullable(rangeConstraint);
444 public List<PatternConstraint> getPatternConstraints() {
445 return ImmutableList.of();
449 public Optional<LengthConstraint> getLengthConstraint() {
450 return Optional.empty();