java 版本号递增

2020-12-02  本文已影响0人  JuneGe2017
@Slf4j
public class VsersionUtil {

    public static String upgradeVersion(String version)
    {
        String upgradeVsersion;
        if (StringUtils.isEmpty(version))
        {
            version = "1.0.0.0";
        }
        //将版本号拆解成整数数组
        List<String> strArr = Splitter.on(".").splitToList(version);
        Integer[] ints = new Integer[strArr.size()];
        for(int i=0 ;i<strArr.size(); i++) {
            ints[i] = Integer.parseInt(strArr.get(i));
        }
        //递归调用
        upgradeVersion(ints, ints.length - 1);

        //数组转字符串
        upgradeVsersion = Joiner.on(".").join(ints);

        return upgradeVsersion;
    }
    private static void upgradeVersion(Integer[] ints, int index)
    {
        if (index == 0) {
            ints[0] = ints[0] + 1;
        } else {
            int value = ints[index] + 1;
            if (value < 10) {
                ints[index] = value;
            } else {
                ints[index] = 0;
                upgradeVersion(ints, index - 1);
            }
        }
    }

    public static void main(String[] args) {
        log.info("version:1.0.0.0|"+upgradeVersion("1.0.0.0"));
        log.info("version:1.0.0.9|"+upgradeVersion("1.0.0.9"));
        log.info("version:1.0.1.9|"+upgradeVersion("1.0.1.9"));
        log.info("version:1.0.9.9|"+upgradeVersion("1.0.9.9"));
        log.info("version:1.9.9.9|"+upgradeVersion("1.9.9.9"));
    }
}
上一篇下一篇

猜你喜欢

热点阅读