
org.nuiton.util.converter.VersionConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nuiton-utils Show documentation
Show all versions of nuiton-utils Show documentation
Library of usefull class to be used in any project.
/*
* #%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.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.converter.NuitonConverter;
import org.nuiton.util.Version;
import org.nuiton.util.VersionUtil;
import static org.nuiton.i18n.I18n.t;
/**
* classe pour convertir une chaine en un objet Version.
*
* @author Tony Chemit - [email protected]
* @see Version
* @since 1.3 (replace {@code org.nuiton.util.VersionConverter}).
* @deprecated since 3.0, prefer use the {@link org.nuiton.util.version.VersionConverter}.
*/
@Deprecated
public class VersionConverter implements NuitonConverter {
/** Logger. */
private static final Log log = LogFactory.getLog(VersionConverter.class);
public VersionConverter() {
if (log.isDebugEnabled()) {
log.debug("init version converter " + this);
}
}
@Override
public T convert(Class aClass, Object value) {
if (value == null) {
throw new ConversionException(
t("nuitonutil.error.convertor.noValue", this));
}
if (isEnabled(aClass)) {
Object result;
if (isEnabled(value.getClass())) {
result = value;
return aClass.cast(result);
}
if (value instanceof String) {
try {
result = VersionUtil.valueOf((String) value);
return aClass.cast(result);
} catch (IllegalArgumentException e) {
throw new ConversionException(
t("nuitonutil.error.version.convertor", value, this, e.getMessage()), e);
}
}
}
throw new ConversionException(
t("nuitonutil.error.no.convertor", aClass.getName(), value));
}
@Override
public Class getType() {
return Version.class;
}
protected boolean isEnabled(Class> aClass) {
return Version.class.equals(aClass);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy