-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUUID.java
More file actions
51 lines (41 loc) · 1.78 KB
/
UUID.java
File metadata and controls
51 lines (41 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.security.SecureRandom;
public final class UUID {
private static long lastTime = Long.MIN_VALUE;
// use a short instead of a byte to work around Java's lack of unsigned types.
private static short sequenceCounter = 0;
private static long versionIdentifier = 0xC000000000000000L;
private static long versionAndNodeIdentifier;
static {
// generate a random number for the node identifier.
SecureRandom random = new SecureRandom();
long nodeIdentifier = (random.nextLong() & 0x0000FFFFFFFFFFFFL) << 8;
versionAndNodeIdentifier = versionIdentifier | nodeIdentifier;
}
public static synchronized java.util.UUID next() {
// just pad out to microseconds for now.
long time = System.currentTimeMillis() * 1000;
// handle the clock moving backwards.
if (time < lastTime) time = lastTime;
// handle multiple ids generated "simultaneously".
if (time == lastTime) {
if (sequenceCounter == 256) {
// rather than block, we'll cheat and return a UUID from the very near future.
lastTime = ++time;
sequenceCounter = 0;
} else {
sequenceCounter++;
}
} else {
lastTime = time;
sequenceCounter = 0;
}
return new java.util.UUID(time, versionAndNodeIdentifier | (sequenceCounter & 0xFF));
}
// helper functions for comparing a UUID to a time range.
public static java.util.UUID from(long time) {
return new java.util.UUID(time, versionIdentifier);
}
public static java.util.UUID until(long time) {
return new java.util.UUID(time, versionIdentifier | 0x00FFFFFFFFFFFFFFL);
}
}