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

import org.apache.commons.lang3.StringUtils;

public class BooleanEditor extends AbstractParameterEditor {
    private final boolean automicType;

    public BooleanEditor(int header, boolean automicType) {
        super(header);
        this.automicType = automicType;
    }

    @Override
    public Object parseText(Object text) {
        if (text == null) {
            return automicType ? Boolean.FALSE: null;
        }

        if (text instanceof Boolean) {
            return text;
        }

        String s = text.toString();
        if (StringUtils.isBlank(s)) {
            return automicType ? Boolean.FALSE: null;
        }

        return Boolean.valueOf(s);
    }
}
