All Downloads are FREE. Search and download functionalities are using the official Maven repository.

spring.turbo.util.collection.VersionComparator Maven / Gradle / Ivy

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *    ____             _            _____           _
 *   / ___| _ __  _ __(_)_ __   __ |_   _|   _ _ __| |__   ___
 *   \___ \| '_ \| '__| | '_ \ / _` || || | | | '__| '_ \ / _ \
 *    ___) | |_) | |  | | | | | (_| || || |_| | |  | |_) | (_) |
 *   |____/| .__/|_|  |_|_| |_|\__, ||_| \__,_|_|  |_.__/ \___/
 *         |_|                 |___/   https://github.com/yingzhuo/spring-turbo
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package spring.turbo.util.collection;

import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import spring.turbo.lang.Singleton;
import spring.turbo.util.Asserts;
import spring.turbo.util.StringPool;

import java.util.Comparator;
import java.util.Objects;

/**
 * 版本号比较器
 *
 * @author 应卓
 * @see #getInstance()
 * @since 1.1.4
 */
@Singleton
public class VersionComparator implements Comparator {

    /**
     * 私有构造方法
     */
    private VersionComparator() {
        super();
    }

    public static VersionComparator getInstance() {
        return AsyncAvoid.INSTANCE;
    }

    /**
     * 比较两个版本
* null版本排在最小:即: *
     * compare(null, "v1") < 0
     * compare("v1", "v1") = 0
     * compare(null, null) = 0
     * compare("v1", null) > 0
     * compare("1.0.0", "1.0.2") < 0
     * compare("1.0.2", "1.0.2a") < 0
     * compare("1.13.0", "1.12.1c") > 0
     * compare("V0.0.20170102", "V0.0.20170101") > 0
     * 
* * @param version1 版本1 * @param version2 版本2 */ @Override public int compare(@Nullable String version1, @Nullable String version2) { if (Objects.equals(version1, version2)) { return 0; } if (version1 == null) {// null视为最小版本,排在前 return -1; } if (version2 == null) { return 1; } final String[] v1s = StringUtils.split(version1, StringPool.DOT); final String[] v2s = StringUtils.split(version2, StringPool.DOT); Asserts.notNull(v1s); Asserts.notNull(v2s); int diff = 0; int minLength = Math.min(v1s.length, v2s.length);// 取最小长度值 String v1; String v2; for (int i = 0; i < minLength; i++) { v1 = v1s[i]; v2 = v2s[i]; // 先比较长度 diff = v1.length() - v2.length(); if (0 == diff) { diff = v1.compareTo(v2); } if (diff != 0) { //已有结果,结束 break; } } // 如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大; return (diff != 0) ? diff : v1s.length - v2s.length; } // ----------------------------------------------------------------------------------------------------------------- private static class AsyncAvoid { public static final VersionComparator INSTANCE = new VersionComparator(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy