Skip to content

Commit 2068998

Browse files
committed
comparing Coords: do not consider comment
Version: refactor
1 parent 11aaf86 commit 2068998

File tree

4 files changed

+32
-35
lines changed

4 files changed

+32
-35
lines changed

src/main/java/org/scm4j/commons/Coords.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ public class Coords {
1010
private final String groupId;
1111
private final String classifier;
1212
private final Version version;
13-
private final String coordsString;
13+
private final String coordsStringNoComment;
1414

1515
public String getComment() {
1616
return commentStr;
1717
}
1818

1919
public Coords(String coordsString) {
20-
this.coordsString = coordsString;
2120
String str = coordsString;
22-
2321
// Comment
2422
{
2523
Integer pos = coordsString.indexOf("#");
@@ -28,8 +26,10 @@ public Coords(String coordsString) {
2826
// add spaces between valuable chars and # to comment
2927
commentStr = StringUtils.difference(str.substring(0, pos).trim(), str.substring(0, pos)) + str.substring(pos);
3028
str = str.substring(0, pos).trim();
29+
this.coordsStringNoComment = str;
3130
} else {
3231
commentStr = "";
32+
this.coordsStringNoComment = coordsString;
3333
}
3434
}
3535

@@ -61,7 +61,7 @@ public Coords(String coordsString) {
6161
public int hashCode() {
6262
final int prime = 31;
6363
int result = 1;
64-
result = prime * result + ((coordsString == null) ? 0 : coordsString.hashCode());
64+
result = prime * result + ((coordsStringNoComment == null) ? 0 : coordsStringNoComment.hashCode());
6565
return result;
6666
}
6767

@@ -74,10 +74,10 @@ public boolean equals(Object obj) {
7474
if (getClass() != obj.getClass())
7575
return false;
7676
Coords other = (Coords) obj;
77-
if (coordsString == null) {
78-
if (other.coordsString != null)
77+
if (coordsStringNoComment == null) {
78+
if (other.coordsStringNoComment != null)
7979
return false;
80-
} else if (!coordsString.equals(other.coordsString))
80+
} else if (!coordsStringNoComment.equals(other.coordsStringNoComment))
8181
return false;
8282
return true;
8383
}

src/main/java/org/scm4j/commons/Version.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,16 @@ public String toSnapshotString() {
151151
public Version toSnapshot() {
152152
return new Version(toSnapshotString());
153153
}
154+
155+
public Version toRelease() {
156+
if (!isSemantic) {
157+
return this;
158+
}
159+
return new Version(prefix + minor + (patch.isEmpty() ? "" : ".0"));
160+
}
154161

155162
public String toReleaseString() {
156-
if (!StringUtils.isNumeric(minor)) {
157-
return verStr;
158-
}
159-
return prefix + minor + (patch.isEmpty() ? "" : "." + patch);
163+
return toRelease().toString();
160164
}
161165

162166
public Boolean isGreaterThan(Version other) {
@@ -198,13 +202,6 @@ public String getReleaseNoPatchString() {
198202
return prefix + minor;
199203
}
200204

201-
public Version toRelease() {
202-
if (!isSemantic) {
203-
return this;
204-
}
205-
return new Version(prefix + minor + (patch.isEmpty() ? "" : "." + patch));
206-
}
207-
208205
public Version setMinor(String minor) {
209206
if (!isSemantic) {
210207
throw new IllegalStateException("can not set minor for non-semantic version");

src/test/java/org/scm4j/commons/CoordsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void testVersion() {
8080
public void testEqualsAndHashCode() {
8181
EqualsVerifier
8282
.forClass(Coords.class)
83-
.withOnlyTheseFields("coordsString")
83+
.withOnlyTheseFields("coordsStringNoComment")
8484
.usingGetClass()
8585
.verify();
8686
}

src/test/java/org/scm4j/commons/VersionTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public void testToString() {
3131

3232
@Test
3333
public void testToReleaseString() {
34-
assertEquals(new Version("11.21.31.41-SNAPSHOT").toReleaseString(), "11.21.31.41");
35-
assertEquals(new Version("11.21.31.41").toReleaseString(), "11.21.31.41");
36-
assertEquals(new Version("11.21.31").toReleaseString(), "11.21.31");
37-
assertEquals(new Version("11.21").toReleaseString(), "11.21");
38-
assertEquals(new Version("11-SNAPSHOT").toReleaseString(), "11");
39-
assertEquals(new Version("-SNAPSHOT").toReleaseString(), "-SNAPSHOT");
34+
assertEquals("11.21.31.0", new Version("11.21.31.41-SNAPSHOT").toReleaseString());
35+
assertEquals("11.21.31.0", new Version("11.21.31.41").toReleaseString());
36+
assertEquals("11.21.0", new Version("11.21.31").toReleaseString());
37+
assertEquals("11.0", new Version("11.21").toReleaseString());
38+
assertEquals("11", new Version("11-SNAPSHOT").toReleaseString());
39+
assertEquals("-SNAPSHOT", new Version("-SNAPSHOT").toReleaseString());
4040

4141
}
4242

@@ -50,12 +50,12 @@ public void testSnapshot() {
5050

5151
@Test
5252
public void testMinorBumping() {
53-
assertEquals(new Version("11.21.31.41").toPreviousMinor().toReleaseString(), "11.21.30.41");
54-
assertEquals(new Version("11.21.31.41-SNAPSHOT").toPreviousMinor().toReleaseString(), "11.21.30.41");
55-
assertEquals(new Version("11.21.31.41").toNextMinor().toReleaseString(), "11.21.32.41");
56-
assertEquals(new Version("11.21.31.41-SNAPSHOT").toNextMinor().toReleaseString(), "11.21.32.41");
57-
assertEquals(new Version("11.21.31.41").toNextMinor().toReleaseString(), "11.21.32.41");
58-
assertEquals(new Version("11.21.31.41-SNAPSHOT").toNextMinor().toString(), "11.21.32.41-SNAPSHOT");
53+
assertEquals("11.21.30.0", new Version("11.21.31.41").toPreviousMinor().toReleaseString());
54+
assertEquals("11.21.30.0", new Version("11.21.31.41-SNAPSHOT").toPreviousMinor().toReleaseString());
55+
assertEquals("11.21.32.0", new Version("11.21.31.41").toNextMinor().toReleaseString());
56+
assertEquals("11.21.32.0", new Version("11.21.31.41-SNAPSHOT").toNextMinor().toReleaseString());
57+
assertEquals("11.21.32.0", new Version("11.21.31.41").toNextMinor().toReleaseString());
58+
assertEquals("11.21.32.41-SNAPSHOT", new Version("11.21.31.41-SNAPSHOT").toNextMinor().toString());
5959
Version version = new Version("");
6060
try {
6161
version.toNextMinor();
@@ -151,10 +151,10 @@ public void testGetReleaseNoPatchString() {
151151

152152
@Test
153153
public void testToRelease() {
154-
assertEquals(new Version("11.21.31.41"), new Version("11.21.31.41-SNAPSHOT").toRelease());
155-
assertEquals(new Version("11.21.31.41"), new Version("11.21.31.41").toRelease());
156-
assertEquals(new Version("11.21.31"), new Version("11.21.31").toRelease());
157-
assertEquals(new Version("11.21"), new Version("11.21").toRelease());
154+
assertEquals(new Version("11.21.31.0"), new Version("11.21.31.41-SNAPSHOT").toRelease());
155+
assertEquals(new Version("11.21.31.0"), new Version("11.21.31.41").toRelease());
156+
assertEquals(new Version("11.21.0"), new Version("11.21.31").toRelease());
157+
assertEquals(new Version("11.0"), new Version("11.21").toRelease());
158158
assertEquals(new Version("11"), new Version("11-SNAPSHOT").toRelease());
159159
assertEquals(new Version("-SNAPSHOT"), new Version("-SNAPSHOT").toRelease());
160160
}

0 commit comments

Comments
 (0)