From: Ben Soares Date: Tue, 25 Jun 2024 09:22:42 +0000 (+0100) Subject: JAL-3631 Forgot to add this file for 'Change parity of comparison for convention... X-Git-Tag: Release_2_11_4_0~24^2~18 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=f1e4a084d18cf14bff0a3bfabfb04ee3ccbfe8e8;p=jalview.git JAL-3631 Forgot to add this file for 'Change parity of comparison for convention' commit --- diff --git a/getdown/src/getdown/core/src/main/java/jalview/util/LaunchUtils.java b/getdown/src/getdown/core/src/main/java/jalview/util/LaunchUtils.java index dc8961f..58910a8 100644 --- a/getdown/src/getdown/core/src/main/java/jalview/util/LaunchUtils.java +++ b/getdown/src/getdown/core/src/main/java/jalview/util/LaunchUtils.java @@ -693,29 +693,35 @@ public class LaunchUtils } if (!f0.exists()) { - return 1; + return -1; } if (!f1.exists()) { - return -1; + return 1; } String v0 = getJarImplementationVersion(f0); String v1 = getJarImplementationVersion(f1); syserr(true, false, "Got launcher versions '" + v0 + "' and '" + v1 + "'"); + return compareGetdownLauncherJarVersions(v0, v1); + } + + public static int compareGetdownLauncherJarVersions(String v0, String v1) + { if (v0 == null && v1 == null) { return 0; } if (v0 == null) { - return 1; + return -1; } if (v1 == null) { - return -1; + return 1; } - // now for the fun + + // remove the subscript if (v0.endsWith("JVL")) { v0 = v0.substring(0, v0.lastIndexOf('_')); @@ -731,30 +737,23 @@ public class LaunchUtils int compare = 0; for (int j = 0; j < Math.min(v0parts.length, v1parts.length); j++) { - compare = 0; - try - { - compare = compareVersions(v0parts[j], v1parts[j]); - } catch (NumberFormatException e) - { - syserr(true, false, - "Problem parsing one of the getdown launcher version numbers: '" - + v0 + "', '" + v1 + "'"); - } - - if (compare != 0 - || (v0parts.length == j + 1 && v1parts.length == j + 1)) + compare = compareVersions(v0parts[j], v1parts[j]); + if (compare != 0) { return compare; } } - return v1parts.length - v0parts.length; + return v0parts.length - v1parts.length; } - // just comparing 1.2.3.4...n + /** + * comparing versions numbers of the form 1.2.3.4...n ONLY returns 0 if v0 and + * v1 are the same, a negative number if v0 < v1 and a positive number if v0 > + * v1. The number returned does NOT show how far apart the version numbers + * are. + */ public static int compareVersions(String v0, String v1) - throws NumberFormatException { if (v0 == null && v1 == null) { @@ -762,24 +761,40 @@ public class LaunchUtils } if (v0 == null) { - return 1; + return -1; } if (v1 == null) { - return -1; + return 1; } String[] v0dots = v0.split("\\."); String[] v1dots = v1.split("\\."); + int compare = 0; for (int i = 0; i < Math.min(v0dots.length, v1dots.length); i++) { - if (!v0dots[i].equals(v1dots[i])) + if (!v0dots[i].equals(v1dots[i])) // avoids unnecessary + // NumberFormatException { - return Integer.parseInt(v1dots[i]) - Integer.parseInt(v0dots[i]); + try + { + compare = Integer.parseInt(v0dots[i]) + - Integer.parseInt(v1dots[i]); + } catch (NumberFormatException e) + { + syserr(true, false, "Couldn't parse one of '" + v0dots[i] + + "' or '" + v1dots[i] + "': " + e.getMessage()); + syserr(true, false, "Comparing as strings."); + compare = v0dots[i].compareTo(v1dots[i]); + } + if (compare != 0) + { + return compare; + } } } // all numbers match up to min length. If one has more dots, assume it's - // bigger. - return v1dots.length - v0dots.length; + // a greater version (e.g. 1.3.2 > 1.3) + return v0dots.length - v1dots.length; } public static String getJarImplementationVersion(File jarFile)