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

com.greenpepper.converter.AbstractTypeConverter Maven / Gradle / Ivy

There is a newer version: 4.2.4
Show newest version
/*
 * Copyright (c) 2006 Pyxis Technologies inc.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA,
 * or see the FSF site: http://www.fsf.org.
 */
package com.greenpepper.converter;

import com.greenpepper.util.StringUtil;

/**
 * Abstract base class for TypeConverter implementations that takes
 * care handling null or empty string values. Subclasses are garanteed to
 * receive a non-null and non-empty string in their doConvert()
 * implementations.
 *
 * @version $Revision: $ $Date: $
 * @author oaouattara
 */
public abstract class AbstractTypeConverter
        implements TypeConverter
{
    /**
     * {@inheritDoc}
     *
     * Template implementation of the convert() method. Takes care
     * of handling null and empty string values. Once these basic operations
     * are handled, will delegate to the doConvert() method of
     * subclasses.
     */
    public Object parse(String value, Class type)
    {
        if (StringUtil.isBlank(value))
        {
            return null;
        }

        return doConvert(value.trim(), type);
    }

    /**
     * {@inheritDoc}
     *
     * Basic implementation of the toString() method. Takes care
     * of handling null values otherwise call the doToString of the object.
     */
    public String toString(Object value)
    {
    	if (value == null) return "";
    	return doToString(value);
    }

    /**
     * Subclass must implement this method to do the actual type conversion.
     * Subclass implementations are garanteed to receive a non-null and
     * non-empty string.
     *
     * @param value The string value to convert
     * @return The converted value, or null
     */
    protected abstract Object doConvert(String value);

	/**
	 * Do the conversion of the given value into the given type.
	 *
	 * @param value The string value to convert
	 * @param type a {@link java.lang.Class} object.
	 * @return The converted value, or null
	 */
	protected Object doConvert(String value, Class type) {
		return doConvert(value);
	}

    /**
     * Subclass must implement this method to overide the default call to the toString
     * method of the value.
     * Subclass implementations are garanteed to receive a non-null object.
     *
     * @param value The string value to convert
     * @return The converted value, or null
     */
    protected String doToString(Object value)
    {
        return String.valueOf(value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy