Skip to content

Commit f209543

Browse files
Copilotsimbo1905
andcommitted
Add UUIDGeneratorDemo showing usage examples
Co-authored-by: simbo1905 <[email protected]>
1 parent 328e11c commit f209543

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package jdk.sandbox.demo;
2+
3+
import jdk.sandbox.java.util.json.UUIDGenerator;
4+
5+
import java.util.UUID;
6+
7+
/// Demonstrates usage of {@link UUIDGenerator} with both UUIDv7 and unique-then-time modes.
8+
///
9+
/// This demo shows:
10+
/// - Default UUIDv7 generation
11+
/// - Direct UUIDv7 creation with specific timestamps
12+
/// - Unique-then-time UUID generation
13+
/// - Configuration mode detection
14+
public final class UUIDGeneratorDemo {
15+
16+
public static void main(final String[] args) {
17+
System.out.println("=== UUID Generator Demo ===\n");
18+
19+
// Show current configuration
20+
final UUIDGenerator.Mode mode = UUIDGenerator.getConfiguredMode();
21+
System.out.println("Configured mode: " + mode);
22+
System.out.println("System property: " + System.getProperty(UUIDGenerator.MODE_PROPERTY, "(not set)"));
23+
System.out.println();
24+
25+
// Generate UUIDs using the configured mode
26+
System.out.println("--- Generating UUIDs with configured mode ---");
27+
for (int i = 0; i < 5; i++) {
28+
final UUID uuid = UUIDGenerator.generateUUID();
29+
System.out.println("UUID " + (i + 1) + ": " + uuid);
30+
if (mode == UUIDGenerator.Mode.V7) {
31+
System.out.println(" Version: " + uuid.version() + ", Variant: " + uuid.variant());
32+
}
33+
}
34+
System.out.println();
35+
36+
// Demonstrate UUIDv7 with specific timestamps
37+
System.out.println("--- UUIDv7 with specific timestamps ---");
38+
final long baseTime = System.currentTimeMillis();
39+
for (int i = 0; i < 3; i++) {
40+
final long timestamp = baseTime + (i * 1000); // 1 second apart
41+
final UUID uuid = UUIDGenerator.ofEpochMillis(timestamp);
42+
System.out.println("Timestamp: " + timestamp + " -> " + uuid);
43+
System.out.println(" Version: " + uuid.version() + ", Variant: " + uuid.variant());
44+
}
45+
System.out.println();
46+
47+
// Demonstrate unique-then-time mode
48+
System.out.println("--- Unique-then-time mode ---");
49+
for (int i = 0; i < 3; i++) {
50+
final long uniqueMsb = 0x1000000000000000L + i;
51+
final UUID uuid = UUIDGenerator.uniqueThenTime(uniqueMsb);
52+
System.out.println("Unique MSB: " + Long.toHexString(uniqueMsb) + " -> " + uuid);
53+
}
54+
System.out.println();
55+
56+
// Demonstrate monotonicity of UUIDv7
57+
System.out.println("--- UUIDv7 Monotonicity (time-ordered) ---");
58+
UUID previous = null;
59+
for (int i = 0; i < 5; i++) {
60+
final long timestamp = baseTime + (i * 100); // 100ms apart
61+
final UUID current = UUIDGenerator.ofEpochMillis(timestamp);
62+
if (previous != null) {
63+
final int comparison = current.compareTo(previous);
64+
System.out.println(current + " > " + previous + " ? " + (comparison > 0));
65+
} else {
66+
System.out.println(current + " (first)");
67+
}
68+
previous = current;
69+
}
70+
System.out.println();
71+
72+
// Show configuration examples
73+
System.out.println("=== Configuration Examples ===");
74+
System.out.println("To use UUIDv7 (default):");
75+
System.out.println(" java -jar app.jar");
76+
System.out.println(" or");
77+
System.out.println(" java -D" + UUIDGenerator.MODE_PROPERTY + "=v7 -jar app.jar");
78+
System.out.println();
79+
System.out.println("To use unique-then-time mode:");
80+
System.out.println(" java -D" + UUIDGenerator.MODE_PROPERTY + "=unique-then-time -jar app.jar");
81+
System.out.println();
82+
System.out.println("On Android, set in Application.onCreate():");
83+
System.out.println(" System.setProperty(\"" + UUIDGenerator.MODE_PROPERTY + "\", \"v7\");");
84+
System.out.println(" Note: Must be set before first UUIDGenerator access");
85+
}
86+
}

0 commit comments

Comments
 (0)