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.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import java.util.List;
16 import java.util.concurrent.Future;
17 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
18 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
19 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
20 import org.opendaylight.yangtools.sal.binding.model.api.Type;
21 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
22 import org.opendaylight.yangtools.yang.binding.Augmentable;
23 import org.opendaylight.yangtools.yang.binding.Augmentation;
25 public final class Types {
26 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
27 new CacheLoader<Class<?>, ConcreteType>() {
29 public ConcreteType load(final Class<?> key) {
30 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
33 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
34 CacheBuilder.newBuilder().weakKeys().softValues().build(TYPE_LOADER);
36 private static final Type SET_TYPE = typeForClass(Set.class);
37 private static final Type LIST_TYPE = typeForClass(List.class);
38 private static final Type MAP_TYPE = typeForClass(Map.class);
40 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
41 public static final ConcreteType FUTURE = typeForClass(Future.class);
42 public static final ConcreteType STRING = typeForClass(String.class);
43 public static final ConcreteType VOID = typeForClass(Void.class);
44 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
45 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
48 * It is not desirable to create instance of this class
54 * Creates the instance of type
55 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
56 * ConcreteType} which represents JAVA <code>void</code> type.
58 * @return <code>ConcreteType</code> instance which represents JAVA
61 public static ConcreteType voidType() {
66 * Creates the instance of type
67 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
68 * ConcreteType} which represents primitive JAVA type for which package
71 * @param primitiveType
72 * string containing programmatic construction based on
73 * primitive type (e.g byte[])
74 * @return <code>ConcreteType</code> instance which represents programmatic
75 * construction with primitive JAVA type
77 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
78 return new ConcreteTypeImpl("", primitiveType, restrictions);
82 * Returns an instance of {@link ConcreteType} describing the class
86 * @return Description of class
88 public static ConcreteType typeForClass(final Class<?> cls) {
89 return TYPE_CACHE.getUnchecked(cls);
92 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
93 if (restrictions != null) {
94 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
96 return typeForClass(cls);
101 * Returns an instance of {@link ParameterizedType} describing the typed
102 * {@link Map}<K,V>
108 * @return Description of generic type instance
110 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
111 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
115 * Returns an instance of {@link ParameterizedType} describing the typed
116 * {@link Set}<V> with concrete type of value.
120 * @return Description of generic type instance of Set
122 public static ParameterizedType setTypeFor(final Type valueType) {
123 return parameterizedTypeFor(SET_TYPE, valueType);
127 * Returns an instance of {@link ParameterizedType} describing the typed
128 * {@link List}<V> with concrete type of value.
132 * @return Description of type instance of List
134 public static ParameterizedType listTypeFor(final Type valueType) {
135 return parameterizedTypeFor(LIST_TYPE, valueType);
139 * Creates instance of type
140 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
144 * JAVA <code>Type</code> for raw type
146 * JAVA <code>Type</code>s for actual parameter types
147 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
148 * and its parameters <code>parameters</code>
150 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
151 return new ParametrizedTypeImpl(type, parameters);
155 * Creates instance of type
156 * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
160 * string with the package name
162 * string with the type name
163 * @return <code>WildcardType</code> representation of
164 * <code>packageName</code> and <code>typeName</code>
166 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
167 return new WildcardTypeImpl(packageName, typeName);
171 * Creates instance of
172 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
173 * ParameterizedType} where raw type is
174 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
175 * parameter is <code>valueType</code>.
178 * JAVA <code>Type</code> with actual parameter
179 * @return <code>ParametrizedType</code> representation of raw type
180 * <code>Augmentable</code> with actual parameter
181 * <code>valueType</code>
183 public static ParameterizedType augmentableTypeFor(final Type valueType) {
184 final Type augmentable = typeForClass(Augmentable.class);
185 return parameterizedTypeFor(augmentable, valueType);
189 * Creates instance of
190 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
191 * ParameterizedType} where raw type is
192 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
193 * parameter is <code>valueType</code>.
196 * JAVA <code>Type</code> with actual parameter
197 * @return <code>ParametrizedType</code> reprezentation of raw type
198 * <code>Augmentation</code> with actual parameter
199 * <code>valueType</code>
201 public static ParameterizedType augmentationTypeFor(final Type valueType) {
202 final Type augmentation = typeForClass(Augmentation.class);
203 return parameterizedTypeFor(augmentation, valueType);
208 * Represents concrete JAVA type.
211 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
212 private final Restrictions restrictions;
215 * Creates instance of this class with package <code>pkName</code> and
216 * with the type name <code>name</code>.
219 * string with package name
221 * string with the name of the type
223 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
225 this.restrictions = restrictions;
229 public Restrictions getRestrictions() {
236 * Represents parametrized JAVA type.
239 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
241 * Array of JAVA actual type parameters.
243 private final Type[] actualTypes;
246 * JAVA raw type (like List, Set, Map...)
248 private final Type rawType;
251 public Type[] getActualTypeArguments() {
257 public Type getRawType() {
262 * Creates instance of this class with concrete rawType and array of
266 * JAVA <code>Type</code> for raw type
268 * array of actual parameters
270 public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
271 super(rawType.getPackageName(), rawType.getName());
272 this.rawType = rawType;
273 this.actualTypes = actTypes.clone();
280 * Represents JAVA bounded wildcard type.
283 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
285 * Creates instance of this class with concrete package and type name.
288 * string with the package name
290 * string with the name of type
292 public WildcardTypeImpl(final String packageName, final String typeName) {
293 super(packageName, typeName);