Skip to content

Commit

Permalink
bzlmod: Small cleanup for Version
Browse files Browse the repository at this point in the history
    (bazelbuild/bazel#13316)

    Turns out nobody needs to deal with a null version anyway, so I removed the null handling. And the unused Version#mustParse method.

    PiperOrigin-RevId: 384897795
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent 86de4b9 commit 7292a76
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,27 +153,16 @@ public static Version parse(String version) throws ParseException {
return new AutoValue_Version(releaseSplit.build(), prereleaseSplit.build(), version);
}

/** Same as {@link #parse} but throws an unchecked exception instead, useful for testing. */
public static Version mustParse(String version) {
try {
return parse(version);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}

private static final Comparator<Version> COMPARATOR =
Comparator.nullsFirst(
comparing(Version::isEmpty, falseFirst())
.thenComparing(
Version::getRelease, lexicographical(Comparator.<Integer>naturalOrder()))
.thenComparing(Version::isPrerelease, trueFirst())
.thenComparing(
Version::getPrerelease,
lexicographical(
comparing(Identifier::isDigitsOnly, trueFirst())
.thenComparingInt(Identifier::asNumber)
.thenComparing(Identifier::asString))));
comparing(Version::isEmpty, falseFirst())
.thenComparing(Version::getRelease, lexicographical(Comparator.<Integer>naturalOrder()))
.thenComparing(Version::isPrerelease, trueFirst())
.thenComparing(
Version::getPrerelease,
lexicographical(
comparing(Identifier::isDigitsOnly, trueFirst())
.thenComparingInt(Identifier::asNumber)
.thenComparing(Identifier::asString)));

@Override
public int compareTo(Version o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,12 @@ public class VersionTest {

@Test
public void testEmptyBeatsEverything() throws Exception {
assertThat(Version.parse("")).isGreaterThan(null);
assertThat(Version.parse("")).isGreaterThan(Version.parse("1.0"));
assertThat(Version.parse("")).isGreaterThan(Version.parse("1.0+build"));
assertThat(Version.parse("")).isGreaterThan(Version.parse("1.0-pre"));
assertThat(Version.parse("")).isGreaterThan(Version.parse("1.0-pre+build-kek.lol"));
}

@Test
public void testEverythingBeatsNull() throws Exception {
assertThat(Version.parse("1.0")).isGreaterThan(null);
assertThat(Version.parse("1.0+build")).isGreaterThan(null);
assertThat(Version.parse("1.0-pre")).isGreaterThan(null);
assertThat(Version.parse("1.0-pre+build-kek")).isGreaterThan(null);
}

@Test
public void testReleaseVersion() throws Exception {
assertThat(Version.parse("2.0")).isGreaterThan(Version.parse("1.0"));
Expand Down

0 comments on commit 7292a76

Please sign in to comment.