package cc.smtweb.framework.core.mvc.controller.binder.param;

import org.apache.commons.lang3.StringUtils;


/**
 * 字符串到Number类型的转换
 *
 * @author kevin
 */
public abstract class AbstractNumberEditor extends AbstractParameterEditor {
    protected boolean automicType;

    public AbstractNumberEditor(int paramEditType, boolean automicType) {
        super(paramEditType);
        this.automicType = automicType;
    }

    @Override
    protected Object parseText(Object value) {
        Object result = null;

        if (value == null) {
            return automicType ? defaultValue(): null;
        }

        if (value instanceof Number) {
            return convert((Number) value);
        }

        String text = value.toString();

        if (StringUtils.isNotBlank(text)) {
            result = getValue(text);
        } else {
            result = automicType ? defaultValue(): null;
        }

        return result;
    }

    protected abstract Object convert(Number value);

    protected abstract Object getValue(String text);

    protected abstract Object defaultValue();
}
