xiejun
2024-11-01 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a
Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/Version.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,202 @@
/*
 *      Copyright (c) 2018-2028, DreamLu All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: DreamLu å¢æ˜¥æ¢¦ (596392912@qq.com)
 */
package org.springblade.core.tool.utils;
import org.springframework.lang.Nullable;
/**
 * ç‰ˆæœ¬å·æ¯”较工具
 * <p>
 * æ€è·¯æ¥æºäºŽï¼š https://github.com/hotoo/versioning/blob/master/versioning.js
 * <p>
 * example
 * * ##完整模式
 * Version.of("v0.1.1").eq("v0.1.2"); // false
 * <p>
 * ##不完整模式
 * Version.of("v0.1").incomplete().eq("v0.1.2");   // true
 *
 * @author L.cm
 * email: 596392912@qq.com
 * site:http://www.dreamlu.net
 * date 2015å¹´7月9日下午10:48:39
 */
public class Version {
   private static final String DELIMITER = "\\.";
   /**
    * ç‰ˆæœ¬å·
    */
   @Nullable
   private String version;
   /**
    * æ˜¯å¦å®Œæ•´æ¨¡å¼ï¼Œé»˜è®¤ä½¿ç”¨å®Œæ•´æ¨¡å¼
    */
   private boolean complete = true;
   /**
    * ç§æœ‰å®žä¾‹åŒ–构造方法
    */
   private Version() {
   }
   private Version(@Nullable String version) {
      this.version = version;
   }
   /**
    * ä¸å®Œæ•´æ¨¡å¼
    *
    * @return {Version}
    */
   public Version incomplete() {
      this.complete = false;
      return this;
   }
   /**
    * æž„造器
    *
    * @param version ç‰ˆæœ¬
    * @return {Version}
    */
   public static Version of(@Nullable String version) {
      return new Version(version);
   }
   /**
    * æ¯”较版本号是否相同
    * <p>
    * example:
    * * Version.of("v0.3").eq("v0.4")
    *
    * @param version å­—符串版本号
    * @return {boolean}
    */
   public boolean eq(@Nullable String version) {
      return compare(version) == 0;
   }
   /**
    * ä¸ç›¸åŒ
    * <p>
    * example:
    * * Version.of("v0.3").ne("v0.4")
    *
    * @param version å­—符串版本号
    * @return {boolean}
    */
   public boolean ne(@Nullable String version) {
      return compare(version) != 0;
   }
   /**
    * å¤§äºŽ
    *
    * @param version ç‰ˆæœ¬å·
    * @return æ˜¯å¦å¤§äºŽ
    */
   public boolean gt(@Nullable String version) {
      return compare(version) > 0;
   }
   /**
    * å¤§äºŽå’Œç­‰äºŽ
    *
    * @param version ç‰ˆæœ¬å·
    * @return æ˜¯å¦å¤§äºŽå’Œç­‰äºŽ
    */
   public boolean gte(@Nullable String version) {
      return compare(version) >= 0;
   }
   /**
    * å°äºŽ
    *
    * @param version ç‰ˆæœ¬å·
    * @return æ˜¯å¦å°äºŽ
    */
   public boolean lt(@Nullable String version) {
      return compare(version) < 0;
   }
   /**
    * å°äºŽå’Œç­‰äºŽ
    *
    * @param version ç‰ˆæœ¬å·
    * @return æ˜¯å¦å°äºŽå’Œç­‰äºŽ
    */
   public boolean lte(@Nullable String version) {
      return compare(version) <= 0;
   }
   /**
    * å’Œå¦å¤–一个版本号比较
    *
    * @param version ç‰ˆæœ¬å·
    * @return {int}
    */
   private int compare(@Nullable String version) {
      return Version.compare(this.version, version, complete);
   }
   /**
    * æ¯”较2个版本号
    *
    * @param v1       v1
    * @param v2       v2
    * @param complete æ˜¯å¦å®Œæ•´çš„æ¯”较两个版本
    * @return (v1 < v2) ? -1 : ((v1 == v2) ? 0 : 1)
    */
   private static int compare(@Nullable String v1, @Nullable String v2, boolean complete) {
      // v1 null视为最小版本,排在前
      if (v1 == v2) {
         return 0;
      } else if (v1 == null) {
         return -1;
      } else if (v2 == null) {
         return 1;
      }
      // åŽ»é™¤ç©ºæ ¼
      v1 = v1.trim();
      v2 = v2.trim();
      if (v1.equals(v2)) {
         return 0;
      }
      String[] v1s = v1.split(DELIMITER);
      String[] v2s = v2.split(DELIMITER);
      int v1sLen = v1s.length;
      int v2sLen = v2s.length;
      int len = complete
         ? Math.max(v1sLen, v2sLen)
         : Math.min(v1sLen, v2sLen);
      for (int i = 0; i < len; i++) {
         String c1 = len > v1sLen || null == v1s[i] ? "" : v1s[i];
         String c2 = len > v2sLen || null == v2s[i] ? "" : v2s[i];
         int result = c1.compareTo(c2);
         if (result != 0) {
            return result;
         }
      }
      return 0;
   }
}