package xyz.wbsite.frame.excel.converter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

    @Override
    public Long convert(String var) {
        try {
            //增加对1.0一类的的兼容性
            if (var.matches("(\\d+)\\.(\\d*)")) {
                Pattern compile = Pattern.compile("(\\d+)\\.(\\d*)");
                Matcher matcher = compile.matcher(var);
                if (matcher.find()) {
                    var = matcher.group(1);
                }
            }

            return Long.parseLong(var);
        } catch (Exception e) {
            return 0L;
        }
    }

    @Override
    public String string(Long var) {
        if (var == null) {
            return "";
        }
        return String.valueOf(var);
    }
}
