xiejun
2024-11-01 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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;
    }
 
}