This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 1bcc44764c83 camel-zookeeper: treat '9' as a digit in 
NaturalSortComparator (#29676)
1bcc44764c83 is described below

commit 1bcc44764c83eb0c55d82631018e2443d629145c
Author: dhairya05 <[email protected]>
AuthorDate: Thu Nov 12 17:32:35 2026 +0530

    camel-zookeeper: treat '9' as a digit in NaturalSortComparator (#29676)
    
    isDigit() excluded the character it was meant to include:
    
        return c >= 48 && c < 57;    // 57 is '9'
    
    so '9' was compared as an ordinary character instead of starting a number
    sequence. Any pair of strings whose digit runs begin with a 9 sorted by
    character value rather than numerically:
    
        before: [8, 10, 11, 100, 9, 90, camel-2.8.0, camel-2.10.0,
                 camel-2.11.0, camel-2.9.0, node-0000000009, node-0000000008,
                 node-0000000010]
        after:  [8, 9, 10, 11, 90, 100, camel-2.8.0, camel-2.9.0,
                 camel-2.10.0, camel-2.11.0, node-0000000008, node-0000000009,
                 node-0000000010]
    
    node-0000000009 sorting before node-0000000008 is the case that matters for
    this component, since ZooKeeper's sequential znode names are what the 
children
    consumer sorts with this comparator.
    
    Also give the Order constructor a default. It only assigned the field when 
the
    argument was non-null, so new NaturalSortComparator(null) left order null 
and
    every compare() threw a NullPointerException on order.direction. It now 
falls
    back to Ascending, which is what the no-argument constructor uses.
    
    The stale TODO about first being null is dropped: the three null checks at 
the
    top of compare() already return before that line.
---
 .../component/zookeeper/NaturalSortComparator.java |  7 ++--
 .../zookeeper/NaturalSortComparatorTest.java       | 39 +++++++++++++++++++++-
 2 files changed, 40 insertions(+), 6 deletions(-)

diff --git 
a/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/NaturalSortComparator.java
 
b/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/NaturalSortComparator.java
index d7fc092fe380..795f61dfc593 100644
--- 
a/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/NaturalSortComparator.java
+++ 
b/components/camel-zookeeper/src/main/java/org/apache/camel/component/zookeeper/NaturalSortComparator.java
@@ -48,9 +48,7 @@ public class NaturalSortComparator implements 
Comparator<CharSequence> {
     }
 
     public NaturalSortComparator(Order order) {
-        if (order != null) {
-            this.order = order;
-        }
+        this.order = order != null ? order : Order.Ascending;
     }
 
     @Override
@@ -68,7 +66,6 @@ public class NaturalSortComparator implements 
Comparator<CharSequence> {
         int compare = 0;
         int fx = 0;
         int sx = 0;
-        // TODO first can contain null and the next line causes NPE then
         while (fx < first.length() && sx < second.length() && compare == 0) {
             if (isDigit(first.charAt(fx)) && isDigit(second.charAt(sx))) {
                 int flen = getNumSequenceLength(first, fx);
@@ -96,7 +93,7 @@ public class NaturalSortComparator implements 
Comparator<CharSequence> {
     }
 
     private boolean isDigit(char c) {
-        return c >= 48 && c < 57;
+        return c >= '0' && c <= '9';
     }
 
     private int getNumSequenceLength(CharSequence sequence, int index) {
diff --git 
a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/NaturalSortComparatorTest.java
 
b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/NaturalSortComparatorTest.java
index aac0fe885ffe..df4c17de1784 100644
--- 
a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/NaturalSortComparatorTest.java
+++ 
b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/NaturalSortComparatorTest.java
@@ -33,7 +33,7 @@ public class NaturalSortComparatorTest {
 
         List<String> sorted = Arrays
                 .asList(new String[] {
-                        "0", "1", "3", "4.0", "11", "30", "55", "225", "333", 
"camel-2.1.0", "camel-2.1.1",
+                        "0", "1", "3", "4.0", "9", "11", "30", "55", "90", 
"225", "333", "camel-2.1.0", "camel-2.1.1",
                         "camel-2.1.1-SNAPSHOT", "camel-2.2.0" });
 
         List<String> unsorted = new ArrayList<>(sorted);
@@ -47,6 +47,43 @@ public class NaturalSortComparatorTest {
         compareLists(sorted, unsorted);
     }
 
+    @Test
+    public void testSortOrderAroundNine() {
+
+        List<String> sorted = Arrays
+                .asList(new String[] {
+                        "camel-2.8.0", "camel-2.9.0", "camel-2.10.0", 
"camel-2.11.0" });
+
+        List<String> unsorted = new ArrayList<>(sorted);
+        Collections.shuffle(unsorted);
+        Collections.sort(unsorted, new NaturalSortComparator());
+        compareLists(sorted, unsorted);
+    }
+
+    @Test
+    public void testSortOrderOfSequentialNodeNames() {
+
+        List<String> sorted = Arrays
+                .asList(new String[] {
+                        "node-0000000008", "node-0000000009", 
"node-0000000010" });
+
+        List<String> unsorted = new ArrayList<>(sorted);
+        Collections.shuffle(unsorted);
+        Collections.sort(unsorted, new NaturalSortComparator());
+        compareLists(sorted, unsorted);
+    }
+
+    @Test
+    public void testNullOrderSortsAscending() {
+
+        List<String> sorted = Arrays.asList(new String[] { "1", "2", "10" });
+
+        List<String> unsorted = new ArrayList<>(sorted);
+        Collections.shuffle(unsorted);
+        Collections.sort(unsorted, new NaturalSortComparator(null));
+        compareLists(sorted, unsorted);
+    }
+
     private void compareLists(List<String> sorted, List<String> unsorted) {
         for (int x = 0; x < unsorted.size(); x++) {
             assertEquals(sorted.get(x), unsorted.get(x));

Reply via email to