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
 * 
 * $Id: EnumConverter.java 2360 2012-06-11 10:24:36Z tchemit $
 * $HeadURL: http://svn.nuiton.org/svn/nuiton-utils/tags/nuiton-utils-2.6.10/nuiton-utils/src/main/java/org/nuiton/util/converter/EnumConverter.java $
 * %%
 * 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._;

/**
 * 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 tchemit * @see Enum * @see Enum#ordinal() */ public class EnumConverter implements Converter { /** Logger. */ static 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( _("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( _("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 - 2025 Weber Informatics LLC | Privacy Policy