Skip to content

Commit

Permalink
JAVA-15621, Potential issue in "Guide to UUID in Java" article. (euge…
Browse files Browse the repository at this point in the history
…np#13271)

* JAVA-15621, Potential issue in "Guide to UUID in Java" article.

* JAVA-15621, Keep the raw strings in the article to make it easy for users to understand.

* JAVA-15621, Keep the raw strings in the article to make it easy for users to understand.

Co-authored-by: jogra <[email protected]>
  • Loading branch information
jsgrah-spring and jsgmiles authored Jan 16, 2023
1 parent 57975a0 commit e8ff2e3
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ public static UUID generateType1UUID() {

private static long get64LeastSignificantBitsForVersion1() {
final long random63BitLong = new Random().nextLong() & 0x3FFFFFFFFFFFFFFFL;
final long variant3BitFlag = 0x8000000000000000L;
return random63BitLong + variant3BitFlag;
long variant3BitFlag = 0x8000000000000000L;
return random63BitLong | variant3BitFlag;
}

private static long get64MostSignificantBitsForVersion1() {
final long timeForUuidIn100Nanos = System.currentTimeMillis();
final long time_low = (timeForUuidIn100Nanos & 0x0000_0000_FFFF_FFFFL) << 32;
final long time_mid = ((timeForUuidIn100Nanos >> 32) & 0xFFFF) << 16;
final long currentTimeMillis = System.currentTimeMillis();
final long time_low = (currentTimeMillis & 0x0000_0000_FFFF_FFFFL) << 32;
final long time_mid = ((currentTimeMillis >> 32) & 0xFFFF) << 16;
final long version = 1 << 12;
final long time_hi = ((timeForUuidIn100Nanos >> 48) & 0x0FFF);
return time_low + time_mid + version + time_hi;
final long time_high = ((currentTimeMillis >> 48) & 0x0FFF);
return time_low | time_mid | version | time_high;
}

/**
Expand Down

0 comments on commit e8ff2e3

Please sign in to comment.