[jsemver] 15/95: Make version classes immutable
Alexandre Viau
reazem-guest at moszumanska.debian.org
Mon Feb 16 14:58:25 UTC 2015
This is an automated email from the git hooks/post-receive script.
reazem-guest pushed a commit to branch master
in repository jsemver.
commit c0baccf4ba6730e684df104c91a0a5e1f9518e35
Author: Zafar Khaja <zafarkhaja at gmail.com>
Date: Wed Mar 6 00:46:53 2013 +0400
Make version classes immutable
---
.../zafarkhaja/semver/AlphaNumericVersion.java | 2 +-
.../github/zafarkhaja/semver/NormalVersion.java | 21 +++++-------
.../java/com/github/zafarkhaja/semver/Version.java | 18 +++++-----
.../zafarkhaja/semver/NormalVersionTest.java | 39 ++++++++++++++--------
.../com/github/zafarkhaja/semver/VersionTest.java | 23 +++++++++----
5 files changed, 61 insertions(+), 42 deletions(-)
diff --git a/src/main/java/com/github/zafarkhaja/semver/AlphaNumericVersion.java b/src/main/java/com/github/zafarkhaja/semver/AlphaNumericVersion.java
index 08363fa..981f9bd 100644
--- a/src/main/java/com/github/zafarkhaja/semver/AlphaNumericVersion.java
+++ b/src/main/java/com/github/zafarkhaja/semver/AlphaNumericVersion.java
@@ -32,7 +32,7 @@ import java.util.regex.Pattern;
*/
class AlphaNumericVersion implements Comparable<AlphaNumericVersion> {
- private String value;
+ private final String value;
static final String FORMAT = "([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)";
private static final Pattern PATTERN = Pattern.compile("^" + FORMAT + "$");
diff --git a/src/main/java/com/github/zafarkhaja/semver/NormalVersion.java b/src/main/java/com/github/zafarkhaja/semver/NormalVersion.java
index 909893e..51639b1 100644
--- a/src/main/java/com/github/zafarkhaja/semver/NormalVersion.java
+++ b/src/main/java/com/github/zafarkhaja/semver/NormalVersion.java
@@ -32,9 +32,9 @@ import java.util.regex.Pattern;
*/
class NormalVersion implements Comparable<NormalVersion> {
- private int major;
- private int minor;
- private int patch;
+ private final int major;
+ private final int minor;
+ private final int patch;
static final String FORMAT = "(\\d+)\\.(\\d+)\\.(\\d+)";
private static final Pattern PATTERN = Pattern.compile("^" + FORMAT + "$");
@@ -74,19 +74,16 @@ class NormalVersion implements Comparable<NormalVersion> {
return patch;
}
- void incrementMajor() {
- major = major + 1;
- minor = 0;
- patch = 0;
+ NormalVersion incrementMajor() {
+ return new NormalVersion(major + 1, 0, 0);
}
- void incrementMinor() {
- minor = minor + 1;
- patch = 0;
+ NormalVersion incrementMinor() {
+ return new NormalVersion(major, minor + 1, 0);
}
- void incrementPatch() {
- patch = patch + 1;
+ NormalVersion incrementPatch() {
+ return new NormalVersion(major, minor, patch + 1);
}
@Override
diff --git a/src/main/java/com/github/zafarkhaja/semver/Version.java b/src/main/java/com/github/zafarkhaja/semver/Version.java
index b66bcba..072c2c2 100644
--- a/src/main/java/com/github/zafarkhaja/semver/Version.java
+++ b/src/main/java/com/github/zafarkhaja/semver/Version.java
@@ -32,9 +32,9 @@ import java.util.regex.Pattern;
*/
public class Version implements Comparable<Version> {
- private NormalVersion normal;
- private AlphaNumericVersion preRelease;
- private AlphaNumericVersion build;
+ private final NormalVersion normal;
+ private final AlphaNumericVersion preRelease;
+ private final AlphaNumericVersion build;
private static final String PRE_RELEASE_PREFIX = "-";
private static final String BUILD_PREFIX = "+";
@@ -92,16 +92,16 @@ public class Version implements Comparable<Version> {
return new Version(normal, preRelease, build);
}
- public void incrementMajorVersion() {
- normal.incrementMajor();
+ public Version incrementMajorVersion() {
+ return new Version(normal.incrementMajor(), preRelease, build);
}
- public void incrementMinorVersion() {
- normal.incrementMinor();
+ public Version incrementMinorVersion() {
+ return new Version(normal.incrementMinor(), preRelease, build);
}
- public void incrementPatchVersion() {
- normal.incrementPatch();
+ public Version incrementPatchVersion() {
+ return new Version(normal.incrementPatch(), preRelease, build);
}
public int getMajorVersion() {
diff --git a/src/test/java/com/github/zafarkhaja/semver/NormalVersionTest.java b/src/test/java/com/github/zafarkhaja/semver/NormalVersionTest.java
index 7bab8c0..587e312 100644
--- a/src/test/java/com/github/zafarkhaja/semver/NormalVersionTest.java
+++ b/src/test/java/com/github/zafarkhaja/semver/NormalVersionTest.java
@@ -72,30 +72,30 @@ public class NormalVersionTest {
public void mustIncreaseEachElementNumericallyByIncrementsOfOne() {
int major = 1, minor = 2, patch = 3;
NormalVersion v = new NormalVersion(major, minor, patch);
- v.incrementPatch();
- assertEquals(patch + 1, v.getPatch());
- v.incrementMinor();
- assertEquals(minor + 1, v.getMinor());
- v.incrementMajor();
- assertEquals(major + 1, v.getMajor());
+ NormalVersion incrementedPatch = v.incrementPatch();
+ assertEquals(patch + 1, incrementedPatch.getPatch());
+ NormalVersion incrementedMinor = v.incrementMinor();
+ assertEquals(minor + 1, incrementedMinor.getMinor());
+ NormalVersion incrementedMajor = v.incrementMajor();
+ assertEquals(major + 1, incrementedMajor.getMajor());
}
@Test
public void mustResetMinorAndPatchToZeroWhenMajorIsIncremented() {
NormalVersion v = new NormalVersion(1, 2, 3);
- v.incrementMajor();
- assertEquals(2, v.getMajor());
- assertEquals(0, v.getMinor());
- assertEquals(0, v.getPatch());
+ NormalVersion incremented = v.incrementMajor();
+ assertEquals(2, incremented.getMajor());
+ assertEquals(0, incremented.getMinor());
+ assertEquals(0, incremented.getPatch());
}
@Test
public void mustResetPatchToZeroWhenMinorIsIncremented() {
NormalVersion v = new NormalVersion(1, 2, 3);
- v.incrementMinor();
- assertEquals(1, v.getMajor());
- assertEquals(3, v.getMinor());
- assertEquals(0, v.getPatch());
+ NormalVersion incremented = v.incrementMinor();
+ assertEquals(1, incremented.getMajor());
+ assertEquals(3, incremented.getMinor());
+ assertEquals(0, incremented.getPatch());
}
@Test
@@ -122,6 +122,17 @@ public class NormalVersionTest {
assertEquals(2, v.getMinor());
assertEquals(3, v.getPatch());
}
+
+ @Test
+ public void shoudBeImmutable() {
+ NormalVersion version = new NormalVersion(1, 2, 3);
+ NormalVersion incementedMajor = version.incrementMajor();
+ assertNotSame(version, incementedMajor);
+ NormalVersion incementedMinor = version.incrementMinor();
+ assertNotSame(version, incementedMinor);
+ NormalVersion incementedPatch = version.incrementPatch();
+ assertNotSame(version, incementedPatch);
+ }
}
public static class EqualsMethodTest {
diff --git a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java
index e1286c7..d8fe02a 100644
--- a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java
+++ b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java
@@ -154,22 +154,33 @@ public class VersionTest {
@Test
public void shouldProvideIncrementMajorVersionMethod() {
Version v = Version.valueOf("1.2.3");
- v.incrementMajorVersion();
- assertEquals("2.0.0", v.toString());
+ Version incrementedMajor = v.incrementMajorVersion();
+ assertEquals("2.0.0", incrementedMajor.toString());
}
@Test
public void shouldProvideIncrementMinorVersionMethod() {
Version v = Version.valueOf("1.2.3");
- v.incrementMinorVersion();
- assertEquals("1.3.0", v.toString());
+ Version incrementedMinor = v.incrementMinorVersion();
+ assertEquals("1.3.0", incrementedMinor.toString());
}
@Test
public void shouldProvideIncrementPatchVersionMethod() {
Version v = Version.valueOf("1.2.3");
- v.incrementPatchVersion();
- assertEquals("1.2.4", v.toString());
+ Version incrementedPatch = v.incrementPatchVersion();
+ assertEquals("1.2.4", incrementedPatch.toString());
+ }
+
+ @Test
+ public void shouldBeImmutable() {
+ Version version = Version.valueOf("1.2.3");
+ Version incementedMajor = version.incrementMajorVersion();
+ assertNotSame(version, incementedMajor);
+ Version incementedMinor = version.incrementMinorVersion();
+ assertNotSame(version, incementedMinor);
+ Version incementedPatch = version.incrementPatchVersion();
+ assertNotSame(version, incementedPatch);
}
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jsemver.git
More information about the pkg-java-commits
mailing list