From 838d40e90e835fce39f9833e61dd60118666e6ec Mon Sep 17 00:00:00 2001 From: Zafar Khaja Date: Sun, 3 Mar 2013 23:44:34 +0400 Subject: [PATCH] Make minor improvements --- pom.xml | 4 +- .../semver/AlphaNumericVersion.java | 34 ++++--- .../zafarkhaja/semver/NormalVersion.java | 20 ++--- .../com/github/zafarkhaja/semver/Version.java | 90 ++++++++++--------- .../semver/AlphaNumericVersionTest.java | 24 ++--- .../zafarkhaja/semver/NormalVersionTest.java | 34 +++---- .../github/zafarkhaja/semver/VersionTest.java | 38 ++++---- 7 files changed, 124 insertions(+), 120 deletions(-) diff --git a/pom.xml b/pom.xml index abccb5a..567f4bb 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.github.zafarkhaja semver - 0.2.0-SNAPSHOT + 0.2.1-SNAPSHOT jar semver @@ -46,7 +46,7 @@ test - + diff --git a/src/main/java/com/github/zafarkhaja/semver/AlphaNumericVersion.java b/src/main/java/com/github/zafarkhaja/semver/AlphaNumericVersion.java index b68acfc..08363fa 100644 --- a/src/main/java/com/github/zafarkhaja/semver/AlphaNumericVersion.java +++ b/src/main/java/com/github/zafarkhaja/semver/AlphaNumericVersion.java @@ -27,21 +27,19 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * + * * @author Zafar Khaja */ class AlphaNumericVersion implements Comparable { - + + private String value; + static final String FORMAT = "([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)"; private static final Pattern PATTERN = Pattern.compile("^" + FORMAT + "$"); - - private String value; - + AlphaNumericVersion(String value) { if (value == null) { - throw new NullPointerException( - "Alpha-numeric version MUST NOT be NULL" - ); + throw new NullPointerException("Alpha-numeric version MUST NOT be NULL"); } Matcher matcher = PATTERN.matcher(value); if (!matcher.matches()) { @@ -51,40 +49,40 @@ class AlphaNumericVersion implements Comparable { } this.value = matcher.group(0); } - + @Override public boolean equals(Object other) { if (this == other) { - return true; + return true; } if (!(other instanceof AlphaNumericVersion)) { return false; } return compareTo((AlphaNumericVersion) other) == 0 ? true : false; } - + @Override public int hashCode() { return value.hashCode(); } - + @Override public String toString() { return value; } - + @Override public int compareTo(AlphaNumericVersion other) { String[] thisIds = value.split("\\."); String[] otherIds = other.value.split("\\."); - + int result = compareIdentifierArrays(thisIds, otherIds); if (result == 0) { result = thisIds.length - otherIds.length; } return result; } - + private int compareIdentifierArrays(String[] ids1, String[] ids2) { int result = 0; int length = getLeastCommonArrayLength(ids1, ids2); @@ -96,11 +94,11 @@ class AlphaNumericVersion implements Comparable { } return result; } - + private int getLeastCommonArrayLength(String[] arr1, String[] arr2) { return arr1.length <= arr2.length ? arr1.length : arr2.length; } - + private int compareIdentifiers(String id1, String id2) { if (isInt(id1) && isInt(id2)) { return Integer.parseInt(id1) - Integer.parseInt(id2); @@ -108,7 +106,7 @@ class AlphaNumericVersion implements Comparable { return id1.compareTo(id2); } } - + private boolean isInt(String str) { try { Integer.parseInt(str); diff --git a/src/main/java/com/github/zafarkhaja/semver/NormalVersion.java b/src/main/java/com/github/zafarkhaja/semver/NormalVersion.java index dda755d..909893e 100644 --- a/src/main/java/com/github/zafarkhaja/semver/NormalVersion.java +++ b/src/main/java/com/github/zafarkhaja/semver/NormalVersion.java @@ -35,10 +35,10 @@ class NormalVersion implements Comparable { private int major; private int minor; private int patch; - + static final String FORMAT = "(\\d+)\\.(\\d+)\\.(\\d+)"; private static final Pattern PATTERN = Pattern.compile("^" + FORMAT + "$"); - + NormalVersion(int major, int minor, int patch) { if (major < 0 || minor < 0 || patch < 0) { throw new IllegalArgumentException( @@ -49,7 +49,7 @@ class NormalVersion implements Comparable { this.minor = minor; this.patch = patch; } - + static NormalVersion valueOf(String value) { Matcher matcher = PATTERN.matcher(value); if (!matcher.matches()) { @@ -73,13 +73,13 @@ class NormalVersion implements Comparable { int getPatch() { return patch; } - + void incrementMajor() { major = major + 1; minor = 0; patch = 0; } - + void incrementMinor() { minor = minor + 1; patch = 0; @@ -100,7 +100,7 @@ class NormalVersion implements Comparable { } return result; } - + @Override public boolean equals(Object other) { if (this == other) { @@ -120,12 +120,12 @@ class NormalVersion implements Comparable { hash = 31 * hash + patch; return hash; } - + /** * Returns the string representation of this normal version. - * - * A normal version number MUST take the form X.Y.Z where X, Y, and Z are - * non-negative integers. X is the major version, Y is the minor version, + * + * A normal version number MUST take the form X.Y.Z where X, Y, and Z are + * non-negative integers. X is the major version, Y is the minor version, * and Z is the patch version. (SemVer p.2) */ @Override diff --git a/src/main/java/com/github/zafarkhaja/semver/Version.java b/src/main/java/com/github/zafarkhaja/semver/Version.java index 60dfd76..1860648 100644 --- a/src/main/java/com/github/zafarkhaja/semver/Version.java +++ b/src/main/java/com/github/zafarkhaja/semver/Version.java @@ -31,19 +31,19 @@ import java.util.regex.Pattern; * @author Zafar Khaja */ public class Version implements Comparable { - + private NormalVersion normal; private AlphaNumericVersion preRelease; private AlphaNumericVersion build; - + private static final String PRE_RELEASE_PREFIX = "-"; private static final String BUILD_PREFIX = "+"; - + private static final Pattern SEMVER_PATTERN; - + static { StringBuilder sb = new StringBuilder(); - + sb.append("^"); sb.append(NormalVersion.FORMAT); sb.append("(?:"); @@ -56,91 +56,89 @@ public class Version implements Comparable { sb.append(AlphaNumericVersion.FORMAT); sb.append(")?"); sb.append("$"); - + SEMVER_PATTERN = Pattern.compile(sb.toString()); } - + Version( - NormalVersion normal, - AlphaNumericVersion preRelease, + NormalVersion normal, + AlphaNumericVersion preRelease, AlphaNumericVersion build ) { this.normal = normal; this.preRelease = preRelease; this.build = build; } - + public static Version valueOf(String value) { Matcher matcher = SEMVER_PATTERN.matcher(value); if (!matcher.matches()) { - throw new IllegalArgumentException( - "Illegal version format" - ); + throw new IllegalArgumentException("Illegal version format"); } - + NormalVersion normal = new NormalVersion( Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3)) ); - - AlphaNumericVersion preRelease = - (matcher.group(4) != null) ? - new AlphaNumericVersion(matcher.group(4)) : + + AlphaNumericVersion preRelease = + (matcher.group(4) != null) ? + new AlphaNumericVersion(matcher.group(4)) : null; - - AlphaNumericVersion build = - (matcher.group(5) != null) ? - new AlphaNumericVersion(matcher.group(5)) : + + AlphaNumericVersion build = + (matcher.group(5) != null) ? + new AlphaNumericVersion(matcher.group(5)) : null; - - return new Version(normal, preRelease, build); + + return new Version(normal, preRelease, build); } - + public int getMajorVersion() { return normal.getMajor(); } - + public int getMinorVersion() { return normal.getMinor(); } - + public int getPatchVersion() { return normal.getPatch(); } - + public String getNormalVersion() { return normal.toString(); } - + public String getPreReleaseVersion() { return (preRelease != null) ? preRelease.toString() : ""; } - + public String getBuildVersion() { return (build != null) ? build.toString() : ""; } - + public boolean greaterThan(Version other) { return compareTo(other) > 0 ? true : false; } - + public boolean greaterThanOrEqualsTo(Version other) { return compareTo(other) >= 0 ? true : false; } - + public boolean lessThan(Version other) { return compareTo(other) < 0 ? true : false; } - + public boolean lessThanOrEqualsTo(Version other) { return compareTo(other) <= 0 ? true : false; } - + @Override public boolean equals(Object other) { if (this == other) { - return true; + return true; } if (!(other instanceof Version)) { return false; @@ -156,7 +154,7 @@ public class Version implements Comparable { hash = 97 * hash + (build != null ? build.hashCode() : 0); return hash; } - + @Override public String toString() { StringBuilder sb = new StringBuilder(getNormalVersion()); @@ -170,7 +168,7 @@ public class Version implements Comparable { } return sb.toString(); } - + @Override public int compareTo(Version other) { int result = normal.compareTo(other.normal); @@ -182,23 +180,31 @@ public class Version implements Comparable { } return result; } - + private int comparePreReleases(Version other) { int result = 0; if (preRelease != null && other.preRelease != null) { result = preRelease.compareTo(other.preRelease); } else if (preRelease == null ^ other.preRelease == null) { - result = preRelease == null ? 1 : -1; + /** + * Pre-release versions satisfy but have a lower precedence + * than the associated normal version. (SemVer p.9) + */ + result = (preRelease == null) ? 1 : -1; } return result; } - + private int compareBuilds(Version other) { int result = 0; if (build != null && other.build != null) { result = build.compareTo(other.build); } else if (build == null ^ other.build == null) { - result = build == null ? -1 : 1; + /** + * Build versions satisfy and have a higher precedence + * than the associated normal version. (SemVer p.10) + */ + result = (build == null) ? -1 : 1; } return result; } diff --git a/src/test/java/com/github/zafarkhaja/semver/AlphaNumericVersionTest.java b/src/test/java/com/github/zafarkhaja/semver/AlphaNumericVersionTest.java index b7afd4d..9b45110 100644 --- a/src/test/java/com/github/zafarkhaja/semver/AlphaNumericVersionTest.java +++ b/src/test/java/com/github/zafarkhaja/semver/AlphaNumericVersionTest.java @@ -34,16 +34,16 @@ import org.junit.runner.RunWith; */ @RunWith(Enclosed.class) public class AlphaNumericVersionTest { - + public static class CoreFunctionalityTest { - + @Test public void mustConsistOfDotSeparatedIdentifiersOfAlphaNumericsAndHyphen() { String[] invalidVersions = { null, - "", - "123!", - "1a:2b:3c", + "", + "123!", + "1a:2b:3c", "123,abc,123", }; for (String ver : invalidVersions) { @@ -88,14 +88,14 @@ public class AlphaNumericVersionTest { public void shouldOverrideEqualsMethod() { AlphaNumericVersion v1 = new AlphaNumericVersion("alpha.123"); AlphaNumericVersion v2 = new AlphaNumericVersion("alpha.123"); - AlphaNumericVersion v3 = new AlphaNumericVersion("alpha.321"); + AlphaNumericVersion v3 = new AlphaNumericVersion("alpha.321"); assertTrue(v1.equals(v2)); assertFalse(v1.equals(v3)); } } - + public static class EqualsMethodTest { - + @Test public void shouldBeReflexive() { AlphaNumericVersion v = new AlphaNumericVersion("alpha.123"); @@ -142,9 +142,9 @@ public class AlphaNumericVersionTest { assertFalse(v1.equals(v2)); } } - + public static class HashCodeMethodTest { - + @Test public void shouldReturnSameHashCodeIfVersionsAreEqual() { AlphaNumericVersion v1 = new AlphaNumericVersion("alpha.123"); @@ -153,9 +153,9 @@ public class AlphaNumericVersionTest { assertEquals(v1.hashCode(), v2.hashCode()); } } - + public static class ToStringMethodTest { - + @Test public void shouldReturnStringRepresentation() { String value = "beta.abc.def"; diff --git a/src/test/java/com/github/zafarkhaja/semver/NormalVersionTest.java b/src/test/java/com/github/zafarkhaja/semver/NormalVersionTest.java index 4d4cbc0..7bab8c0 100644 --- a/src/test/java/com/github/zafarkhaja/semver/NormalVersionTest.java +++ b/src/test/java/com/github/zafarkhaja/semver/NormalVersionTest.java @@ -34,9 +34,9 @@ import org.junit.runner.RunWith; */ @RunWith(Enclosed.class) public class NormalVersionTest { - + public static class CoreFunctionalityTest { - + @Test public void mustConsistOfMajorMinorAndPatchVersions() { NormalVersion v = new NormalVersion(1, 2, 3); @@ -44,13 +44,13 @@ public class NormalVersionTest { assertEquals(2, v.getMinor()); assertEquals(3, v.getPatch()); } - + @Test public void mustTakeTheFormOfXDotYDotZWhereXyzAreNonNegativeIntegers() { NormalVersion v = new NormalVersion(1, 2, 3); assertEquals("1.2.3", v.toString()); } - + @Test public void shouldAcceptOnlyNonNegativeMajorMinorAndPatchVersions() { int[][] invalidVersions = {{-1, 2, 3}, {1, -2, 3}, {1, 2, -3}}; @@ -67,7 +67,7 @@ public class NormalVersionTest { fail("Major, minor and patch versions MUST be non-negative integers."); } } - + @Test public void mustIncreaseEachElementNumericallyByIncrementsOfOne() { int major = 1, minor = 2, patch = 3; @@ -79,7 +79,7 @@ public class NormalVersionTest { v.incrementMajor(); assertEquals(major + 1, v.getMajor()); } - + @Test public void mustResetMinorAndPatchToZeroWhenMajorIsIncremented() { NormalVersion v = new NormalVersion(1, 2, 3); @@ -88,7 +88,7 @@ public class NormalVersionTest { assertEquals(0, v.getMinor()); assertEquals(0, v.getPatch()); } - + @Test public void mustResetPatchToZeroWhenMinorIsIncremented() { NormalVersion v = new NormalVersion(1, 2, 3); @@ -97,7 +97,7 @@ public class NormalVersionTest { assertEquals(3, v.getMinor()); assertEquals(0, v.getPatch()); } - + @Test public void mustCompareMajorMinorAndPatchNumerically() { NormalVersion v = new NormalVersion(1, 2, 3); @@ -105,7 +105,7 @@ public class NormalVersionTest { assertTrue(0 == v.compareTo(new NormalVersion(1, 2, 3))); assertTrue(0 > v.compareTo(new NormalVersion(1, 2, 4))); } - + @Test public void shouldOverrideEqualsMethod() { NormalVersion v1 = new NormalVersion(1, 2, 3); @@ -114,7 +114,7 @@ public class NormalVersionTest { assertTrue(v1.equals(v2)); assertFalse(v1.equals(v3)); } - + @Test public void shouldHaveStaticFactoryMethod() { NormalVersion v = NormalVersion.valueOf("1.2.3"); @@ -123,15 +123,15 @@ public class NormalVersionTest { assertEquals(3, v.getPatch()); } } - + public static class EqualsMethodTest { - + @Test public void shouldBeReflexive() { NormalVersion v = new NormalVersion(1, 2, 3); assertTrue(v.equals(v)); } - + @Test public void shouldBeSymmetric() { NormalVersion v1 = new NormalVersion(1, 2, 3); @@ -172,9 +172,9 @@ public class NormalVersionTest { assertFalse(v1.equals(v2)); } } - + public static class HashCodeMethodTest { - + @Test public void shouldReturnSameHashCodeIfVersionsAreEqual() { NormalVersion v1 = new NormalVersion(1, 2, 3); @@ -183,9 +183,9 @@ public class NormalVersionTest { assertEquals(v1.hashCode(), v2.hashCode()); } } - + public static class ToStringMethodTest { - + @Test public void shouldReturnStringRepresentation() { NormalVersion v = new NormalVersion(1, 2, 3); diff --git a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java index 00bbf8c..2d69971 100644 --- a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java +++ b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java @@ -34,9 +34,9 @@ import org.junit.runner.RunWith; */ @RunWith(Enclosed.class) public class VersionTest { - + public static class CoreFunctionalityTest { - + @Test public void mayHavePreReleaseFollowingPatchAppendedWithHyphen() { Version v = Version.valueOf("1.2.3-alpha"); @@ -121,16 +121,16 @@ public class VersionTest { @Test public void shouldCorrectlyCompareAllVersionsFromSpecification() { String[] versions = { - "1.0.0-alpha", - "1.0.0-alpha.1", - "1.0.0-beta.2", - "1.0.0-beta.11", - "1.0.0-rc.1", - "1.0.0-rc.1+build.1", - "1.0.0", - "1.0.0+0.3.7", - "1.3.7+build", - "1.3.7+build.2.b8f12d7", + "1.0.0-alpha", + "1.0.0-alpha.1", + "1.0.0-beta.2", + "1.0.0-beta.11", + "1.0.0-rc.1", + "1.0.0-rc.1+build.1", + "1.0.0", + "1.0.0+0.3.7", + "1.3.7+build", + "1.3.7+build.2.b8f12d7", "1.3.7+build.11.e0f985a" }; for (int i = 1; i < versions.length; i++) { @@ -139,7 +139,7 @@ public class VersionTest { assertTrue(v1.lessThan(v2)); } } - + @Test public void shouldHaveStaticFactoryMethod() { Version v = Version.valueOf("1.0.0-rc.1+build.1"); @@ -151,9 +151,9 @@ public class VersionTest { assertEquals("build.1", v.getBuildVersion()); } } - + public static class EqualsMethodTest { - + @Test public void shouldBeReflexive() { Version v1 = Version.valueOf("2.3.7"); @@ -200,9 +200,9 @@ public class VersionTest { assertFalse(v1.equals(v2)); } } - + public static class HashCodeMethodTest { - + @Test public void shouldReturnSameHashCodeIfVersionsAreEqual() { Version v1 = Version.valueOf("2.3.7"); @@ -211,9 +211,9 @@ public class VersionTest { assertEquals(v1.hashCode(), v2.hashCode()); } } - + public static class ToStringMethodTest { - + @Test public void shouldReturnStringRepresentation() { String value = "1.2.3-beta+build";