Make refactoring, rename class members, add comment

This commit is contained in:
Zafar Khaja 2012-11-06 22:44:35 +04:00
parent 220384023c
commit e5041f42cb
2 changed files with 68 additions and 64 deletions

View File

@ -33,11 +33,12 @@ import java.util.regex.Pattern;
*/ */
public class Version implements Comparable<Version> { public class Version implements Comparable<Version> {
private int major; private int majorVersion;
private int minor; private int minorVersion;
private int patch; private int patchVersion;
private String preRelease;
private String build; private String preReleaseVersion;
private String buildVersion;
private static final String NORMAL_VERSION = private static final String NORMAL_VERSION =
"((?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+))"; "((?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+))";
@ -59,73 +60,73 @@ public class Version implements Comparable<Version> {
"Illegal version format" "Illegal version format"
); );
} }
major = Integer.parseInt(matcher.group("major")); majorVersion = Integer.parseInt(matcher.group("major"));
minor = Integer.parseInt(matcher.group("minor")); minorVersion = Integer.parseInt(matcher.group("minor"));
patch = Integer.parseInt(matcher.group("patch")); patchVersion = Integer.parseInt(matcher.group("patch"));
preRelease = matcher.group("preRelease"); preReleaseVersion = matcher.group("preRelease");
build = matcher.group("build"); buildVersion = matcher.group("build");
} }
public int getMajor() { public int getMajorVersion() {
return major; return majorVersion;
} }
public int getMinor() { public int getMinorVersion() {
return minor; return minorVersion;
} }
public int getPatch() { public int getPatchVersion() {
return patch; return patchVersion;
} }
public String getPreRelease() { public String getPreReleaseVersion() {
return preRelease; return preReleaseVersion;
} }
public String getBuild() { public String getBuildVersion() {
return build; return buildVersion;
} }
public void bumpMajor() { public void bumpMajorVersion() {
major = major + 1; majorVersion = majorVersion + 1;
minor = 0; minorVersion = 0;
patch = 0; patchVersion = 0;
} }
public void bumpMinor() { public void bumpMinorVersion() {
minor = minor + 1; minorVersion = minorVersion + 1;
patch = 0; patchVersion = 0;
} }
public void bumpPatch() { public void bumpPatchVersion() {
patch = patch + 1; patchVersion = patchVersion + 1;
} }
@Override @Override
public int compareTo(Version other) { public int compareTo(Version other) {
int result = compareNormalVersions(other); int result = compareNormalVersions(other);
if (result == 0 && preRelease != null) { if (result == 0 && preReleaseVersion != null) {
result = compareAlphaNumericVersions( result = compareAlphaNumericVersions(
preRelease, preReleaseVersion,
other.getPreRelease() other.getPreReleaseVersion()
); );
} }
if (result == 0 && build != null) { if (result == 0 && buildVersion != null) {
result = compareAlphaNumericVersions( result = compareAlphaNumericVersions(
build, buildVersion,
other.getBuild() other.getBuildVersion()
); );
} }
return result; return result;
} }
private int compareNormalVersions(Version other) { private int compareNormalVersions(Version other) {
int result = compareInts(major, other.getMajor()); int result = compareInts(majorVersion, other.getMajorVersion());
if (result == 0) { if (result == 0) {
result = compareInts(minor, other.getMinor()); result = compareInts(minorVersion, other.getMinorVersion());
if (result == 0) { if (result == 0) {
result = compareInts(patch, other.getPatch()); result = compareInts(patchVersion, other.getPatchVersion());
} }
} }
return result; return result;
@ -148,8 +149,7 @@ public class Version implements Comparable<Version> {
private int compareIdentifierArrays(String[] thisArr, String[] otherArr) { private int compareIdentifierArrays(String[] thisArr, String[] otherArr) {
int result = 0; int result = 0;
int loopCount = getSmallestArrayLength(thisArr, otherArr); for (int i = 0; i < getSmallestArrayLength(thisArr, otherArr); i++) {
for (int i = 0; i < loopCount; i++) {
result = compareIdentifiers(thisArr[i], otherArr[i]); result = compareIdentifiers(thisArr[i], otherArr[i]);
if (result != 0) { if (result != 0) {
break; break;
@ -173,6 +173,10 @@ public class Version implements Comparable<Version> {
Integer.parseInt(otherIdent) Integer.parseInt(otherIdent)
); );
} else if (isInt(thisIdent) || isInt(otherIdent)) { } else if (isInt(thisIdent) || isInt(otherIdent)) {
/**
* Numeric identifiers always have lower precedence
* than non-numeric identifiers.
*/
return isInt(thisIdent) ? -1 : 1; return isInt(thisIdent) ? -1 : 1;
} else { } else {
return thisIdent.compareTo(otherIdent); return thisIdent.compareTo(otherIdent);

View File

@ -36,17 +36,17 @@ public class VersionTest {
@Test public void @Test public void
mustConsistOfMajorMinorAndPatchVersions() { mustConsistOfMajorMinorAndPatchVersions() {
Version version = new Version("1.2.3"); Version version = new Version("1.2.3");
assertNotNull(version.getMajor()); assertNotNull(version.getMajorVersion());
assertNotNull(version.getMinor()); assertNotNull(version.getMinorVersion());
assertNotNull(version.getPatch()); assertNotNull(version.getPatchVersion());
} }
@Test public void @Test public void
mustTakeTheFormOfXDotYDotZWhereXyzAreNonNegativeIntegers() { mustTakeTheFormOfXDotYDotZWhereXyzAreNonNegativeIntegers() {
Version version = new Version("1.2.3"); Version version = new Version("1.2.3");
assertEquals(1, version.getMajor()); assertEquals(1, version.getMajorVersion());
assertEquals(2, version.getMinor()); assertEquals(2, version.getMinorVersion());
assertEquals(3, version.getPatch()); assertEquals(3, version.getPatchVersion());
} }
@Test public void @Test public void
@ -65,54 +65,54 @@ public class VersionTest {
@Test public void @Test public void
mustIncreaseEachElementNumericallyByIncrementsOfOne() { mustIncreaseEachElementNumericallyByIncrementsOfOne() {
Version version = new Version("1.2.3"); Version version = new Version("1.2.3");
version.bumpPatch(); version.bumpPatchVersion();
assertEquals(4, version.getPatch()); assertEquals(4, version.getPatchVersion());
version.bumpMinor(); version.bumpMinorVersion();
assertEquals(3, version.getMinor()); assertEquals(3, version.getMinorVersion());
version.bumpMajor(); version.bumpMajorVersion();
assertEquals(2, version.getMajor()); assertEquals(2, version.getMajorVersion());
} }
@Test public void @Test public void
mustResetToZeroMinorAndPatchVersionsWhenMajorVersionIsIncremented() { mustResetToZeroMinorAndPatchVersionsWhenMajorVersionIsIncremented() {
Version version = new Version("1.2.3"); Version version = new Version("1.2.3");
version.bumpMajor(); version.bumpMajorVersion();
assertEquals(2, version.getMajor()); assertEquals(2, version.getMajorVersion());
assertEquals(0, version.getMinor()); assertEquals(0, version.getMinorVersion());
assertEquals(0, version.getPatch()); assertEquals(0, version.getPatchVersion());
} }
@Test public void @Test public void
mustResetToZeroPatchVersionWhenMinorVersionIsIncremented() { mustResetToZeroPatchVersionWhenMinorVersionIsIncremented() {
Version version = new Version("1.2.3"); Version version = new Version("1.2.3");
version.bumpMinor(); version.bumpMinorVersion();
assertEquals(1, version.getMajor()); assertEquals(1, version.getMajorVersion());
assertEquals(3, version.getMinor()); assertEquals(3, version.getMinorVersion());
assertEquals(0, version.getPatch()); assertEquals(0, version.getPatchVersion());
} }
@Test public void @Test public void
mayHavePreReleaseVersionFollowingPatchVersionAppendedWithDash() { mayHavePreReleaseVersionFollowingPatchVersionAppendedWithDash() {
Version version = new Version("1.2.3-alpha"); Version version = new Version("1.2.3-alpha");
assertEquals("alpha", version.getPreRelease()); assertEquals("alpha", version.getPreReleaseVersion());
} }
@Test public void @Test public void
preReleaseVersionMustCompriseDotSeparatedIdentifiersOfAlphaNumericsAndDash() { preReleaseVersionMustCompriseDotSeparatedIdentifiersOfAlphaNumericsAndDash() {
Version version = new Version("1.0.0-x.7.z.92"); 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 @Test public void
mayHaveBuildVersionFollowingPatchOrPreReleaseVersionsAppendedWithPlus() { mayHaveBuildVersionFollowingPatchOrPreReleaseVersionsAppendedWithPlus() {
Version version = new Version("1.2.3+build"); Version version = new Version("1.2.3+build");
assertEquals("build", version.getBuild()); assertEquals("build", version.getBuildVersion());
} }
@Test public void @Test public void
buildVersionMustCompriseDotSeparatedIdentifiersOfAlphaNumericsAndDash() { buildVersionMustCompriseDotSeparatedIdentifiersOfAlphaNumericsAndDash() {
Version version = new Version("1.3.7+build.11.e0f985a"); 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 @Test public void