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

org.fastnate.data.csv.CsvNumberConverter Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package org.fastnate.data.csv;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;

/**
 * Converts a string from a CSV file to a number.
 *
 * @author Tobias Liefke
 */
public class CsvNumberConverter implements CsvPropertyConverter {

	@Override
	public Number convert(final Class targetType, final String value) {
		if (StringUtils.isBlank(value)) {
			if (targetType.isPrimitive()) {
				return convert(targetType, "0");
			}
			return null;
		}
		if (targetType == Number.class) {
			return new Float(value);
		}
		final Class wrapperType = targetType.isPrimitive() ? ClassUtils.primitiveToWrapper(targetType)
				: targetType;
		try {
			return wrapperType.getConstructor(String.class).newInstance(value);
		} catch (final InstantiationException | IllegalAccessException | InvocationTargetException
				| NoSuchMethodException e) {
			throw new IllegalArgumentException(e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy