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.yangtools.binding.generator.util;
10 import com.google.common.base.CharMatcher;
11 import com.google.common.base.Splitter;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import com.google.common.collect.Iterables;
16 import java.util.List;
19 import java.util.concurrent.Future;
20 import javax.annotation.Nullable;
21 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
22 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
24 import org.opendaylight.yangtools.sal.binding.model.api.Type;
25 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
26 import org.opendaylight.yangtools.yang.binding.Augmentable;
27 import org.opendaylight.yangtools.yang.binding.Augmentation;
29 public final class Types {
30 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
31 new CacheLoader<Class<?>, ConcreteType>() {
33 public ConcreteType load(final Class<?> key) {
34 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
37 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
38 CacheBuilder.newBuilder().weakKeys().softValues().build(TYPE_LOADER);
40 private static final Type SET_TYPE = typeForClass(Set.class);
41 private static final Type LIST_TYPE = typeForClass(List.class);
42 private static final Type MAP_TYPE = typeForClass(Map.class);
44 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
45 public static final ConcreteType FUTURE = typeForClass(Future.class);
46 public static final ConcreteType STRING = typeForClass(String.class);
47 public static final ConcreteType VOID = typeForClass(Void.class);
48 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
49 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
50 private static final Splitter DOT_SPLITTER = Splitter.on('.');
53 * It is not desirable to create instance of this class
59 * Creates the instance of type
60 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
61 * ConcreteType} which represents JAVA <code>void</code> type.
63 * @return <code>ConcreteType</code> instance which represents JAVA
66 public static ConcreteType voidType() {
71 * Creates the instance of type
72 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
73 * ConcreteType} which represents primitive JAVA type for which package
76 * @param primitiveType
77 * string containing programmatic construction based on
78 * primitive type (e.g byte[])
79 * @return <code>ConcreteType</code> instance which represents programmatic
80 * construction with primitive JAVA type
82 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
83 return new ConcreteTypeImpl("", primitiveType, restrictions);
87 * Returns an instance of {@link ConcreteType} describing the class
91 * @return Description of class
93 public static ConcreteType typeForClass(final Class<?> cls) {
94 return TYPE_CACHE.getUnchecked(cls);
97 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
98 if (restrictions != null) {
99 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
101 return typeForClass(cls);
106 * Returns an instance of {@link ParameterizedType} describing the typed
107 * {@link Map}<K,V>
113 * @return Description of generic type instance
115 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
116 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
120 * Returns an instance of {@link ParameterizedType} describing the typed
121 * {@link Set}<V> with concrete type of value.
125 * @return Description of generic type instance of Set
127 public static ParameterizedType setTypeFor(final Type valueType) {
128 return parameterizedTypeFor(SET_TYPE, valueType);
132 * Returns an instance of {@link ParameterizedType} describing the typed
133 * {@link List}<V> with concrete type of value.
137 * @return Description of type instance of List
139 public static ParameterizedType listTypeFor(final Type valueType) {
140 return parameterizedTypeFor(LIST_TYPE, valueType);
144 * Creates instance of type
145 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
149 * JAVA <code>Type</code> for raw type
151 * JAVA <code>Type</code>s for actual parameter types
152 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
153 * and its parameters <code>parameters</code>
155 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
156 return new ParametrizedTypeImpl(type, parameters);
160 * Creates instance of type
161 * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
165 * string with the package name
167 * string with the type name
168 * @return <code>WildcardType</code> representation of
169 * <code>packageName</code> and <code>typeName</code>
171 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
172 return new WildcardTypeImpl(packageName, typeName);
176 * Creates instance of
177 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
178 * ParameterizedType} where raw type is
179 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
180 * parameter is <code>valueType</code>.
183 * JAVA <code>Type</code> with actual parameter
184 * @return <code>ParametrizedType</code> representation of raw type
185 * <code>Augmentable</code> with actual parameter
186 * <code>valueType</code>
188 public static ParameterizedType augmentableTypeFor(final Type valueType) {
189 final Type augmentable = typeForClass(Augmentable.class);
190 return parameterizedTypeFor(augmentable, valueType);
194 * Creates instance of
195 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
196 * ParameterizedType} where raw type is
197 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
198 * parameter is <code>valueType</code>.
201 * JAVA <code>Type</code> with actual parameter
202 * @return <code>ParametrizedType</code> reprezentation of raw type
203 * <code>Augmentation</code> with actual parameter
204 * <code>valueType</code>
206 public static ParameterizedType augmentationTypeFor(final Type valueType) {
207 final Type augmentation = typeForClass(Augmentation.class);
208 return parameterizedTypeFor(augmentation, valueType);
212 public static @Nullable String getOuterClassName(final Type valueType) {
213 final String pkgName = valueType.getPackageName();
214 if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) {
215 // It is inner class.
216 return Iterables.getLast(DOT_SPLITTER.split(pkgName));
223 * Represents concrete JAVA type.
226 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
227 private final Restrictions restrictions;
230 * Creates instance of this class with package <code>pkName</code> and
231 * with the type name <code>name</code>.
234 * string with package name
236 * string with the name of the type
238 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
240 this.restrictions = restrictions;
244 public Restrictions getRestrictions() {
251 * Represents parametrized JAVA type.
254 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
256 * Array of JAVA actual type parameters.
258 private final Type[] actualTypes;
261 * JAVA raw type (like List, Set, Map...)
263 private final Type rawType;
266 public Type[] getActualTypeArguments() {
272 public Type getRawType() {
277 * Creates instance of this class with concrete rawType and array of
281 * JAVA <code>Type</code> for raw type
283 * array of actual parameters
285 public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
286 super(rawType.getPackageName(), rawType.getName());
287 this.rawType = rawType;
288 this.actualTypes = actTypes.clone();
295 * Represents JAVA bounded wildcard type.
298 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
300 * Creates instance of this class with concrete package and type name.
303 * string with the package name
305 * string with the name of type
307 public WildcardTypeImpl(final String packageName, final String typeName) {
308 super(packageName, typeName);