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.Arrays;
14 import java.util.List;
17 import java.util.concurrent.Future;
18 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
19 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
20 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
21 import org.opendaylight.yangtools.sal.binding.model.api.Type;
22 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
23 import org.opendaylight.yangtools.yang.binding.Augmentable;
24 import org.opendaylight.yangtools.yang.binding.Augmentation;
26 public final class Types {
27 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
28 new CacheLoader<Class<?>, ConcreteType>() {
30 public ConcreteType load(final Class<?> key) {
31 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
34 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
35 CacheBuilder.newBuilder().weakKeys().softValues().build(TYPE_LOADER);
37 private static final Type SET_TYPE = typeForClass(Set.class);
38 private static final Type LIST_TYPE = typeForClass(List.class);
39 private static final Type MAP_TYPE = typeForClass(Map.class);
41 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
42 public static final ConcreteType FUTURE = typeForClass(Future.class);
43 public static final ConcreteType STRING = typeForClass(String.class);
44 public static final ConcreteType VOID = typeForClass(Void.class);
45 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
46 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
49 * It is not desirable to create instance of this class
55 * Creates the instance of type
56 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
57 * ConcreteType} which represents JAVA <code>void</code> type.
59 * @return <code>ConcreteType</code> instance which represents JAVA
62 public static ConcreteType voidType() {
67 * Creates the instance of type
68 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
69 * ConcreteType} which represents primitive JAVA type for which package
72 * @param primitiveType
73 * string containing programmatic construction based on
74 * primitive type (e.g byte[])
75 * @return <code>ConcreteType</code> instance which represents programmatic
76 * construction with primitive JAVA type
78 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
79 return new ConcreteTypeImpl("", primitiveType, restrictions);
83 * Returns an instance of {@link ConcreteType} describing the class
87 * @return Description of class
89 public static ConcreteType typeForClass(final Class<?> cls) {
90 return TYPE_CACHE.getUnchecked(cls);
93 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
94 if (restrictions != null) {
95 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
97 return typeForClass(cls);
102 * Returns an instance of {@link ParameterizedType} describing the typed
103 * {@link Map}<K,V>
109 * @return Description of generic type instance
111 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
112 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
116 * Returns an instance of {@link ParameterizedType} describing the typed
117 * {@link Set}<V> with concrete type of value.
121 * @return Description of generic type instance of Set
123 public static ParameterizedType setTypeFor(final Type valueType) {
124 return parameterizedTypeFor(SET_TYPE, valueType);
128 * Returns an instance of {@link ParameterizedType} describing the typed
129 * {@link List}<V> with concrete type of value.
133 * @return Description of type instance of List
135 public static ParameterizedType listTypeFor(final Type valueType) {
136 return parameterizedTypeFor(LIST_TYPE, valueType);
140 * Creates instance of type
141 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
145 * JAVA <code>Type</code> for raw type
147 * JAVA <code>Type</code>s for actual parameter types
148 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
149 * and its parameters <code>parameters</code>
151 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
152 return new ParametrizedTypeImpl(type, parameters);
156 * Creates instance of type
157 * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
161 * string with the package name
163 * string with the type name
164 * @return <code>WildcardType</code> representation of
165 * <code>packageName</code> and <code>typeName</code>
167 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
168 return new WildcardTypeImpl(packageName, typeName);
172 * Creates instance of
173 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
174 * ParameterizedType} where raw type is
175 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
176 * parameter is <code>valueType</code>.
179 * JAVA <code>Type</code> with actual parameter
180 * @return <code>ParametrizedType</code> representation of raw type
181 * <code>Augmentable</code> with actual parameter
182 * <code>valueType</code>
184 public static ParameterizedType augmentableTypeFor(final Type valueType) {
185 final Type augmentable = typeForClass(Augmentable.class);
186 return parameterizedTypeFor(augmentable, valueType);
190 * Creates instance of
191 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
192 * ParameterizedType} where raw type is
193 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
194 * parameter is <code>valueType</code>.
197 * JAVA <code>Type</code> with actual parameter
198 * @return <code>ParametrizedType</code> reprezentation of raw type
199 * <code>Augmentation</code> with actual parameter
200 * <code>valueType</code>
202 public static ParameterizedType augmentationTypeFor(final Type valueType) {
203 final Type augmentation = typeForClass(Augmentation.class);
204 return parameterizedTypeFor(augmentation, valueType);
209 * Represents concrete JAVA type.
212 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
213 private final Restrictions restrictions;
216 * Creates instance of this class with package <code>pkName</code> and
217 * with the type name <code>name</code>.
220 * string with package name
222 * string with the name of the type
224 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
226 this.restrictions = restrictions;
230 public Restrictions getRestrictions() {
237 * Represents parametrized JAVA type.
240 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
242 * Array of JAVA actual type parameters.
244 private final Type[] actualTypes;
247 * JAVA raw type (like List, Set, Map...)
249 private final Type rawType;
252 public Type[] getActualTypeArguments() {
258 public Type getRawType() {
263 * Creates instance of this class with concrete rawType and array of
267 * JAVA <code>Type</code> for raw type
269 * array of actual parameters
271 public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
272 super(rawType.getPackageName(), rawType.getName());
273 this.rawType = rawType;
274 this.actualTypes = Arrays.copyOf(actTypes, actTypes.length);
281 * Represents JAVA bounded wildcard type.
284 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
286 * Creates instance of this class with concrete package and type name.
289 * string with the package name
291 * string with the name of type
293 public WildcardTypeImpl(final String packageName, final String typeName) {
294 super(packageName, typeName);