org.beanio.internal.util.TypeUtil Maven / Gradle / Ivy
/*
* Copyright 2010-2011 Kevin Seim
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.beanio.internal.util;
import java.math.*;
import java.net.URL;
import java.util.*;
/**
* Utility class for working with Java types supported by BeanIO.
*
* @author Kevin Seim
* @since 1.0
*/
public class TypeUtil {
/**
* Alias for a java.util.Date that includes both date and time information.
* If a type handler is registered using this alias, the registered type handler will
* become the default type handler for all Date classes.
*/
public static final String DATETIME_ALIAS = "datetime";
/** Alias for the java.util.Date class that includes only date information */
public static final String DATE_ALIAS = "date";
/** Alias for a java.util.Date that only includes only time information */
public static final String TIME_ALIAS = "time";
/** Class type used to indicate a Java array */
public static final Class> ARRAY_TYPE = ArrayCollection.class;
/**
* Cannot instantiate.
*/
private TypeUtil() { }
/**
* Returns true if to.isAssignableFrom(from) after converting
* primitive values of to to its object counterpart.
* @param to the class or primitive to test assignability to
* @param from the class to test assignability from
* @return true if to is assignable from from
*/
public static boolean isAssignable(Class to, Class from) {
return toWrapperClass(to).isAssignableFrom(from);
}
/**
* Converts primitive types to their wrapper counterparts.
* @param type the class type to convert
* @return the wrapper equivalent for the primitive type, or if type
* was not a primitive, its returned as is
*/
public static Class toWrapperClass(Class type) {
if (!type.isPrimitive())
return type;
else if (int.class.equals(type))
return Integer.class;
else if (double.class.equals(type))
return Double.class;
else if (char.class.equals(type))
return Character.class;
else if (boolean.class.equals(type))
return Boolean.class;
else if (long.class.equals(type))
return Long.class;
else if (float.class.equals(type))
return Float.class;
else if (short.class.equals(type))
return Short.class;
else if (byte.class.equals(type))
return Byte.class;
else
throw new IllegalArgumentException("Primitive type not supported: " + type.getName());
}
/**
* Returns the Class object for a class name or type alias. A type alias is not
* case sensitive. The following type aliases are supported:
*
* Alias Class or Primitive
* string java.lang.String
* boolean java.lang.Boolean
* byte java.lang.Byte
* int java.lang.Integer
* integer java.lang.Integer
* short java.lang.Short<
* char java.lang.Character
* character java.lang.Character
* long java.lang.Long
* float java.lang.Float
* double java.lang.Double
* bigdecimal java.math.BigDecimal
* decimal java.math.BigDecimal
* biginteger java.math.BigInteger
* uuid java.util.UUID
* url java.net.URL
* date java.util.Date
* time java.util.Date
* datetime java.util.Date
*
*
* @param classLoader the {@link ClassLoader} for loading classes
* @param type the fully qualified class name or type alias
* @return the class, or null if the type name is invalid
*/
public static Class toType(ClassLoader classLoader, String type) {
if ("string".equalsIgnoreCase(type))
return String.class;
else if ("boolean".equalsIgnoreCase(type))
return Boolean.class;
else if ("byte".equalsIgnoreCase(type))
return Byte.class;
else if ("char".equalsIgnoreCase(type))
return Character.class;
else if ("character".equalsIgnoreCase(type))
return Character.class;
else if ("short".equalsIgnoreCase(type))
return Short.class;
else if ("int".equalsIgnoreCase(type))
return Integer.class;
else if ("Integer".equalsIgnoreCase(type))
return Integer.class;
else if ("long".equalsIgnoreCase(type))
return Long.class;
else if ("float".equalsIgnoreCase(type))
return Float.class;
else if ("double".equalsIgnoreCase(type))
return Double.class;
else if ("bigdecimal".equalsIgnoreCase(type))
return BigDecimal.class;
else if ("decimal".equalsIgnoreCase(type))
return BigDecimal.class;
else if ("biginteger".equalsIgnoreCase(type))
return BigInteger.class;
else if ("uuid".equalsIgnoreCase(type)) {
return UUID.class;
}
else if ("url".equalsIgnoreCase(type)) {
return URL.class;
}
else if (
DATE_ALIAS.equalsIgnoreCase(type) ||
TIME_ALIAS.equalsIgnoreCase(type) ||
DATETIME_ALIAS.equalsIgnoreCase(type))
return Date.class;
try {
return classLoader.loadClass(type);
}
catch (ClassNotFoundException e) {
return null;
}
}
/**
* Returns true if the type alias is not used to register a
* type handler for its associated class.
* @param alias the type alias to check
* @return true if the type alias is only an alias
*/
public static boolean isAliasOnly(String alias) {
return DATE_ALIAS.equalsIgnoreCase(alias) || TIME_ALIAS.equalsIgnoreCase(alias);
}
/**
* Returns the collection Class object for a collection class name or type alias.
* A type alias is not case sensitive. The following collection type aliases are supported:
*
* Alias Class or Primitive
* array Java Array
* list java.util.ArrayList
* set java.util.HashSet
*
*
* @param type the fully qualified class name or type alias of the collection
* @return the collection class, or {@link #ARRAY_TYPE} for array,
* or null if the type name is invalid
*/
@SuppressWarnings("unchecked")
public static Class> toCollectionType(String type) {
if ("array".equalsIgnoreCase(type))
return ARRAY_TYPE;
else if ("collection".equalsIgnoreCase(type))
return (Class>)(Class) Collection.class;
else if ("list".equalsIgnoreCase(type))
return (Class>)(Class) List.class;
else if ("set".equalsIgnoreCase(type))
return (Class>)(Class) Set.class;
try {
Class clazz = Class.forName(type);
if (!Collection.class.isAssignableFrom(clazz)) {
return null;
}
return (Class>) clazz;
}
catch (ClassNotFoundException ex) {
return null;
}
}
public static Class toAggregationType(String type) {
if ("array".equalsIgnoreCase(type))
return ARRAY_TYPE;
else if ("collection".equalsIgnoreCase(type))
return Collection.class;
else if ("list".equalsIgnoreCase(type))
return List.class;
else if ("set".equalsIgnoreCase(type))
return Set.class;
else if ("map".equalsIgnoreCase(type))
return Map.class;
try {
Class clazz = Class.forName(type);
if (Collection.class.isAssignableFrom(clazz))
return clazz;
else if (Map.class.isAssignableFrom(clazz))
return clazz;
else
return null;
}
catch (ClassNotFoundException ex) {
return null;
}
}
private static interface ArrayCollection extends Collection