Нарушает ли сравнение UUID на Java стандарт UUID?JAVA

Программисты JAVA общаются здесь
Anonymous
Нарушает ли сравнение UUID на Java стандарт UUID?

Сообщение Anonymous »

Согласно стандартному RFC 4122 UUID, UUID следует рассматривать как Unsigned int 128, и сравнение должно быть проведено таким образом: < /p>

Код: Выделить всё

Rules for Lexical Equivalence:
Consider each field of the UUID to be an unsigned integer as shown
in the table in section Section 4.1.2.  Then, to compare a pair of
UUIDs, arithmetically compare the corresponding fields from each
UUID in order of significance and according to their data type.
Two UUIDs are equal if and only if all the corresponding fields
are equal.
< /code>
 UUIDs, as defined in this document, can also be ordered
lexicographically.  For a pair of UUIDs, the first one follows the
second if the most significant field in which the UUIDs differ is
greater for the first UUID.  The second precedes the first if the
most significant field in which the UUIDs differ is greater for
the second UUID.
< /code>
Meaning that  println(UUID.fromString("b533260f-6479-4014-a007-818481bd98c6") < UUID.fromString("131f0ada-6b6a-4e75-a6a0-4149958664e3"))
должен распечатать false.
Однако он печатает true!@Override
public int compareTo(UUID val) {
// The ordering is intentionally set up so that the UUIDs
// can simply be numerically compared as two numbers
int mostSigBits = Long.compare(this.mostSigBits, val.mostSigBits);
return mostSigBits != 0 ? mostSigBits : Long.compare(this.leastSigBits, val.leastSigBits);
}

< /code>
For this particular case, the most significant bits are
-5389922481480318956
1377831944219938421
< /code>
respectively. Meaning that comparison is wrong because the long overflows.

Подробнее здесь: https://stackoverflow.com/questions/794 ... d-standard

Вернуться в «JAVA»