package ${basePackage}.frame.excel.converter;

import ${basePackage}.frame.excel.exception.ValueConverterException;

/**
 * BooleanConverter - Boolean转化器，重写了对象到String，String到对象的转化方式
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
public class BooleanConverter implements Converter<Boolean> {

    @Override
    public Boolean convert(String var) throws ValueConverterException {
        if (null == var || "".equals(var)) {
            return false;
        }

        String lowerCase = var.toLowerCase();

        if (lowerCase.matches("y|n")) {
            return "y".equals(lowerCase);
        } else if (lowerCase.matches("yes|no")) {
            return "yes".equals(lowerCase);
        } else if (lowerCase.matches("true|false")) {
            return "true".equals(lowerCase);
        } else if (lowerCase.matches("是|否")) {
            return "是".equals(lowerCase);
        } else if (lowerCase.matches("有|无")) {
            return "有".equals(lowerCase);
        } else if (lowerCase.matches("是|不是")) {
            return "是".equals(lowerCase);
        } else {
            throw new ValueConverterException("[" + var + "] can not convert to Boolean");
        }
    }

    @Override
    public String string(Boolean var) {
        if (var == null) {
            return "";
        }
        return var ? "是" : "否";
    }
}