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

se.wfh.libs.common.web.converter.IntegerConverter Maven / Gradle / Ivy

package se.wfh.libs.common.web.converter;

import java.text.NumberFormat;
import java.text.ParseException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

@FacesConverter(forClass = Integer.class, value = "wfh.IntegerConverter")
public class IntegerConverter implements Converter {
	private static final NumberFormat INT_FMT = NumberFormat.getIntegerInstance();

	@Override
	public Object getAsObject(final FacesContext context,
			final UIComponent component, final String value)
			throws ConverterException {
		try {
			Number obj = (Number) INT_FMT.parseObject(value);

			return obj.intValue();
		} catch (ParseException ex) {
			throw new ConverterException(ex);
		}
	}

	@Override
	public String getAsString(final FacesContext context,
			final UIComponent component, final Object value) {
		return INT_FMT.format(((Number) value).intValue());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy