From 1742869b93ce5f9c8e81e10c2b84f6a7e0c7ef56 Mon Sep 17 00:00:00 2001 From: CJ Burkey Date: Thu, 23 May 2024 13:41:45 -0400 Subject: [PATCH] Turn SemVer class into a record --- .../com/cjburkey/claimchunk/ClaimChunk.java | 2 +- .../cjburkey/claimchunk/update/SemVer.java | 35 ++++++------------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java b/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java index be35503..1bd39e0 100644 --- a/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java +++ b/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java @@ -146,7 +146,7 @@ public void onLoad() { // Get the current plugin version version = SemVer.fromString(getDescription().getVersion()); - if (version.marker != null) { + if (version.marker() != null) { Utils.debug("Plugin version is nonstandard release %s", version); } diff --git a/src/main/java/com/cjburkey/claimchunk/update/SemVer.java b/src/main/java/com/cjburkey/claimchunk/update/SemVer.java index ac5196d..050427b 100644 --- a/src/main/java/com/cjburkey/claimchunk/update/SemVer.java +++ b/src/main/java/com/cjburkey/claimchunk/update/SemVer.java @@ -1,6 +1,6 @@ package com.cjburkey.claimchunk.update; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; import java.util.Objects; @@ -8,28 +8,10 @@ * A nice little semantic versioning class. Feel free to use this in your own projects if you want * :) */ -public class SemVer implements Comparable { +public record SemVer(int major, int minor, int patch, String marker) implements Comparable { - @SuppressWarnings("WeakerAccess") - public final int major; - - @SuppressWarnings("WeakerAccess") - public final int minor; - - @SuppressWarnings("WeakerAccess") - public final int patch; - - @SuppressWarnings("WeakerAccess") - public final String marker; - - private SemVer(int major, int minor, int patch, @Nullable String marker) { - this.major = major; - this.minor = minor; - this.patch = patch; - this.marker = ((marker == null) ? null : marker.toUpperCase()); - } - - public static SemVer fromString(final String version) { + public static @NotNull SemVer fromString(@NotNull String version) + throws IllegalArgumentException { try { final String[] split = version.trim().split("\\."); if (split.length != 3) { @@ -43,10 +25,11 @@ public static SemVer fromString(final String version) { final int major = Integer.parseInt(split[0].trim()); final int minor = Integer.parseInt(split[1].trim()); final int patch = Integer.parseInt(patchMarker[0].trim()); - final String marker = ((patchMarker.length == 2) ? patchMarker[1].trim() : null); + final String marker = + ((patchMarker.length == 2) ? patchMarker[1].trim().toUpperCase() : null); return new SemVer(major, minor, patch, marker); } catch (Exception e) { - throw INVALID_SEMVER(version); + throw INVALID_SEMVER(version, e); } } @@ -54,6 +37,10 @@ private static IllegalArgumentException INVALID_SEMVER(String input) { return new IllegalArgumentException("Invalid SemVer format: " + input); } + private static IllegalArgumentException INVALID_SEMVER(String input, Throwable cause) { + return new IllegalArgumentException("Invalid SemVer format: " + input, cause); + } + @Override public int compareTo(SemVer o) { if (o.major > major) return -1;