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_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_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"); 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); 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 major;
private final int minor; private final int minor;
private final int prerelease;
private final int patch; private final int patch;
private final double revision; 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"); 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 (patch.isEmpty()) patch = "0";
if (revision.isEmpty()) revision = "0";
if (prerelease.isEmpty()) prerelease = "-1";
return new BukkitVersion(Integer.parseInt(major), return new BukkitVersion(Integer.parseInt(major),
Integer.parseInt(minor), Integer.parseInt(minor),
Integer.parseInt(patch), 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.major = major;
this.minor = minor; this.minor = minor;
this.patch = patch; this.patch = patch;
this.revision = revision; this.revision = revision;
this.prerelease = prerelease;
} }
public boolean isHigherThan(BukkitVersion o) { public boolean isHigherThan(BukkitVersion o) {
@ -105,6 +112,10 @@ public class VersionUtil {
return revision; return revision;
} }
public int getPrerelease() {
return prerelease;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) { if (this == o) {
@ -117,17 +128,25 @@ public class VersionUtil {
return major == that.major && return major == that.major &&
minor == that.minor && minor == that.minor &&
patch == that.patch && patch == that.patch &&
revision == that.revision; revision == that.revision &&
prerelease == that.prerelease;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(major, minor, patch, revision); return Objects.hashCode(major, minor, patch, revision, prerelease);
} }
@Override @Override
public String toString() { 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 @Override
@ -147,7 +166,13 @@ public class VersionUtil {
} else if (patch > o.patch) { } else if (patch > o.patch) {
return 1; return 1;
} else { // equal patch } else { // equal patch
return Double.compare(revision, o.revision); 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.getMinor(), 13);
assertEquals(v.getPatch(), 2); assertEquals(v.getPatch(), 2);
assertEquals(v.getRevision(), 0.1); 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.getMajor(), 1);
assertEquals(v.getMinor(), 9); assertEquals(v.getMinor(), 9);
assertEquals(v.getPatch(), 0); assertEquals(v.getPatch(), 0);
assertEquals(v.getRevision(), 1.4); 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);
} }
} }