Skip to content

Commit

Permalink
Turn SemVer class into a record
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 23, 2024
1 parent afd1958 commit 1742869
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/cjburkey/claimchunk/ClaimChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
35 changes: 11 additions & 24 deletions src/main/java/com/cjburkey/claimchunk/update/SemVer.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
package com.cjburkey.claimchunk.update;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
* A nice little semantic versioning class. Feel free to use this in your own projects if you want
* :)
*/
public class SemVer implements Comparable<SemVer> {
public record SemVer(int major, int minor, int patch, String marker) implements Comparable<SemVer> {

@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) {
Expand All @@ -43,17 +25,22 @@ 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);
}
}

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;
Expand Down

0 comments on commit 1742869

Please sign in to comment.