All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.util.converter.EnumConverter Maven / Gradle / Ivy

There is a newer version: 3.1
Show newest version
/*
 * #%L
 * Nuiton Utils
 * %%
 * Copyright (C) 2004 - 2010 CodeLutin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */

package org.nuiton.util.converter;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.logging.Log;

import java.util.EnumSet;

import static org.apache.commons.logging.LogFactory.getLog;
import static org.nuiton.i18n.I18n.t;

/**
 * classe pour convertir une chaine en un objet Enum type-safe en
 * connaissant le type d'enumération utilisée {@link #enumType}.
 *
 * Il est possible aussi de convertir une Enum à partir de sa valeur ordinal.
 *
 * Pour enregister un nouveau convertissemnt pour un type d'Enum utiliser les
 * méthodes * {@link ConverterUtil#registerEnumConverter(Class)},
 * et {@link ConverterUtil#registerEnumConverter(Class, Object)} .
 *
 * @author Tony Chemit - [email protected]
 * @see Enum
 * @see Enum#ordinal()
 * @deprecated since 3.0 use instead {@link org.nuiton.converter.EnumConverter}
 */
@Deprecated
public class EnumConverter implements Converter {

    /** Logger. */
    private static final Log log = getLog(EnumConverter.class);

    /** valeur par default à utiliser, si pas non trouvée et {@link #useDefault} actif. */
    protected Object defaultValue;

    /** flag pour utiliser la valeur par defaut {@link #defaultValue} si non trouvé. */
    protected boolean useDefault;

    /** le type de l'énumération à convertir */
    protected Class enumType;

    @Override
    public Object convert(Class aClass, Object value) {
        if (value == null) {
            if (useDefault) {
                return defaultValue;
            }
            throw new ConversionException(
                    t("nuitonutil.error.convertor.noValue", this));
        }
        if (isEnabled(aClass, enumType)) {
            Object result;
            if (isEnabled(value.getClass(), enumType)) {
                result = value;
                return result;
            }
            if (value instanceof String) {
                try {
                    result = valueOf(aClass, value);
                } catch (IllegalArgumentException e) {
                    // try an ordinal conversion
                    result = convertFromOrdinal(aClass, value);
                }
                return result;
            }
            if (value instanceof Integer) {
                // try a ordinal conversion
                result = convertFromOrdinal(aClass, value);
                return result;
            }
        }
        throw new ConversionException(
                t("nuitonutil.error.no.convertor", aClass.getName(), value));
    }

    public EnumConverter(Class enumType, Object defaultValue) {
        this.enumType = enumType;
        this.defaultValue = defaultValue;
        useDefault = defaultValue != null;
        if (log.isDebugEnabled()) {
            log.debug(toString() + '<' + enumType + '>');
        }
    }

    public EnumConverter(Class enumType) {
        this(enumType, null);
    }

    protected static boolean isEnabled(Class aClass, Class enumType) {
        return aClass != null && aClass.isEnum() && aClass == enumType;
    }

    protected Object convertFromOrdinal(Class aClass, Object value) {
        Object result = null;
        try {
            int ordinal = Integer.valueOf(value + "");
            EnumSet vals = allOf(aClass);
            if (ordinal > -1 && ordinal < vals.size()) {
                for (Enum val : vals) {
                    if (val.ordinal() == ordinal) {
                        result = val;
                        break;
                    }
                }
            }
        } catch (NumberFormatException e1) {
            // quiet conversion
            result = null;
        }
        return result;
    }

    protected Object valueOf(Class aClass, Object value) {
        Object result;
        result = Enum.valueOf((Class) aClass, (String) value);
        return result;
    }

    protected EnumSet allOf(Class aClass) {
        EnumSet vals;
        vals = EnumSet.allOf((Class) aClass);
        return vals;
    }

    public Class getType() {
        return enumType;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy