Class ConverterUtil

java.lang.Object
org.nuiton.converter.ConverterUtil

public class ConverterUtil extends Object
Useful class to get / register converter from ConvertUtils or trying to get it from this package.
Since:
1.0
Author:
Tony Chemit - chemit@codelutin.com
  • Field Details

    • CONVERTER_PACKAGE

      protected static final String CONVERTER_PACKAGE
      Default package where to find the converter.
      See Also:
  • Constructor Details

    • ConverterUtil

      protected ConverterUtil()
  • Method Details

    • getConverter

      public static <T> org.apache.commons.beanutils.Converter getConverter(Class<T> type)
      Try to find amatching converter for the given type.

      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 NuitonConverter contract named typeConverter in the current package (CONVERTER_PACKAGE).

      For example with type MyType, will try to find the converter org.nuiton.converter.MyTypeConverter.

      Type Parameters:
      T - type to convert
      Parameters:
      type - type to convert
      Returns:
      the found converter, or null if 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 convert
      toConvert - data to convert
      Returns:
      the converted data or null if no converter found for the type of data.
    • registerConverter

    • registerConverter0

    • 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 convert
      defaultValue - default value to use.
      See Also:
    • 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:
    • 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) or getConverter(Class), default converters will be registred again.

    • initConverters

      public static void initConverters()
      To init default converters (using the service loader mecanism on contract NuitonConverter).

      If was already init, then will do nothing.

      Note: the getConverter(Class) or convert(Class, Object) methods always invoke this method if the class was not initialized (see the wasInit internal flag).