From cfba203981adb307a45093d167050eebb2fff3c9 Mon Sep 17 00:00:00 2001 From: md678685 Date: Wed, 24 Apr 2019 13:37:43 +0100 Subject: [PATCH] Add prerelease support to VersionUtil --- .../essentials/utils/VersionUtil.java | 41 +++++++++++++++---- .../com/earth2me/essentials/UtilTest.java | 16 +++++++- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/utils/VersionUtil.java b/Essentials/src/com/earth2me/essentials/utils/VersionUtil.java index f87fb5800..746c6fd53 100644 --- a/Essentials/src/com/earth2me/essentials/utils/VersionUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/VersionUtil.java @@ -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 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,7 +166,13 @@ public class VersionUtil { } else if (patch > o.patch) { return 1; } 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); + } } } } diff --git a/Essentials/test/com/earth2me/essentials/UtilTest.java b/Essentials/test/com/earth2me/essentials/UtilTest.java index cd905f9ad..6c22e5508 100644 --- a/Essentials/test/com/earth2me/essentials/UtilTest.java +++ b/Essentials/test/com/earth2me/essentials/UtilTest.java @@ -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); } }