@@ -18,17 +18,14 @@
|
|||||||
package org.jackhuang.hmcl.util.versioning;
|
package org.jackhuang.hmcl.util.versioning;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.ArrayDeque;
|
import java.util.*;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Deque;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copied from org.apache.maven.artifact.versioning.ComparableVersion
|
* Copied from org.apache.maven.artifact.versioning.ComparableVersion
|
||||||
* Apache License 2.0
|
* Apache License 2.0
|
||||||
*
|
* <p>
|
||||||
* Maybe we can migrate to org.jenkins-ci:version-number:1.7?
|
* Maybe we can migrate to org.jenkins-ci:version-number:1.7?
|
||||||
|
*
|
||||||
* @see <a href="http://maven.apache.org/pom.html#Version_Order_Specification">Specification</a>
|
* @see <a href="http://maven.apache.org/pom.html#Version_Order_Specification">Specification</a>
|
||||||
*/
|
*/
|
||||||
public final class VersionNumber implements Comparable<VersionNumber> {
|
public final class VersionNumber implements Comparable<VersionNumber> {
|
||||||
@@ -220,9 +217,17 @@ public final class VersionNumber implements Comparable<VersionNumber> {
|
|||||||
*/
|
*/
|
||||||
private static final class StringItem implements Item {
|
private static final class StringItem implements Item {
|
||||||
private final String value;
|
private final String value;
|
||||||
|
private final boolean pre;
|
||||||
|
|
||||||
StringItem(String value) {
|
StringItem(String value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
|
||||||
|
String lower = value.trim().toLowerCase(Locale.ROOT);
|
||||||
|
this.pre = lower.startsWith("alpha")
|
||||||
|
|| lower.startsWith("beta")
|
||||||
|
|| lower.startsWith("pre")
|
||||||
|
|| lower.startsWith("rc")
|
||||||
|
|| lower.startsWith("experimental");
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getType() {
|
public int getType() {
|
||||||
@@ -235,8 +240,8 @@ public final class VersionNumber implements Comparable<VersionNumber> {
|
|||||||
|
|
||||||
public int compareTo(Item item) {
|
public int compareTo(Item item) {
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
// 1-string > 1
|
// 1-beta < 1 < 1-string
|
||||||
return 1;
|
return pre ? -1 : 1;
|
||||||
}
|
}
|
||||||
switch (item.getType()) {
|
switch (item.getType()) {
|
||||||
case LONG_ITEM:
|
case LONG_ITEM:
|
||||||
|
|||||||
@@ -90,12 +90,17 @@ public class VersionNumberTest {
|
|||||||
assertLessThan("1.99999999999999999999", "1.199999999999999999999");
|
assertLessThan("1.99999999999999999999", "1.199999999999999999999");
|
||||||
assertLessThan("1.99999999999999999999", "2");
|
assertLessThan("1.99999999999999999999", "2");
|
||||||
assertLessThan("1.99999999999999999999", "2.0");
|
assertLessThan("1.99999999999999999999", "2.0");
|
||||||
|
assertLessThan("1.0", "1.0-zzz");
|
||||||
|
assertLessThan("1.0-beta.1", "1.0");
|
||||||
|
assertLessThan("1.0-alpha.1", "1.0-beta.1");
|
||||||
|
assertLessThan("3.6.15", "3.6.15.289");
|
||||||
|
assertLessThan("3.6.15.289", "3.6.16");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSorting() {
|
public void testSorting() {
|
||||||
final Comparator<String> comparator = ((Comparator<String>) VersionNumber::compare).thenComparing(String::compareTo);
|
final Comparator<String> comparator = ((Comparator<String>) VersionNumber::compare).thenComparing(String::compareTo);
|
||||||
final List<String> input = Collections.unmodifiableList(Arrays.asList(
|
final List<String> input = List.of(
|
||||||
"0",
|
"0",
|
||||||
"0.10.0",
|
"0.10.0",
|
||||||
"1.6.4",
|
"1.6.4",
|
||||||
@@ -120,6 +125,9 @@ public class VersionNumberTest {
|
|||||||
"1.8.9",
|
"1.8.9",
|
||||||
"1.8.9-forge1902",
|
"1.8.9-forge1902",
|
||||||
"1.9",
|
"1.9",
|
||||||
|
"1.10-alpha.2",
|
||||||
|
"1.10-beta.1",
|
||||||
|
"1.10-beta.2",
|
||||||
"1.10",
|
"1.10",
|
||||||
"1.10.2",
|
"1.10.2",
|
||||||
"1.10.2-AOE",
|
"1.10.2-AOE",
|
||||||
@@ -140,7 +148,8 @@ public class VersionNumberTest {
|
|||||||
"1.99999999999999999999",
|
"1.99999999999999999999",
|
||||||
"2",
|
"2",
|
||||||
"2.0",
|
"2.0",
|
||||||
"2.1"));
|
"2.1"
|
||||||
|
);
|
||||||
|
|
||||||
List<String> output = new ArrayList<>(input);
|
List<String> output = new ArrayList<>(input);
|
||||||
output.sort(comparator);
|
output.sort(comparator);
|
||||||
|
|||||||
Reference in New Issue
Block a user