diff --git a/src/main/java/com/github/zafarkhaja/semver/Version.java b/src/main/java/com/github/zafarkhaja/semver/Version.java index 994446d..17cbd3c 100644 --- a/src/main/java/com/github/zafarkhaja/semver/Version.java +++ b/src/main/java/com/github/zafarkhaja/semver/Version.java @@ -33,11 +33,12 @@ import java.util.regex.Pattern; */ public class Version implements Comparable { - private int major; - private int minor; - private int patch; - private String preRelease; - private String build; + private int majorVersion; + private int minorVersion; + private int patchVersion; + + private String preReleaseVersion; + private String buildVersion; private static final String NORMAL_VERSION = "((?\\d+)\\.(?\\d+)\\.(?\\d+))"; @@ -59,73 +60,73 @@ public class Version implements Comparable { "Illegal version format" ); } - major = Integer.parseInt(matcher.group("major")); - minor = Integer.parseInt(matcher.group("minor")); - patch = Integer.parseInt(matcher.group("patch")); + majorVersion = Integer.parseInt(matcher.group("major")); + minorVersion = Integer.parseInt(matcher.group("minor")); + patchVersion = Integer.parseInt(matcher.group("patch")); - preRelease = matcher.group("preRelease"); - build = matcher.group("build"); + preReleaseVersion = matcher.group("preRelease"); + buildVersion = matcher.group("build"); } - public int getMajor() { - return major; + public int getMajorVersion() { + return majorVersion; } - public int getMinor() { - return minor; + public int getMinorVersion() { + return minorVersion; } - public int getPatch() { - return patch; + public int getPatchVersion() { + return patchVersion; } - public String getPreRelease() { - return preRelease; + public String getPreReleaseVersion() { + return preReleaseVersion; } - public String getBuild() { - return build; + public String getBuildVersion() { + return buildVersion; } - public void bumpMajor() { - major = major + 1; - minor = 0; - patch = 0; + public void bumpMajorVersion() { + majorVersion = majorVersion + 1; + minorVersion = 0; + patchVersion = 0; } - public void bumpMinor() { - minor = minor + 1; - patch = 0; + public void bumpMinorVersion() { + minorVersion = minorVersion + 1; + patchVersion = 0; } - public void bumpPatch() { - patch = patch + 1; + public void bumpPatchVersion() { + patchVersion = patchVersion + 1; } @Override public int compareTo(Version other) { int result = compareNormalVersions(other); - if (result == 0 && preRelease != null) { + if (result == 0 && preReleaseVersion != null) { result = compareAlphaNumericVersions( - preRelease, - other.getPreRelease() + preReleaseVersion, + other.getPreReleaseVersion() ); } - if (result == 0 && build != null) { + if (result == 0 && buildVersion != null) { result = compareAlphaNumericVersions( - build, - other.getBuild() + buildVersion, + other.getBuildVersion() ); } return result; } private int compareNormalVersions(Version other) { - int result = compareInts(major, other.getMajor()); + int result = compareInts(majorVersion, other.getMajorVersion()); if (result == 0) { - result = compareInts(minor, other.getMinor()); + result = compareInts(minorVersion, other.getMinorVersion()); if (result == 0) { - result = compareInts(patch, other.getPatch()); + result = compareInts(patchVersion, other.getPatchVersion()); } } return result; @@ -148,8 +149,7 @@ public class Version implements Comparable { private int compareIdentifierArrays(String[] thisArr, String[] otherArr) { int result = 0; - int loopCount = getSmallestArrayLength(thisArr, otherArr); - for (int i = 0; i < loopCount; i++) { + for (int i = 0; i < getSmallestArrayLength(thisArr, otherArr); i++) { result = compareIdentifiers(thisArr[i], otherArr[i]); if (result != 0) { break; @@ -173,6 +173,10 @@ public class Version implements Comparable { Integer.parseInt(otherIdent) ); } else if (isInt(thisIdent) || isInt(otherIdent)) { + /** + * Numeric identifiers always have lower precedence + * than non-numeric identifiers. + */ return isInt(thisIdent) ? -1 : 1; } else { return thisIdent.compareTo(otherIdent); diff --git a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java index 9588895..e618002 100644 --- a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java +++ b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java @@ -36,17 +36,17 @@ public class VersionTest { @Test public void mustConsistOfMajorMinorAndPatchVersions() { Version version = new Version("1.2.3"); - assertNotNull(version.getMajor()); - assertNotNull(version.getMinor()); - assertNotNull(version.getPatch()); + assertNotNull(version.getMajorVersion()); + assertNotNull(version.getMinorVersion()); + assertNotNull(version.getPatchVersion()); } @Test public void mustTakeTheFormOfXDotYDotZWhereXyzAreNonNegativeIntegers() { Version version = new Version("1.2.3"); - assertEquals(1, version.getMajor()); - assertEquals(2, version.getMinor()); - assertEquals(3, version.getPatch()); + assertEquals(1, version.getMajorVersion()); + assertEquals(2, version.getMinorVersion()); + assertEquals(3, version.getPatchVersion()); } @Test public void @@ -65,54 +65,54 @@ public class VersionTest { @Test public void mustIncreaseEachElementNumericallyByIncrementsOfOne() { Version version = new Version("1.2.3"); - version.bumpPatch(); - assertEquals(4, version.getPatch()); - version.bumpMinor(); - assertEquals(3, version.getMinor()); - version.bumpMajor(); - assertEquals(2, version.getMajor()); + version.bumpPatchVersion(); + assertEquals(4, version.getPatchVersion()); + version.bumpMinorVersion(); + assertEquals(3, version.getMinorVersion()); + version.bumpMajorVersion(); + assertEquals(2, version.getMajorVersion()); } @Test public void mustResetToZeroMinorAndPatchVersionsWhenMajorVersionIsIncremented() { Version version = new Version("1.2.3"); - version.bumpMajor(); - assertEquals(2, version.getMajor()); - assertEquals(0, version.getMinor()); - assertEquals(0, version.getPatch()); + version.bumpMajorVersion(); + assertEquals(2, version.getMajorVersion()); + assertEquals(0, version.getMinorVersion()); + assertEquals(0, version.getPatchVersion()); } @Test public void mustResetToZeroPatchVersionWhenMinorVersionIsIncremented() { Version version = new Version("1.2.3"); - version.bumpMinor(); - assertEquals(1, version.getMajor()); - assertEquals(3, version.getMinor()); - assertEquals(0, version.getPatch()); + version.bumpMinorVersion(); + assertEquals(1, version.getMajorVersion()); + assertEquals(3, version.getMinorVersion()); + assertEquals(0, version.getPatchVersion()); } @Test public void mayHavePreReleaseVersionFollowingPatchVersionAppendedWithDash() { Version version = new Version("1.2.3-alpha"); - assertEquals("alpha", version.getPreRelease()); + assertEquals("alpha", version.getPreReleaseVersion()); } @Test public void preReleaseVersionMustCompriseDotSeparatedIdentifiersOfAlphaNumericsAndDash() { Version version = new Version("1.0.0-x.7.z.92"); - assertEquals("x.7.z.92", version.getPreRelease()); + assertEquals("x.7.z.92", version.getPreReleaseVersion()); } @Test public void mayHaveBuildVersionFollowingPatchOrPreReleaseVersionsAppendedWithPlus() { Version version = new Version("1.2.3+build"); - assertEquals("build", version.getBuild()); + assertEquals("build", version.getBuildVersion()); } @Test public void buildVersionMustCompriseDotSeparatedIdentifiersOfAlphaNumericsAndDash() { Version version = new Version("1.3.7+build.11.e0f985a"); - assertEquals("build.11.e0f985a", version.getBuild()); + assertEquals("build.11.e0f985a", version.getBuildVersion()); } @Test public void