Skip to content

Commit

Permalink
Making the version checking more robust to beta suffixes and non-nume…
Browse files Browse the repository at this point in the history
…rical fields
  • Loading branch information
rambaut committed Jul 15, 2024
1 parent 0ec11e9 commit 2b4f798
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/dr/util/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,46 @@ public interface Version {

String getVersion();

String getVersionString();
String getVersionString();

String getBuildString();
String getBuildString();

String getDateString();
String getDateString();

String[] getCredits();

String getHTMLCredits();

class Utils {
private static String[] splitVersion(String version) {
if (version.contains("-")) {
// in case it has a -beta or similar suffix
version = version.substring(0, version.indexOf("-"));
}
return version.trim().split("\\.");
}

/**
* Is version1 more recent (higher) than version2?
* @param version1
* @param version2
* @return
*/
public static boolean isMoreRecent(String version1, String version2) {
String[] v1 = version1.split("\\.");
String[] v2 = version2.split("\\.");
String[] v1 = splitVersion(version1);
String[] v2 = splitVersion(version2);

for (int i = 0; i < Math.min(v1.length, v2.length); i++) {
if (Integer.parseInt(v1[i]) < Integer.parseInt(v2[i])) {
return false;
} else if (Integer.parseInt(v1[i]) > Integer.parseInt(v2[i])) {
return true;
try {
for (int i = 0; i < Math.min(v1.length, v2.length); i++) {
if (Integer.parseInt(v1[i]) < Integer.parseInt(v2[i])) {
return false;
} else if (Integer.parseInt(v1[i]) > Integer.parseInt(v2[i])) {
return true;
}
}
} catch (NumberFormatException nfe) {
// if the numbers can't be parsed just allow the parsing to proceed.
return false;
}

return false;
Expand Down

0 comments on commit 2b4f798

Please sign in to comment.