Add prerelease support to VersionUtil

This commit is contained in:
md678685 2019-04-24 13:37:43 +01:00
parent d680ed7363
commit cfba203981
2 changed files with 48 additions and 9 deletions

View File

@ -20,6 +20,8 @@ public class VersionUtil {
public static final BukkitVersion v1_12_2_R01 = BukkitVersion.fromString("1.12.2-R0.1-SNAPSHOT");
public static final BukkitVersion v1_13_0_R01 = BukkitVersion.fromString("1.13.0-R0.1-SNAPSHOT");
public static final BukkitVersion v1_13_2_R01 = BukkitVersion.fromString("1.13.2-R0.1-SNAPSHOT");
// TODO: update to 1.14 release
public static final BukkitVersion v1_14_PRE5 = BukkitVersion.fromString("1.14-pre5-SNAPSHOT");
private static final Set<BukkitVersion> supportedVersions = ImmutableSet.of(v1_8_8_R01, v1_9_4_R01, v1_10_2_R01, v1_11_2_R01, v1_12_2_R01, v1_13_2_R01);
@ -41,6 +43,7 @@ public class VersionUtil {
private final int major;
private final int minor;
private final int prerelease;
private final int patch;
private final double revision;
@ -55,22 +58,26 @@ public class VersionUtil {
Preconditions.checkArgument(matcher.matches(), string + " is not in valid version format. e.g. 1.8.8-R0.1");
}
return from(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4));
return from(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4), matcher.group(5));
}
private static BukkitVersion from(String major, String minor, String patch, String revision) {
private static BukkitVersion from(String major, String minor, String patch, String revision, String prerelease) {
if (patch.isEmpty()) patch = "0";
if (revision.isEmpty()) revision = "0";
if (prerelease.isEmpty()) prerelease = "-1";
return new BukkitVersion(Integer.parseInt(major),
Integer.parseInt(minor),
Integer.parseInt(patch),
Double.parseDouble(revision));
Double.parseDouble(revision),
Integer.parseInt(prerelease));
}
private BukkitVersion(int major, int minor, int patch, double revision) {
private BukkitVersion(int major, int minor, int patch, double revision, int prerelease) {
this.major = major;
this.minor = minor;
this.patch = patch;
this.revision = revision;
this.prerelease = prerelease;
}
public boolean isHigherThan(BukkitVersion o) {
@ -105,6 +112,10 @@ public class VersionUtil {
return revision;
}
public int getPrerelease() {
return prerelease;
}
@Override
public boolean equals(Object o) {
if (this == o) {
@ -117,17 +128,25 @@ public class VersionUtil {
return major == that.major &&
minor == that.minor &&
patch == that.patch &&
revision == that.revision;
revision == that.revision &&
prerelease == that.prerelease;
}
@Override
public int hashCode() {
return Objects.hashCode(major, minor, patch, revision);
return Objects.hashCode(major, minor, patch, revision, prerelease);
}
@Override
public String toString() {
return major + "." + minor + "." + patch + "-R" + revision;
StringBuilder sb = new StringBuilder(major + "." + minor);
if (patch != 0) {
sb.append(".").append(patch);
}
if (prerelease != -1) {
sb.append("-pre").append(prerelease);
}
return sb.append("-R").append(revision).toString();
}
@Override
@ -147,10 +166,16 @@ public class VersionUtil {
} else if (patch > o.patch) {
return 1;
} else { // equal patch
if (prerelease < o.prerelease) {
return -1;
} else if (prerelease > o.prerelease) {
return 1;
} else { // equal prerelease
return Double.compare(revision, o.revision);
}
}
}
}
}
}
}

View File

@ -197,10 +197,24 @@ public class UtilTest extends TestCase {
assertEquals(v.getMinor(), 13);
assertEquals(v.getPatch(), 2);
assertEquals(v.getRevision(), 0.1);
v = VersionUtil.BukkitVersion.fromString("1.9-R1.4");
assertEquals(v.getPrerelease(), -1);
v = VersionUtil.BukkitVersion.fromString("1.9-R1.4"); // not real
assertEquals(v.getMajor(), 1);
assertEquals(v.getMinor(), 9);
assertEquals(v.getPatch(), 0);
assertEquals(v.getRevision(), 1.4);
assertEquals(v.getPrerelease(), -1);
v = VersionUtil.BukkitVersion.fromString("1.14-pre5");
assertEquals(v.getMajor(), 1);
assertEquals(v.getMinor(), 14);
assertEquals(v.getPatch(), 0);
assertEquals(v.getRevision(), 0);
assertEquals(v.getPrerelease(), 5);
v = VersionUtil.BukkitVersion.fromString("1.13.2-pre1-R0.1"); // not real
assertEquals(v.getMajor(), 1);
assertEquals(v.getMinor(), 13);
assertEquals(v.getPatch(), 2);
assertEquals(v.getRevision(), 0.1);
assertEquals(v.getPrerelease(), 1);
}
}