app版本号比对

package com.zyy.user.api.helper;

import com.zyy.framework.model.dto.result.ResultEnum;
import com.zyy.framework.model.exception.BusinessException;
import lombok.Builder;
import lombok.Data;

import javax.validation.constraints.NotNull;

/**
 * APP版本比较工具
 */
public class AppVersionHelper {
    /**
     * 判断当前版本是否与目标版本相等
     *
     * @param current 当前版本
     * @param target  目标版本
     * @return 返回比较情况
     */
    public static boolean isEqual(String current, String target) {
        return current.equals(target);
    }

    /**
     * 判断当前版本是否大于目标版本
     *
     * @param current 当前版本
     * @param target  目标版本
     * @return 返回比较情况
     */
    public static boolean isGreaterThan(String current, String target) {
        Version currentVersion = buildVersion(current);
        Version targetVersion = buildVersion(target);

        return currentVersion.compareTo(targetVersion) > 0;
    }

    /**
     * 判断当前版本是否小于目标版本
     *
     * @param current 当前版本
     * @param target  目标版本
     * @return 返回比较情况
     */
    public static boolean isLessThan(String current, String target) {
        Version currentVersion = buildVersion(current);
        Version targetVersion = buildVersion(target);
        return currentVersion.compareTo(targetVersion) < 0;
    }


    /**
     * 判断当前版本是否大于等于目标版本
     *
     * @param current 当前版本
     * @param target  目标版本
     * @return 返回比较情况
     */
    public static boolean isGreaterThanOrEqual(String current, String target) {
        if (isEqual(current, target)) {
            return true;
        }

        Version currentVersion = buildVersion(current);
        Version targetVersion = buildVersion(target);

        return currentVersion.compareTo(targetVersion) > 0;
    }

    /**
     * 判断当前版本是否小于等于目标版本
     *
     * @param current 当前版本
     * @param target  目标版本
     * @return 返回比较情况
     */
    public static boolean isLessThanOrEqual(String current, String target) {
        if (isEqual(current, target)) {
            return true;
        }

        Version currentVersion = buildVersion(current);
        Version targetVersion = buildVersion(target);
        return currentVersion.compareTo(targetVersion) < 0;
    }


    public static Version buildVersion(String versionStr) {
        String[] split = versionStr.split("\\.");
        if (split.length != 3) {
            throw new BusinessException(ResultEnum.FAILURE.getCode(), "版本字符串格式错误");
        }

        return Version.builder()
                .primaryVersion(Integer.parseInt(split[0]))
                .minorVersion(Integer.parseInt(split[1]))
                .repairedVersion(Integer.parseInt(split[2]))
                .build();
    }

    @Data
    @Builder
    static class Version implements Comparable {
        /**
         * 主 版本号
         */
        private int primaryVersion;

        /**
         * 次 版本号
         */
        private int minorVersion;

        /**
         * 修复 版本号
         */
        private int repairedVersion;

        @Override
        public int compareTo(@NotNull Version version) {
            if (primaryVersion > version.primaryVersion) {
                return 1;
            } else if (primaryVersion == version.primaryVersion) {
                if (minorVersion > version.minorVersion) {
                    return 1;
                } else if (minorVersion == version.minorVersion) {
                    return Integer.compare(repairedVersion, version.repairedVersion);
                } else {
                    return -1;
                }
            } else {
                return -1;
            }
        }
    }
}

测试:

public static void main(String[] args) {
        boolean greaterThan = isGreaterThan("1.1.1", "1.1.2");
        boolean lessThanOrEqual = isLessThanOrEqual("1.1.1", "1.1.2");
        System.out.println("greaterThan :"+greaterThan+",lessThanOrEqual :"+lessThanOrEqual);
    }

测试结果:

greaterThan :false,lessThanOrEqual :true

你可能感兴趣的:(java)