Skip to content

Commit 097cbb4

Browse files
committed
VersionUtils.compare: handle SemVer suffixes
With SemVer, 2.0.0 is newer than 2.0.0-beta-1. We were behaving in the opposite way. In other words: we want empty post-numeric suffixes to sort _after_ non-empty ones. So we need a special test for empty string here.
1 parent 423fa17 commit 097cbb4

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/main/java/org/scijava/util/VersionUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ private static int compareToken(final String t1, final String t2) {
156156
suffix1 = t1.substring(i1);
157157
suffix2 = t2.substring(i2);
158158
}
159+
160+
// Final version (empty string) is larger than non-final (non-empty).
161+
// For example: 2.0.0 > 2.0.0-beta-1.
162+
if (suffix1.isEmpty() && suffix2.isEmpty()) return 0;
163+
if (suffix1.isEmpty()) return 1;
164+
if (suffix2.isEmpty()) return -1;
165+
159166
// Compare lexicographically.
160167
return suffix1.compareTo(suffix2);
161168
}

src/test/java/org/scijava/util/VersionUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void testCompare() {
6060
assertTrue(VersionUtils.compare("2.7.3", "1.7.3") > 0);
6161

6262
// Suffix indicates version is older than final release.
63-
assertTrue(VersionUtils.compare("1.5.2", "1.5.2-beta-1") < 0);
63+
assertTrue(VersionUtils.compare("1.5.2", "1.5.2-beta-1") > 0);
6464

6565
// Check when number of version tokens does not match.
6666
assertTrue(VersionUtils.compare("1.5", "1.5.1") < 0);

0 commit comments

Comments
 (0)