*/
package org.opendaylight.yangtools.binding.generator.util;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.opendaylight.yangtools.yang.binding.Augmentation;
public final class Types {
+ private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
+ new CacheLoader<Class<?>, ConcreteType>() {
+ @Override
+ public ConcreteType load(final Class<?> key) {
+ return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
+ }
+ };
+ private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
+ CacheBuilder.newBuilder().weakKeys().softValues().build(TYPE_LOADER);
+
private static final Type SET_TYPE = typeForClass(Set.class);
private static final Type LIST_TYPE = typeForClass(List.class);
private static final Type MAP_TYPE = typeForClass(Map.class);
public static final ConcreteType FUTURE = typeForClass(Future.class);
public static final ConcreteType STRING = typeForClass(String.class);
public static final ConcreteType VOID = typeForClass(Void.class);
+ public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
+ public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
/**
* It is not desirable to create instance of this class
* doesn't exist.
*
* @param primitiveType
- * string containing programaticall construction based on
+ * string containing programmatic construction based on
* primitive type (e.g byte[])
- * @return <code>ConcreteType</code> instance which represents programatic
+ * @return <code>ConcreteType</code> instance which represents programmatic
* construction with primitive JAVA type
*/
- public static Type primitiveType(final String primitiveType, final Restrictions restrictions) {
+ public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
return new ConcreteTypeImpl("", primitiveType, restrictions);
}
* Class to describe
* @return Description of class
*/
- public static ConcreteType typeForClass(Class<?> cls) {
- return typeForClass(cls, null);
+ public static ConcreteType typeForClass(final Class<?> cls) {
+ return TYPE_CACHE.getUnchecked(cls);
}
- public static ConcreteType typeForClass(Class<?> cls, Restrictions restrictions) {
- return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+ public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
+ if (restrictions != null) {
+ return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+ } else {
+ return typeForClass(cls);
+ }
}
/**
* Value Type
* @return Description of generic type instance
*/
- public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {
+ public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
}
* Value Type
* @return Description of generic type instance of Set
*/
- public static ParameterizedType setTypeFor(Type valueType) {
+ public static ParameterizedType setTypeFor(final Type valueType) {
return parameterizedTypeFor(SET_TYPE, valueType);
}
* Value Type
* @return Description of type instance of List
*/
- public static ParameterizedType listTypeFor(Type valueType) {
+ public static ParameterizedType listTypeFor(final Type valueType) {
return parameterizedTypeFor(LIST_TYPE, valueType);
}
* @return <code>ParametrizedType</code> reprezentation of <code>type</code>
* and its parameters <code>parameters</code>
*/
- public static ParameterizedType parameterizedTypeFor(Type type, Type... parameters) {
+ public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
return new ParametrizedTypeImpl(type, parameters);
}
* string with the package name
* @param typeName
* string with the type name
- * @return <code>WildcardType</code> reprezentation of
+ * @return <code>WildcardType</code> representation of
* <code>packageName</code> and <code>typeName</code>
*/
- public static WildcardType wildcardTypeFor(String packageName, String typeName) {
+ public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
return new WildcardTypeImpl(packageName, typeName);
}
*
* @param valueType
* JAVA <code>Type</code> with actual parameter
- * @return <code>ParametrizedType</code> reprezentation of raw type
+ * @return <code>ParametrizedType</code> representation of raw type
* <code>Augmentable</code> with actual parameter
* <code>valueType</code>
*/
- public static ParameterizedType augmentableTypeFor(Type valueType) {
+ public static ParameterizedType augmentableTypeFor(final Type valueType) {
final Type augmentable = typeForClass(Augmentable.class);
return parameterizedTypeFor(augmentable, valueType);
}
* <code>Augmentation</code> with actual parameter
* <code>valueType</code>
*/
- public static ParameterizedType augmentationTypeFor(Type valueType) {
+ public static ParameterizedType augmentationTypeFor(final Type valueType) {
final Type augmentation = typeForClass(Augmentation.class);
return parameterizedTypeFor(augmentation, valueType);
}
* @param name
* string with the name of the type
*/
- private ConcreteTypeImpl(String pkName, String name, Restrictions restrictions) {
+ private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
super(pkName, name);
this.restrictions = restrictions;
}
/**
* Array of JAVA actual type parameters.
*/
- private Type[] actualTypes;
+ private final Type[] actualTypes;
/**
* JAVA raw type (like List, Set, Map...)
*/
- private Type rawType;
+ private final Type rawType;
@Override
public Type[] getActualTypeArguments() {
* @param actTypes
* array of actual parameters
*/
- public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {
+ public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
super(rawType.getPackageName(), rawType.getName());
this.rawType = rawType;
this.actualTypes = Arrays.copyOf(actTypes, actTypes.length);
* @param typeName
* string with the name of type
*/
- public WildcardTypeImpl(String packageName, String typeName) {
+ public WildcardTypeImpl(final String packageName, final String typeName) {
super(packageName, typeName);
}
}
* @return java <code>Type</code> representation of <code>type</code>
*/
@Override
- public Type javaTypeForYangType(String type) {
+ public Type javaTypeForYangType(final String type) {
return typeMap.get(type);
}
* returned.
*/
@Override
- public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode) {
+ public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode) {
if (type != null) {
return typeMap.get(type.getQName().getLocalName());
}
}
@Override
- public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode,
- Restrictions restrictions) {
+ public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode,
+ final Restrictions restrictions) {
String typeName = type.getQName().getLocalName();
switch (typeName) {
case "binary":
- return Types.primitiveType("byte[]", restrictions);
+ return restrictions == null ? Types.BYTE_ARRAY : Types.primitiveType("byte[]", restrictions);
case "decimal64":
return Types.typeForClass(BigDecimal.class, restrictions);
case "enumeration":
}
@Override
- public String getTypeDefaultConstruction(LeafSchemaNode node) {
+ public String getTypeDefaultConstruction(final LeafSchemaNode node) {
return null;
}
@Override
- public String getConstructorPropertyName(SchemaNode node) {
+ public String getConstructorPropertyName(final SchemaNode node) {
return null;
}
@Override
- public String getParamNameFromType(TypeDefinition<?> type) {
+ public String getParamNameFromType(final TypeDefinition<?> type) {
return "_" + BindingGeneratorUtil.parseToValidParamName(type.getQName().getLocalName());
}
};