Reputation: 129
I'm not sure as to how I'd go about comparing two version strings, and include support for "SNAPSHOT" in the version name.
An example: I have two version strings, "1.9.9" and "2.0.0". I want to return a boolean (true) if the second string is greater than the first string. I've used this: https://stackoverflow.com/a/6702029/1442718
It works perfectly.
However, an example like this won't work with that code: I have two version strings, "2.0.0-SNAPSHOT" and "2.0.0". Obviously the SNAPSHOT is one version behind 2.0.0, but this method won't pick it up and throw a NumberFormatException, instead of returning -1 which implies it is a version below.
Any suggestions? Thanks.
Upvotes: 0
Views: 220
Reputation: 20185
You could use something like this:
[...]
boolean isSnapshot1 = str1.endsWith("SNAPSHOT");
boolean isSnapshot2 = str2.endsWith("SNAPSHOT");
[...]
if (...) { // if version-number are equal
if (isSnapshot1 && !isSnapshot2) { // same versions, but first is snapshot
return (1);
}
if { // same versions, but secont is snapshot
return (-1);
}
// else { Neither or both are snapshots, versions are equals -> cannot distinguish their order
return (0);
// }
}
[...]
This should to the job.
Upvotes: 1