Skip to content

Commit

Permalink
Conversion of internal package classes to use static only (#240)
Browse files Browse the repository at this point in the history
* Internal package classes are now containers of pure functions; Slight refinements of Coerce to directly use pattern-matching string as well as Optional for parsing nullable values

* Addressing PR comments

* Addressing PR comment
  • Loading branch information
severn-everett committed Apr 17, 2024
1 parent 671d9c6 commit 84a4c13
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 107 deletions.
31 changes: 17 additions & 14 deletions src/main/java/org/semver4j/Semver.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Semver implements Comparable<Semver> {
public Semver(@NotNull final String version) {
this.originalVersion = version.trim();

Version parsedVersion = new StrictParser().parse(this.originalVersion);
Version parsedVersion = StrictParser.parse(this.originalVersion);

major = parsedVersion.getMajor();
minor = parsedVersion.getMinor();
Expand Down Expand Up @@ -212,7 +212,7 @@ public boolean isStable() {
*/
@NotNull
public Semver nextMajor() {
return new Modifier(this).nextMajor();
return Modifier.nextMajor(this);
}

/**
Expand All @@ -233,7 +233,7 @@ public Semver withIncMajor() {
*/
@NotNull
public Semver withIncMajor(int number) {
return new Modifier(this).withIncMajor(number);
return Modifier.withIncMajor(this, number);
}

/**
Expand All @@ -243,7 +243,7 @@ public Semver withIncMajor(int number) {
*/
@NotNull
public Semver nextMinor() {
return new Modifier(this).nextMinor();
return Modifier.nextMinor(this);
}

/**
Expand All @@ -264,7 +264,7 @@ public Semver withIncMinor() {
*/
@NotNull
public Semver withIncMinor(int number) {
return new Modifier(this).withIncMinor(number);
return Modifier.withIncMinor(this, number);
}

/**
Expand All @@ -274,7 +274,7 @@ public Semver withIncMinor(int number) {
*/
@NotNull
public Semver nextPatch() {
return new Modifier(this).nextPatch();
return Modifier.nextPatch(this);
}

/**
Expand All @@ -295,7 +295,7 @@ public Semver withIncPatch() {
*/
@NotNull
public Semver withIncPatch(int number) {
return new Modifier(this).withIncPatch(number);
return Modifier.withIncPatch(this, number);
}

/**
Expand All @@ -306,7 +306,7 @@ public Semver withIncPatch(int number) {
*/
@NotNull
public Semver withPreRelease(@NotNull final String preRelease) {
return new Modifier(this).withPreRelease(preRelease);
return Modifier.withPreRelease(this, preRelease);
}

/**
Expand All @@ -317,7 +317,7 @@ public Semver withPreRelease(@NotNull final String preRelease) {
*/
@NotNull
public Semver withBuild(@NotNull final String build) {
return new Modifier(this).withBuild(build);
return Modifier.withBuild(this, build);
}

/**
Expand All @@ -327,7 +327,7 @@ public Semver withBuild(@NotNull final String build) {
*/
@NotNull
public Semver withClearedPreRelease() {
return new Modifier(this).withClearedPreRelease();
return Modifier.withClearedPreRelease(this);
}

/**
Expand All @@ -337,7 +337,7 @@ public Semver withClearedPreRelease() {
*/
@NotNull
public Semver withClearedBuild() {
return new Modifier(this).withClearedBuild();
return Modifier.withClearedBuild(this);
}

/**
Expand All @@ -347,12 +347,12 @@ public Semver withClearedBuild() {
*/
@NotNull
public Semver withClearedPreReleaseAndBuild() {
return new Modifier(this).withClearedPreReleaseAndBuild();
return Modifier.withClearedPreReleaseAndBuild(this);
}

@Override
public int compareTo(@NotNull final Semver other) {
return new Comparator(this).compareTo(other);
return Comparator.compareTo(this, other);
}

/**
Expand All @@ -365,6 +365,7 @@ public boolean isApiCompatible(@NotNull final String version) {
/**
* Checks whether the given version is API compatible with this version.
*/
@SuppressWarnings("unused")
public boolean isApiCompatible(@NotNull final Semver version) {
return diff(version).ordinal() < VersionDiff.MAJOR.ordinal();
}
Expand Down Expand Up @@ -526,7 +527,7 @@ public VersionDiff diff(@NotNull final String version) {
*/
@NotNull
public VersionDiff diff(@NotNull final Semver version) {
return new Differ(this).diff(version);
return Differ.diff(this, version);
}

/**
Expand Down Expand Up @@ -632,6 +633,7 @@ public Builder withPatch(int patch) {
return this;
}

@SuppressWarnings("unused")
public Builder withPreRelease(@NotNull String preRelease) {
requireNonNull(preRelease, "preRelease cannot be null");
return withPreReleases(new String[]{preRelease});
Expand All @@ -649,6 +651,7 @@ public Builder withPreReleases(@NotNull String[] preReleases) {
return this;
}

@SuppressWarnings("unused")
public Builder withBuild(@NotNull String build) {
requireNonNull(build, "build cannot be null");
return withBuilds(new String[]{build});
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/org/semver4j/internal/Coerce.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@
import org.jetbrains.annotations.Nullable;

import java.util.Locale;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.String.format;
import static java.util.regex.Pattern.compile;
import static org.semver4j.internal.Tokenizers.COERCE;

public class Coerce {
@NotNull
private static final Pattern pattern = compile(COERCE);
private static final Pattern PATTERN = compile(
"(^|\\D)(\\d{1,16})(?:\\.(\\d{1,16}))?(?:\\.(\\d{1,16}))?(?:$|\\D)"
);

private Coerce() {
}

@Nullable
public static String coerce(@NotNull final String version) {
Matcher matcher = pattern.matcher(version);
Matcher matcher = PATTERN.matcher(version);

if (matcher.find()) {
String group1 = matcher.group(0);
String group2 = matcher.group(1);
String group3 = matcher.group(2);
String group4 = matcher.group(3);
String group5 = matcher.group(4);

group4 = group4 != null ? group4 : "0";
group5 = group5 != null ? group5 : "0";
String coercedMajor = matcher.group(2);
String coercedMinor = Optional.ofNullable(matcher.group(3)).orElse("0");
String coercedPath = Optional.ofNullable(matcher.group(4)).orElse("0");

return format(Locale.ROOT, "%s.%s.%s", group3, group4, group5);
return format(Locale.ROOT, "%s.%s.%s", coercedMajor, coercedMinor, coercedPath);
}

return null;
Expand Down
23 changes: 9 additions & 14 deletions src/main/java/org/semver4j/internal/Comparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import static java.lang.Math.max;

public class Comparator implements Comparable<Semver> {
public class Comparator {
private static final String ALL_DIGITS = "^\\d+$";
private static final String CONTAINS_DIGITS = ".*\\d.*";
private static final String TRAILING_DIGITS_EXTRACT = "(?<=\\D)(?=\\d)";
Expand All @@ -17,23 +17,18 @@ public class Comparator implements Comparable<Semver> {
@NotNull
private static final String UNDEFINED_MARKER = "undef";

@NotNull
private final Semver version;

public Comparator(@NotNull final Semver version) {
this.version = version;
private Comparator() {
}

@Override
public int compareTo(@NotNull final Semver other) {
int result = mainCompare(other);
public static int compareTo(@NotNull final Semver version, @NotNull final Semver other) {
int result = mainCompare(version, other);
if (result == 0) {
return preReleaseCompare(other);
return preReleaseCompare(version, other);
}
return result;
}

private int mainCompare(@NotNull final Semver other) {
private static int mainCompare(@NotNull final Semver version, @NotNull final Semver other) {
int majorCompare = Long.compare(version.getMajor(), other.getMajor());
if (majorCompare == 0) {
int minorCompare = Long.compare(version.getMinor(), other.getMinor());
Expand All @@ -47,7 +42,7 @@ private int mainCompare(@NotNull final Semver other) {
}
}

private int preReleaseCompare(@NotNull final Semver other) {
private static int preReleaseCompare(@NotNull final Semver version, @NotNull final Semver other) {
if (!version.getPreRelease().isEmpty() && other.getPreRelease().isEmpty()) {
return -1;
} else if (version.getPreRelease().isEmpty() && !other.getPreRelease().isEmpty()) {
Expand Down Expand Up @@ -81,7 +76,7 @@ private int preReleaseCompare(@NotNull final Semver other) {
return 0;
}

private int compareIdentifiers(@NotNull final String a, @NotNull final String b) {
private static int compareIdentifiers(@NotNull final String a, @NotNull final String b) {
// Only attempt to parse fully-numeric string sequences so that we can avoid
// raising a costly exception
if (a.matches(ALL_DIGITS) && b.matches(ALL_DIGITS)) {
Expand Down Expand Up @@ -131,7 +126,7 @@ private Integer checkAlphanumericPrerelease(@NotNull final String a, @NotNull fi
}

@NotNull
private String getString(final int i, @NotNull final List<@NotNull String> list) {
private static String getString(final int i, @NotNull final List<@NotNull String> list) {
if (list.size() > i) {
return list.get(i);
} else {
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/semver4j/internal/Differ.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@
import static org.semver4j.Semver.VersionDiff.*;

public class Differ {
@NotNull
private final Semver version;

public Differ(@NotNull final Semver version) {
this.version = version;
private Differ() {
}

@NotNull
public VersionDiff diff(@NotNull final Semver other) {
public static VersionDiff diff(@NotNull final Semver version, @NotNull final Semver other) {
if (version.getMajor() != other.getMajor()) {
return MAJOR;
}
Expand Down

0 comments on commit 84a4c13

Please sign in to comment.