Class EnumConverter<E extends Enum>

java.lang.Object
org.nuiton.converter.EnumConverter<E>
All Implemented Interfaces:
org.apache.commons.beanutils.Converter, NuitonConverter<E>

public class EnumConverter<E extends Enum> extends Object implements NuitonConverter<E>
To convert Enum type-safe (exact type is enumType from and to String.

It is possible also to convert the enum object from his ordinal using method convertFromOrdinal(Class, Object).

you can also define a default value while trying to convert null value.

To register a new such converter use methods ConverterUtil.registerEnumConverter(Class), or ConverterUtil.registerEnumConverter(Class, Object) .

Since:
1.0
Author:
Tony Chemit - chemit@codelutin.com
See Also:
  • Field Details

    • defaultValue

      protected Object defaultValue
      Default value to use if try to convert null value and flag useDefault is on.
    • useDefault

      protected boolean useDefault
      flag to use or not default value.
    • enumType

      protected Class<E extends Enum> enumType
      Enum type-safe.
  • Constructor Details

    • EnumConverter

      public EnumConverter(Class<E> enumType, Object defaultValue)
    • EnumConverter

      public EnumConverter(Class<E> enumType)
  • Method Details

    • convert

      public <T> T convert(Class<T> aClass, Object value)
      Specified by:
      convert in interface org.apache.commons.beanutils.Converter
    • getType

      public Class<E> getType()
      Specified by:
      getType in interface NuitonConverter<E extends Enum>
      Returns:
      the main type of the converter.
    • isEnabled

      protected static boolean isEnabled(Class<?> aClass, Class<?> enumType)
    • convertFromOrdinal

      protected Object convertFromOrdinal(Class<?> aClass, Object value)
    • valueOf

      protected Object valueOf(Class<?> aClass, Object value)
    • allOf

      protected EnumSet<?> allOf(Class<?> aClass)