-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
None
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
A UUID can be thought of as byte array of length 16.
Comparing two UUIDs for order should be the same as comparing the two byte arrays, comparing each element as if it was an unsigned number 0-255.
This is what databases do when they compare columns of type UUID.
I also suspect that this is what most other languages do. (e.g. Python, .NET, Golang, etc)
But JDK is different !
In the UUID.compareTo() method the comparison is done based on comparing two SIGNED longs.
History: This was first acknowledged inJDK-7025832 but was rejected to change the standard compareTo() method due to concerns over backward compatibility.
Later, it has been raised in JDK-8351406.
Suggest to add a static method to the UUID class, which would be:
public static int compareUnsigned(UUID uuid1, UUID uuid2) {
UUID u1 = uuid1 != null ? uuid1 : new UUID(0L, 0L);
UUID u2 = uuid2 != null ? uuid2 : new UUID(0L, 0L);
int mostSigBits = Long.compareUnsigned(u1.getMostSignificantBits(), u2.getMostSignificantBits());
return mostSigBits != 0 ? mostSigBits : Long.compareUnsigned(u1.getLeastSignificantBits(), u2.getLeastSignificantBits());
}
A UUID can be thought of as byte array of length 16.
Comparing two UUIDs for order should be the same as comparing the two byte arrays, comparing each element as if it was an unsigned number 0-255.
This is what databases do when they compare columns of type UUID.
I also suspect that this is what most other languages do. (e.g. Python, .NET, Golang, etc)
But JDK is different !
In the UUID.compareTo() method the comparison is done based on comparing two SIGNED longs.
History: This was first acknowledged in
Later, it has been raised in JDK-8351406.
Suggest to add a static method to the UUID class, which would be:
public static int compareUnsigned(UUID uuid1, UUID uuid2) {
UUID u1 = uuid1 != null ? uuid1 : new UUID(0L, 0L);
UUID u2 = uuid2 != null ? uuid2 : new UUID(0L, 0L);
int mostSigBits = Long.compareUnsigned(u1.getMostSignificantBits(), u2.getMostSignificantBits());
return mostSigBits != 0 ? mostSigBits : Long.compareUnsigned(u1.getLeastSignificantBits(), u2.getLeastSignificantBits());
}
- relates to
-
JDK-7025832 java.lang.UUID compareTo() does not do an unsigned compare
-
- Closed
-
-
JDK-8351406 Allow UUID proper comparison
-
- Open
-
-
JDK-8334015 Add Support for UUID Version 7 (UUIDv7) defined in RFC 9562
-
- Open
-