2 * Copyright (c) 2016 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.binding2.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.binding2.model.api.BaseTypeWithRestrictions;
27 import org.opendaylight.mdsal.binding2.model.api.ConcreteType;
28 import org.opendaylight.mdsal.binding2.model.api.ParameterizedType;
29 import org.opendaylight.mdsal.binding2.model.api.Restrictions;
30 import org.opendaylight.mdsal.binding2.model.api.Type;
31 import org.opendaylight.mdsal.binding2.model.api.WildcardType;
32 import org.opendaylight.mdsal.binding2.spec.Augmentable;
33 import org.opendaylight.mdsal.binding2.spec.Augmentation;
34 import org.opendaylight.mdsal.binding2.spec.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 {
43 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
44 public static final ConcreteType RPC_CALLBACK = typeForClass(RpcCallback.class);
45 public static final ConcreteType STRING = typeForClass(String.class);
46 public static final ConcreteType VOID = typeForClass(Void.class);
47 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
48 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
50 private static final Splitter DOT_SPLITTER = Splitter.on('.');
51 private static final Type SET_TYPE = typeForClass(Set.class);
52 private static final Type LIST_TYPE = typeForClass(List.class);
53 private static final Type MAP_TYPE = typeForClass(Map.class);
55 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
56 new CacheLoader<Class<?>, ConcreteType>() {
59 public ConcreteType load(Class<?> key) throws Exception {
60 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
64 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
65 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
69 throw new UnsupportedOperationException("Utility class");
73 * Creates the instance of type
74 * {@link org.opendaylight.mdsal.binding2.model.api.ConcreteType
75 * ConcreteType} which represents JAVA <code>void</code> type.
77 * @return <code>ConcreteType</code> instance which represents JAVA
80 public static ConcreteType voidType() {
85 * Creates the instance of type
86 * {@link org.opendaylight.mdsal.binding2.model.api.ConcreteType
87 * ConcreteType} which represents primitive JAVA type for which package
90 * @param primitiveType
91 * string containing programmatic construction based on
92 * primitive type (e.g byte[])
93 * @return <code>ConcreteType</code> instance which represents programmatic
94 * construction with primitive JAVA type
96 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
97 return new ConcreteTypeImpl("", primitiveType, restrictions);
101 * Returns an instance of {@link ConcreteType} describing the class
105 * @return Description of class
107 public static ConcreteType typeForClass(final Class<?> cls) {
108 return TYPE_CACHE.getUnchecked(cls);
112 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
113 if (restrictions != null) {
114 if (restrictions instanceof DefaultRestrictions) {
115 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
117 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
120 return typeForClass(cls);
125 * Returns an instance of {@link ParameterizedType} describing the typed
126 * {@link Map}<K,V>
132 * @return Description of generic type instance
134 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
135 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
139 * Returns an instance of {@link ParameterizedType} describing the typed
140 * {@link Set}<V> with concrete type of value.
144 * @return Description of generic type instance of Set
146 public static ParameterizedType setTypeFor(final Type valueType) {
147 return parameterizedTypeFor(SET_TYPE, valueType);
151 * Returns an instance of {@link ParameterizedType} describing the typed
152 * {@link List}<V> with concrete type of value.
156 * @return Description of type instance of List
158 public static ParameterizedType listTypeFor(final Type valueType) {
159 return parameterizedTypeFor(LIST_TYPE, valueType);
163 * Creates instance of type
164 * {@link org.opendaylight.mdsal.binding2.model.api.ParameterizedType
168 * JAVA <code>Type</code> for raw type
170 * JAVA <code>Type</code>s for actual parameter types
171 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
172 * and its parameters <code>parameters</code>
174 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
175 return new ParameterizedTypeImpl(type, parameters);
179 * Creates instance of type
180 * {@link org.opendaylight.mdsal.binding2.model.api.WildcardType
184 * string with the package name
186 * string with the type name
187 * @return <code>WildcardType</code> representation of
188 * <code>packageName</code> and <code>typeName</code>
190 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
191 return new WildcardTypeImpl(packageName, typeName);
195 * Creates instance of
196 * {@link org.opendaylight.mdsal.binding2.model.api.ParameterizedType
197 * ParameterizedType} where raw type is
198 * {@link org.opendaylight.mdsal.binding2.spec.Augmentable} and actual
199 * parameter is <code>valueType</code>.
202 * JAVA <code>Type</code> with actual parameter
203 * @return <code>ParametrizedType</code> representation of raw type
204 * <code>Augmentable</code> with actual parameter
205 * <code>valueType</code>
207 public static ParameterizedType augmentableTypeFor(final Type valueType) {
208 final Type augmentable = typeForClass(Augmentable.class);
209 return parameterizedTypeFor(augmentable, valueType);
213 * Creates instance of
214 * {@link org.opendaylight.mdsal.binding2.model.api.ParameterizedType
215 * ParameterizedType} where raw type is
216 * {@link org.opendaylight.mdsal.binding2.spec.Augmentation} and actual
217 * parameter is <code>valueType</code>.
220 * JAVA <code>Type</code> with actual parameter
221 * @return <code>ParametrizedType</code> reprezentation of raw type
222 * <code>Augmentation</code> with actual parameter
223 * <code>valueType</code>
225 public static ParameterizedType augmentationTypeFor(final Type valueType) {
226 final Type augmentation = typeForClass(Augmentation.class);
227 return parameterizedTypeFor(augmentation, valueType);
231 public static @Nullable
232 String getOuterClassName(final Type valueType) {
233 final String pkgName = valueType.getPackageName();
234 if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) {
235 // It is inner class.
236 return Iterables.getLast(DOT_SPLITTER.split(pkgName));
243 * Represents concrete JAVA type.
246 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
248 private final Restrictions restrictions;
251 * Creates instance of this class with package <code>pkName</code> and
252 * with the type name <code>name</code>.
255 * string with package name
257 * string with the name of the type
259 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
261 this.restrictions = restrictions;
265 public Restrictions getRestrictions() {
272 * Represents concrete JAVA type with changed restriction values.
275 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
276 private final Restrictions restrictions;
279 * Creates instance of this class with package <code>pkName</code> and
280 * with the type name <code>name</code>.
283 * string with package name
285 * string with the name of the type
287 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
289 this.restrictions = Preconditions.checkNotNull(restrictions);
293 public Restrictions getRestrictions() {
300 * Represents parametrized JAVA type.
303 private static class ParameterizedTypeImpl extends AbstractBaseType implements ParameterizedType {
305 * Array of JAVA actual type parameters.
307 private final Type[] actualTypes;
310 * JAVA raw type (like List, Set, Map...)
312 private final Type rawType;
315 public Type[] getActualTypeArguments() {
321 public Type getRawType() {
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());
336 this.rawType = rawType;
337 this.actualTypes = actTypes.clone();
344 * Represents JAVA bounded wildcard type.
347 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
349 * Creates instance of this class with concrete package and type name.
352 * string with the package name
354 * string with the name of type
356 public WildcardTypeImpl(final String packageName, final String typeName) {
357 super(packageName, typeName);
361 public static <T extends Number> DefaultRestrictions<T> getDefaultRestrictions(final T min, final T max) {
362 return new DefaultRestrictions<>(min, max);
365 private static final class DefaultRestrictions<T extends Number> implements Restrictions {
368 private final List<RangeConstraint> rangeConstraints;
370 private DefaultRestrictions(final T min, final T max) {
371 this.min = Preconditions.checkNotNull(min);
372 this.max = Preconditions.checkNotNull(max);
373 this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
374 .absent(), Optional.absent()));
378 public boolean isEmpty() {
383 public List<RangeConstraint> getRangeConstraints() {
384 return rangeConstraints;
388 public List<PatternConstraint> getPatternConstraints() {
389 return ImmutableList.of();
393 public List<LengthConstraint> getLengthConstraints() {
394 return ImmutableList.of();