Class EnumConverter

  • All Implemented Interfaces:
    org.apache.commons.beanutils.Converter

    public final class EnumConverter
    extends Object
    implements org.apache.commons.beanutils.Converter

    A specialized Converter implementation dealing with all types of enumeration classes.

    The implementation of the convert(Class, Object) method expects that the passed in value is a constant as defined for an enumeration class as string. It tries to find the corresponding instance of the enumeration class.

    An instance of this class is registered per default as base class converter at any new InvocationHelper instance. Therefore conversions to parameters or properties of Enum types are supported out of the box.

    Implementation note: This class has no state. So a single instance can be shared between multiple instances. This instance can be obtained using the getInstance() factory method; creating new instances is now allowed.

    Version:
    $Id: EnumConverter.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Method Detail

      • getInstance

        public static EnumConverter getInstance()
        Returns an instance of EnumConverter. Use this factory method to obtain a new instance rather than invoking a constructor.
        Returns:
        the EnumConverter instance
      • convert

        public Object convert​(Class type,
                              Object value)
        Performs the type conversion to an enumeration constant. If this is not possible, a ConversionException is thrown. There are multiple reasons why this could happen, e.g. the value is not an exact string representation of an enumeration constant, or the class might be no Enum class.
        Specified by:
        convert in interface org.apache.commons.beanutils.Converter
        Parameters:
        type - the class to convert to
        value - the object to be converted
        Returns:
        the result of the conversion