jodd.typeconverter.TypeConverterManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jodd-core Show documentation
Show all versions of jodd-core Show documentation
Jodd Core tools and utilities, including type converters, JDateTime, cache etc.
// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.
package jodd.typeconverter;
import java.util.Collection;
/**
* Provides dynamic object conversion to a type.
* Contains a map of registered converters. User may add new converter.
* Static version of {@link TypeConverterManagerBean}.
*/
public class TypeConverterManager {
private static final TypeConverterManagerBean TYPE_CONVERTER_MANAGER_BEAN = new TypeConverterManagerBean();
/**
* Returns default {@link TypeConverterManager}.
*/
public static TypeConverterManagerBean getDefaultTypeConverterManager() {
return TYPE_CONVERTER_MANAGER_BEAN;
}
/**
* Registers default set of converters.
*/
public static void registerDefaults() {
TYPE_CONVERTER_MANAGER_BEAN.registerDefaults();
}
/**
* Registers a converter for specified type.
* User must register converter for all super-classes as well.
*
* @param type class that converter is for
* @param typeConverter converter for provided class
*/
public static void register(Class type, TypeConverter typeConverter) {
TYPE_CONVERTER_MANAGER_BEAN.register(type, typeConverter);
}
public static void unregister(Class type) {
TYPE_CONVERTER_MANAGER_BEAN.unregister(type);
}
/**
* Retrieves converter for provided type. Only registered types are matched,
* therefore subclasses must be also registered.
*
* @return founded converter or null
*/
public static TypeConverter lookup(Class type) {
return TYPE_CONVERTER_MANAGER_BEAN.lookup(type);
}
/**
* Casts an object to destination type using {@link TypeConverterManager type conversion}.
* If destination type is one of common types, consider using {@link jodd.typeconverter.Convert} instead.
*/
public static T convertType(Object value, Class destinationType) {
return TYPE_CONVERTER_MANAGER_BEAN.convertType(value, destinationType);
}
/**
* Special conversion to collections, when component type is known.
*/
public static Collection convertToCollection(Object value, Class> destinationType, Class componentType) {
return TYPE_CONVERTER_MANAGER_BEAN.convertToCollection(value, destinationType, componentType);
}
}