|
2 | 2 |
|
3 | 3 | [](https://central.sonatype.com/artifact/io.github.honhimw/uuid-java) |
4 | 4 |
|
5 | | -## Usage |
6 | | - |
7 | 5 | ```groovy |
8 | 6 | implementation 'io.github.honhimw:uuid-java:{latest}' |
9 | 7 | ``` |
10 | 8 |
|
11 | | -```java |
12 | | -import io.github.honhimw.uuid.*; |
13 | | -import io.github.honhimw.uuid.gen.*; |
| 9 | +--- |
14 | 10 |
|
15 | | -void main() { |
16 | | - // Using via global shared instances. |
17 | | - Generator generator = UUIDs |
18 | | - .FAST // FAST | SECURE default configuration |
19 | | - .V7; // V1 | V3 | V4 | V5 | V6 | V7 |
20 | | - UUID uuid = generator.next(); // Generation |
| 11 | +## Benchmark |
21 | 12 |
|
22 | | - // Customize generator |
23 | | - V7 v7 = new V7(Context.builder() |
24 | | - .clock( |
25 | | - new V7.ClockSequenceV7() // Reseeding timestamp context |
26 | | - .withAdditionalPrecision() // Add 12-bits for timestamp-precision |
27 | | - ) |
28 | | - .random(() -> ThreadLocalRandom.current()) // For fast. `new SecureRandom()` for secure |
29 | | - .node(() -> MacAddress.nodeId()) // Using other by specificizing NodeId.of(new byte[] {1, 2, 3, 4, 5, 6}) |
30 | | - .messageDigest(algorithm -> MessageDigest.getInstance(algorithm)) // MessageDigest provider(getting from thread-local cache?) |
31 | | - .build()); |
32 | | -} |
| 13 | +```shell |
| 14 | +./gradlew bench |
33 | 15 | ``` |
34 | 16 |
|
35 | | -## Benchmark |
36 | | - |
37 | 17 | <details> |
38 | 18 | <summary>Details</summary> |
39 | 19 |
|
@@ -69,3 +49,60 @@ void main() { |
69 | 49 |
|
70 | 50 | </details> |
71 | 51 |
|
| 52 | +--- |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +```java |
| 57 | +import io.github.honhimw.uuid.*; |
| 58 | +import io.github.honhimw.uuid.gen.*; |
| 59 | + |
| 60 | +void main() { |
| 61 | + // Using via global shared instances. |
| 62 | + Generator generator = UUIDs |
| 63 | + .FAST // FAST | SECURE default configuration |
| 64 | + .V7; // V1 | V3 | V4 | V5 | V6 | V7 |
| 65 | + UUID uuid = generator.next(); // Generation |
| 66 | +// UUID uuid = UUIDs.FAST.V1.now(MacAddress.nodeId()); |
| 67 | +// UUID uuid = UUIDs.FAST.V3.of("foo"); |
| 68 | +// UUID uuid = UUIDs.FAST.V4.next(); |
| 69 | +// UUID uuid = UUIDs.FAST.V5.of("bar"); |
| 70 | +// UUID uuid = UUIDs.FAST.V6.now(MacAddress.nodeId()); |
| 71 | +// UUID uuid = UUIDs.FAST.V7.of(Timestamp.now(CounterSequence.SHARED)); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Customize |
| 76 | +```java |
| 77 | +void main() { |
| 78 | + V7 v7 = new V7(Context.builder() |
| 79 | + .clock( |
| 80 | + new V7.ClockSequenceV7() // Reseeding timestamp context |
| 81 | + .withAdditionalPrecision() // Add 12-bits for timestamp-precision |
| 82 | + ) |
| 83 | + .random(() -> ThreadLocalRandom.current()) // For fast. `new SecureRandom()` for secure |
| 84 | + .node(() -> MacAddress.nodeId()) // Using other by specificizing NodeId.of(new byte[] {1, 2, 3, 4, 5, 6}) |
| 85 | + .messageDigest(algorithm -> MessageDigest.getInstance(algorithm)) // MessageDigest provider(getting from thread-local cache?) |
| 86 | + .build()); |
| 87 | + v7.next(); // Generation |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Typed Uuid |
| 92 | + |
| 93 | +```java |
| 94 | +import io.github.honhimw.uuid.Uuid; |
| 95 | + |
| 96 | +void main() { |
| 97 | + UUID jdkUUID = UUID.randomUUID(); |
| 98 | + Uuid uuid = Uuid.fromUUID(jdkUUID); |
| 99 | + |
| 100 | + assert Variant.RFC4122 == uuid.variant() : "variant RFC4122"; |
| 101 | + assert Version.RANDOM == uuid.version() : "version 4"; |
| 102 | + assert uuid.timestamp().isEmpty() : "not time based"; |
| 103 | + assert uuid.node().isEmpty() : "no node"; |
| 104 | + |
| 105 | + jdkUUID = uuid.asUUID(); |
| 106 | + byte[] bytes = uuid.asBytes(); |
| 107 | +} |
| 108 | +``` |
0 commit comments