From 008ac38ebcc4b3e370b299cf14fe894d5361bb04 Mon Sep 17 00:00:00 2001 From: fo40225 Date: Fri, 15 Nov 2024 09:26:50 +0800 Subject: [PATCH] fix java.lang.IllegalArgumentException: Comparison method violates its general contract! (#6239) --- .../java/seaweedfs/client/ReadChunks.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/other/java/client/src/main/java/seaweedfs/client/ReadChunks.java b/other/java/client/src/main/java/seaweedfs/client/ReadChunks.java index e563c0ccc..18826dd48 100644 --- a/other/java/client/src/main/java/seaweedfs/client/ReadChunks.java +++ b/other/java/client/src/main/java/seaweedfs/client/ReadChunks.java @@ -14,20 +14,23 @@ public class ReadChunks { points.add(new Point(chunk.getOffset(), chunk, true)); points.add(new Point(chunk.getOffset() + chunk.getSize(), chunk, false)); } + Collections.sort(points, new Comparator() { @Override public int compare(Point a, Point b) { - int x = (int) (a.x - b.x); - if (a.x != b.x) { - return (int) (a.x - b.x); + int xComparison = Long.compare(a.x, b.x); + if (xComparison != 0) { + return xComparison; } - if (a.ts != b.ts) { - return (int) (a.ts - b.ts); + + // If x values are equal, compare ts + int tsComparison = Long.compare(a.ts, b.ts); + if (tsComparison != 0) { + return tsComparison; } - if (!a.isStart) { - return -1; - } - return 1; + + // If both x and ts are equal, prioritize start points + return Boolean.compare(b.isStart, a.isStart); // b.isStart first to prioritize starts } });