From f1e4a084d18cf14bff0a3bfabfb04ee3ccbfe8e8 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Tue, 25 Jun 2024 10:22:42 +0100 Subject: [PATCH] JAL-3631 Forgot to add this file for 'Change parity of comparison for convention' commit --- .../src/main/java/jalview/util/LaunchUtils.java | 69 ++++++++++++-------- 1 file changed, 42 insertions(+), 27 deletions(-) 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) -- 1.7.10.2