package org.seasar.jsf.util;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

public class LongValidatorUtil {

    public static final String TYPE_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.TYPE";

    private LongValidatorUtil() {
    }
    
    public static long parseLongValue(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {

        if (value instanceof Number) {
            return ((Number) value).longValue();
        } else {
            try {
                return Long.parseLong(value.toString());
            } catch (NumberFormatException e) {
                Object[] args = { UIComponentUtil.getLabel(component) };
                throw new ValidatorException(MessageUtil.getErrorMessage(
                        TYPE_MESSAGE_ID, args));
            }
        }
    }
}