@@ -18,17 +18,14 @@
|
||||
package org.jackhuang.hmcl.util.versioning;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.Iterator;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Copied from org.apache.maven.artifact.versioning.ComparableVersion
|
||||
* Apache License 2.0
|
||||
*
|
||||
* <p>
|
||||
* 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>
|
||||
*/
|
||||
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 final String value;
|
||||
private final boolean pre;
|
||||
|
||||
StringItem(String 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() {
|
||||
@@ -235,8 +240,8 @@ public final class VersionNumber implements Comparable<VersionNumber> {
|
||||
|
||||
public int compareTo(Item item) {
|
||||
if (item == null) {
|
||||
// 1-string > 1
|
||||
return 1;
|
||||
// 1-beta < 1 < 1-string
|
||||
return pre ? -1 : 1;
|
||||
}
|
||||
switch (item.getType()) {
|
||||
case LONG_ITEM:
|
||||
|
||||
@@ -90,12 +90,17 @@ public class VersionNumberTest {
|
||||
assertLessThan("1.99999999999999999999", "1.199999999999999999999");
|
||||
assertLessThan("1.99999999999999999999", "2");
|
||||
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
|
||||
public void testSorting() {
|
||||
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.10.0",
|
||||
"1.6.4",
|
||||
@@ -120,6 +125,9 @@ public class VersionNumberTest {
|
||||
"1.8.9",
|
||||
"1.8.9-forge1902",
|
||||
"1.9",
|
||||
"1.10-alpha.2",
|
||||
"1.10-beta.1",
|
||||
"1.10-beta.2",
|
||||
"1.10",
|
||||
"1.10.2",
|
||||
"1.10.2-AOE",
|
||||
@@ -140,7 +148,8 @@ public class VersionNumberTest {
|
||||
"1.99999999999999999999",
|
||||
"2",
|
||||
"2.0",
|
||||
"2.1"));
|
||||
"2.1"
|
||||
);
|
||||
|
||||
List<String> output = new ArrayList<>(input);
|
||||
output.sort(comparator);
|
||||
|
||||
Reference in New Issue
Block a user