package org.sql2o.converters;

/**
 * Default implementation of {@link EnumConverterFactory},
 * used by sql2o to convert a value from the database into an {@link Enum}.
 */
public class DefaultEnumConverterFactory implements EnumConverterFactory {
    public <E extends Enum> Converter<E> newConverter(final Class<E> enumType) {
        return new Converter<E>() {
            @SuppressWarnings("unchecked")
            public E convert(Object val) throws ConverterException {
                if (val == null) {
                    return null;
                }
                try {
                    if (val instanceof String){
                        return (E)Enum.valueOf(enumType, val.toString());
                    } else if (val instanceof Number){
                        return enumType.getEnumConstants()[((Number)val).intValue()];
                    }
                } catch (Throwable t) {
                    throw new ConverterException("Error converting value '" + val.toString() + "' to " + enumType.getName(), t);
                }
                throw new ConverterException("Cannot convert type '" + val.getClass().getName() + "' to an Enum");
            }

            public Object toDatabaseParam(Enum val) {
                return val.toString();
            }
        };
    }
}

package org.sql2o.converters;

/**
 * Default implementation of {@link EnumConverterFactory},
 * used by sql2o to convert a value from the database into an {@link Enum}.
 */
public class DefaultEnumConverterFactory implements EnumConverterFactory {
    public <E extends Enum> Converter<E> newConverter(final Class<E> enumType) {
        return new Converter<E>() {
            @SuppressWarnings("unchecked")
            public E convert(Object val) throws ConverterException {
                if (val == null) {
                    return null;
                }
                try {
                    if (val instanceof String){
                        return (E)Enum.valueOf(enumType, val.toString());
                    } else if (val instanceof Number){
                        return enumType.getEnumConstants()[((Number)val).intValue()];
                    }
                } catch (Throwable t) {
                    throw new ConverterException("Error converting value '" + val.toString() + "' to " + enumType.getName(), t);
                }
                throw new ConverterException("Cannot convert type '" + val.getClass().getName() + "' to an Enum");
            }

            public Object toDatabaseParam(Enum val) {
                return val.name();
            }
        };
    }
}

Use name() instead of toString() for enum value.
Use name() instead of toString() for enum value.

The javadoc for Enum notes that name() should be used when
correctness depends on getting the exact name of the enum
constant which will not vary from release to release. The
javadoc further encourages overriding the toString() method
when appropriate to provide a more user-friendly name. This
commit fixes the DefaultEnumConverterFactory in cases where
the toString() method is overridden to return something other
than the enum constant.
