2 * Copyright (c) 2013 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
8 package org.opendaylight.mdsal.binding.generator.util;
10 import com.google.common.base.CharMatcher;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Splitter;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import com.google.common.collect.Iterables;
18 import java.util.Collections;
19 import java.util.List;
22 import java.util.concurrent.Future;
23 import javax.annotation.Nullable;
24 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
25 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
26 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
27 import org.opendaylight.mdsal.binding.model.api.Restrictions;
28 import org.opendaylight.mdsal.binding.model.api.Type;
29 import org.opendaylight.mdsal.binding.model.api.WildcardType;
30 import org.opendaylight.yangtools.yang.binding.Augmentable;
31 import org.opendaylight.yangtools.yang.binding.Augmentation;
32 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
33 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
34 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
35 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
37 public final class Types {
38 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
39 new CacheLoader<Class<?>, ConcreteType>() {
41 public ConcreteType load(final Class<?> key) {
42 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
45 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
46 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
48 private static final Type SET_TYPE = typeForClass(Set.class);
49 private static final Type LIST_TYPE = typeForClass(List.class);
50 private static final Type MAP_TYPE = typeForClass(Map.class);
52 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
53 public static final ConcreteType FUTURE = typeForClass(Future.class);
54 public static final ConcreteType STRING = typeForClass(String.class);
55 public static final ConcreteType VOID = typeForClass(Void.class);
56 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
57 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
58 private static final Splitter DOT_SPLITTER = Splitter.on('.');
61 * It is not desirable to create instance of this class
67 * Creates the instance of type
68 * {@link org.opendaylight.mdsal.binding.model.api.ConcreteType
69 * ConcreteType} which represents JAVA <code>void</code> type.
71 * @return <code>ConcreteType</code> instance which represents JAVA
74 public static ConcreteType voidType() {
79 * Creates the instance of type
80 * {@link org.opendaylight.mdsal.binding.model.api.ConcreteType
81 * ConcreteType} which represents primitive JAVA type for which package
84 * @param primitiveType
85 * string containing programmatic construction based on
86 * primitive type (e.g byte[])
89 * @return <code>ConcreteType</code> instance which represents programmatic
90 * construction with primitive JAVA type
92 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
93 return new ConcreteTypeImpl("", primitiveType, restrictions);
97 * Returns an instance of {@link ConcreteType} describing the class
101 * @return Description of class
103 public static ConcreteType typeForClass(final Class<?> cls) {
104 return TYPE_CACHE.getUnchecked(cls);
107 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
108 if (restrictions != null) {
109 if (restrictions instanceof DefaultRestrictions) {
110 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
112 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
115 return typeForClass(cls);
120 * Returns an instance of {@link ParameterizedType} describing the typed
121 * {@link Map}<K,V>
127 * @return Description of generic type instance
129 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
130 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
134 * Returns an instance of {@link ParameterizedType} describing the typed
135 * {@link Set}<V> with concrete type of value.
139 * @return Description of generic type instance of Set
141 public static ParameterizedType setTypeFor(final Type valueType) {
142 return parameterizedTypeFor(SET_TYPE, valueType);
146 * Returns an instance of {@link ParameterizedType} describing the typed
147 * {@link List}<V> with concrete type of value.
151 * @return Description of type instance of List
153 public static ParameterizedType listTypeFor(final Type valueType) {
154 return parameterizedTypeFor(LIST_TYPE, valueType);
158 * Creates instance of type
159 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
163 * JAVA <code>Type</code> for raw type
165 * JAVA <code>Type</code>s for actual parameter types
166 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
167 * and its parameters <code>parameters</code>
169 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
170 return new ParametrizedTypeImpl(type, parameters);
174 * Creates instance of type
175 * {@link org.opendaylight.mdsal.binding.model.api.WildcardType
179 * string with the package name
181 * string with the type name
182 * @return <code>WildcardType</code> representation of
183 * <code>packageName</code> and <code>typeName</code>
185 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
186 return new WildcardTypeImpl(packageName, typeName);
190 * Creates instance of
191 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
192 * ParameterizedType} where raw type is
193 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
194 * parameter is <code>valueType</code>.
197 * JAVA <code>Type</code> with actual parameter
198 * @return <code>ParametrizedType</code> representation of raw type
199 * <code>Augmentable</code> with actual parameter
200 * <code>valueType</code>
202 public static ParameterizedType augmentableTypeFor(final Type valueType) {
203 final Type augmentable = typeForClass(Augmentable.class);
204 return parameterizedTypeFor(augmentable, valueType);
208 * Creates instance of
209 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
210 * ParameterizedType} where raw type is
211 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
212 * parameter is <code>valueType</code>.
215 * JAVA <code>Type</code> with actual parameter
216 * @return <code>ParametrizedType</code> reprezentation of raw type
217 * <code>Augmentation</code> with actual parameter
218 * <code>valueType</code>
220 public static ParameterizedType augmentationTypeFor(final Type valueType) {
221 final Type augmentation = typeForClass(Augmentation.class);
222 return parameterizedTypeFor(augmentation, valueType);
226 public static @Nullable String getOuterClassName(final Type valueType) {
227 final String pkgName = valueType.getPackageName();
228 if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) {
229 // It is inner class.
230 return Iterables.getLast(DOT_SPLITTER.split(pkgName));
237 * Represents concrete JAVA type.
240 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
241 private final Restrictions restrictions;
244 * Creates instance of this class with package <code>pkName</code> and
245 * with the type name <code>name</code>.
248 * string with package name
250 * string with the name of the type
252 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
254 this.restrictions = restrictions;
258 public Restrictions getRestrictions() {
259 return this.restrictions;
265 * Represents concrete JAVA type with changed restriction values.
268 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
269 private final Restrictions restrictions;
272 * Creates instance of this class with package <code>pkName</code> and
273 * with the type name <code>name</code>.
276 * string with package name
278 * string with the name of the type
280 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
282 this.restrictions = Preconditions.checkNotNull(restrictions);
286 public Restrictions getRestrictions() {
287 return this.restrictions;
293 * Represents parametrized JAVA type.
296 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
298 * Array of JAVA actual type parameters.
300 private final Type[] actualTypes;
303 * JAVA raw type (like List, Set, Map...)
305 private final Type rawType;
308 public Type[] getActualTypeArguments() {
310 return this.actualTypes;
314 public Type getRawType() {
319 * Creates instance of this class with concrete rawType and array of
323 * JAVA <code>Type</code> for raw type
325 * array of actual parameters
327 public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
328 super(rawType.getPackageName(), rawType.getName());
329 this.rawType = rawType;
330 this.actualTypes = actTypes.clone();
337 * Represents JAVA bounded wildcard type.
340 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
342 * Creates instance of this class with concrete package and type name.
345 * string with the package name
347 * string with the name of type
349 public WildcardTypeImpl(final String packageName, final String typeName) {
350 super(packageName, typeName);
354 public static <T extends Number> DefaultRestrictions<T> getDefaultRestrictions(final T min, final T max) {
355 return new DefaultRestrictions<>(min, max);
358 private static final class DefaultRestrictions<T extends Number> implements Restrictions {
361 private final List<RangeConstraint> rangeConstraints;
363 private DefaultRestrictions(final T min, final T max) {
364 this.min = Preconditions.checkNotNull(min);
365 this.max = Preconditions.checkNotNull(max);
366 this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
367 .<String>absent(), Optional.<String>absent()));
371 public boolean isEmpty() {
376 public List<RangeConstraint> getRangeConstraints() {
377 return this.rangeConstraints;
381 public List<PatternConstraint> getPatternConstraints() {
382 return Collections.emptyList();
386 public List<LengthConstraint> getLengthConstraints() {
387 return Collections.emptyList();