Class ConverterUtil
- java.lang.Object
-
- org.nuiton.converter.ConverterUtil
-
public class ConverterUtil extends Object
Useful class to get / register converter fromConvertUtilsor trying to get it from this package.- Since:
- 1.0
- Author:
- Tony Chemit - chemit@codelutin.com
-
-
Field Summary
Fields Modifier and Type Field Description protected static StringCONVERTER_PACKAGEDefault package where to find the converter.
-
Constructor Summary
Constructors Modifier Constructor Description protectedConverterUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]convert(char[] chars)Converts a array of char to an array of bytes.static <T> Tconvert(Class<T> type, Object toConvert)Try to convert a value.static voidderegister()To remove any registered converter.static <T> org.apache.commons.beanutils.ConvertergetConverter(Class<T> type)Try to find amatching converter for the giventype.static voidinitConverters()To init default converters (using the service loader mecanism on contractNuitonConverter).static voidregisterConverter(Class<?> type)protected static voidregisterConverter0(Class<?> type)static <E extends Enum>
voidregisterEnumConverter(Class<E> type)Register a new converter for a given enum type, not using any default value.static <E extends Enum>
voidregisterEnumConverter(Class<E> type, Object defaultValue)Register a new converter for a given enum type, using the given default value.
-
-
-
Field Detail
-
CONVERTER_PACKAGE
protected static final String CONVERTER_PACKAGE
Default package where to find the converter.- See Also:
- Constant Field Values
-
-
Method Detail
-
getConverter
public static <T> org.apache.commons.beanutils.Converter getConverter(Class<T> type)
Try to find amatching converter for the giventype.If required, init defaults converters (see
initConverters()).If not found and type is an enum, then will register a enum type converter.
If still not found, try to register a converter having the
NuitonConvertercontract namedtypeConverterin the current package (CONVERTER_PACKAGE).For example with type
MyType, will try to find the converterorg.nuiton.converter.MyTypeConverter.- Type Parameters:
T- type to convert- Parameters:
type- type to convert- Returns:
- the found converter, or
nullif not found
-
convert
public static <T> T convert(Class<T> type, Object toConvert)
Try to convert a value.Will first try to find a matching converter, if not found return then
null.Note: will init default converters or matching data type converter if required.
- Type Parameters:
T- type of data to convert- Parameters:
type- type of data to converttoConvert- data to convert- Returns:
- the converted data or
nullif no converter found for the type of data.
-
registerConverter
public static void registerConverter(Class<?> type) throws IllegalAccessException, InstantiationException, ClassNotFoundException
-
registerConverter0
protected static void registerConverter0(Class<?> type) throws IllegalAccessException, InstantiationException, ClassNotFoundException
-
registerEnumConverter
public static <E extends Enum> void registerEnumConverter(Class<E> type, Object defaultValue)
Register a new converter for a given enum type, using the given default value.- Parameters:
type- enum type to convertdefaultValue- default value to use.- See Also:
EnumConverter
-
registerEnumConverter
public static <E extends Enum> void registerEnumConverter(Class<E> type)
Register a new converter for a given enum type, not using any default value.- Parameters:
type- enum type to convert- See Also:
EnumConverter
-
convert
public static byte[] convert(char[] chars)
Converts a array of char to an array of bytes.TODO Find out who uses this ?
- Parameters:
chars- array of characters to converter- Returns:
- the array of bytes
-
deregister
public static void deregister()
To remove any registered converter.At the next call to method
convert(Class, Object)orgetConverter(Class), default converters will be registred again.
-
initConverters
public static void initConverters()
To init default converters (using the service loader mecanism on contractNuitonConverter).If was already init, then will do nothing.
Note: the
getConverter(Class)orconvert(Class, Object)methods always invoke this method if the class was not initialized (see thewasInitinternal flag).
-
-