elharo commented on code in PR #179:
URL: 
https://github.com/apache/maven-toolchains-plugin/pull/179#discussion_r3631154588


##########
src/main/java/org/apache/maven/plugins/toolchain/jdk/ToolchainDiscoverer.java:
##########
@@ -370,24 +370,52 @@ Comparator<ToolchainModel> version() {
                     String[] b = v2.split("\\.");
                     int length = Math.min(a.length, b.length);
                     for (int i = 0; i < length; i++) {
-                        String oa = a[i];
-                        String ob = b[i];
-                        if (!Objects.equals(oa, ob)) {
-                            // A null element is less than a non-null element
-                            if (oa == null || ob == null) {
-                                return oa == null ? -1 : 1;
-                            }
-                            int v = oa.compareTo(ob);
-                            if (v != 0) {
-                                return v;
-                            }
+                        int cmp = compareVersionSegments(a[i], b[i]);
+                        if (cmp != 0) {
+                            return cmp;
                         }
                     }
-                    return a.length - b.length;
+                    return Integer.compare(a.length, b.length);
                 })
                 .reversed();
     }
 
+    private static int compareVersionSegments(String sa, String sb) {
+        int na = parseVersionSegment(sa);
+        int nb = parseVersionSegment(sb);
+        if (na != nb) {
+            return Integer.compare(na, nb);
+        }
+        boolean suffixA = hasSuffix(sa);
+        boolean suffixB = hasSuffix(sb);
+        if (suffixA != suffixB) {
+            return suffixA ? -1 : 1;
+        }
+        return 0;
+    }
+
+    private static boolean hasSuffix(String s) {
+        for (int i = 0; i < s.length(); i++) {
+            char c = s.charAt(i);
+            if (c < '0' || c > '9') {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static int parseVersionSegment(String s) {
+        int n = 0;
+        for (int i = 0; i < s.length(); i++) {
+            char c = s.charAt(i);
+            if (c < '0' || c > '9') {
+                break;
+            }
+            n = n * 10 + (c - '0');
+        }
+        return n;
+    }

Review Comment:
   @copilot done



##########
src/main/java/org/apache/maven/plugins/toolchain/jdk/ToolchainDiscoverer.java:
##########
@@ -370,24 +370,52 @@ Comparator<ToolchainModel> version() {
                     String[] b = v2.split("\\.");
                     int length = Math.min(a.length, b.length);
                     for (int i = 0; i < length; i++) {
-                        String oa = a[i];
-                        String ob = b[i];
-                        if (!Objects.equals(oa, ob)) {
-                            // A null element is less than a non-null element
-                            if (oa == null || ob == null) {
-                                return oa == null ? -1 : 1;
-                            }
-                            int v = oa.compareTo(ob);
-                            if (v != 0) {
-                                return v;
-                            }
+                        int cmp = compareVersionSegments(a[i], b[i]);
+                        if (cmp != 0) {
+                            return cmp;
                         }
                     }
-                    return a.length - b.length;
+                    return Integer.compare(a.length, b.length);
                 })
                 .reversed();
     }
 
+    private static int compareVersionSegments(String sa, String sb) {
+        int na = parseVersionSegment(sa);
+        int nb = parseVersionSegment(sb);
+        if (na != nb) {
+            return Integer.compare(na, nb);
+        }
+        boolean suffixA = hasSuffix(sa);
+        boolean suffixB = hasSuffix(sb);
+        if (suffixA != suffixB) {
+            return suffixA ? -1 : 1;
+        }
+        return 0;
+    }

Review Comment:
   @copilot done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to