|
4 | 4 | [](https://central.sonatype.com/artifact/io.azam.ulidj/ulidj)
|
5 | 5 | [](https://github.com/azam/ulidj/actions/workflows/build.yml)
|
6 | 6 |
|
7 |
| -ULID (Universally Unique Lexicographically Sortable Identifier) generator and parser for Java. |
| 7 | +ULID (Universally Unique Lexicographically Sortable Identifier) generator and parser for Java. Refer [ulid/spec](https://github.com/ulid/spec) for a more detailed ULID specification. |
8 | 8 |
|
9 |
| -Refer [ulid/spec](https://github.com/ulid/spec) for a more detailed ULID specification. |
| 9 | +## Features |
| 10 | + |
| 11 | +* Generates ULID to `String` (Crockford's base32) or `byte[]` (128-bit UUID compatible) objects |
| 12 | +* Parses ULID from `String` (Crockford's base32) or `byte[]` (128-bit UUID compatible) objects |
| 13 | +* Fast and simple static methods |
| 14 | +* Includes ULID monotonic generator |
| 15 | +* Zero runtime dependencies |
10 | 16 |
|
11 | 17 | ## License
|
12 | 18 |
|
@@ -55,26 +61,39 @@ String ulid1 = ULID.random();
|
55 | 61 | String ulid2 = ULID.random(ThreadLocalRandom.current());
|
56 | 62 | String ulid3 = ULID.random(SecureRandom.newInstance("SHA1PRNG"));
|
57 | 63 | byte[] entropy = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9 };
|
58 |
| -String ulid4 = ULID.generate(System.currentTimeMillis(), entropy); |
| 64 | +String ulid4 = ULID.generate(System.currentTimeMillis(), entropy); // Generate ULID in string representation |
| 65 | +byte[] ulid5 = ULID.generateBinary(System.currentTimeMillis(), entropy); // Generate ULID in binary representation |
59 | 66 | ```
|
60 | 67 |
|
61 | 68 | ULID parsing examples:
|
62 | 69 |
|
63 | 70 | ```java
|
| 71 | +// ULID string parsing |
64 | 72 | String ulid = "003JZ9J6G80123456789abcdef";
|
65 | 73 | assert ULID.isValid(ulid);
|
66 | 74 | long ts = ULID.getTimestamp(ulid);
|
67 | 75 | assert ts == 123456789000L;
|
68 | 76 | byte[] entropy = ULID.getEntropy(ulid);
|
| 77 | + |
| 78 | +// ULID binary parsing |
| 79 | +byte[] ulidBinary = new byte[] { // |
| 80 | + // Timestamp part |
| 81 | + (byte) 0x01, (byte) 0x33, (byte) 0x7C, (byte) 0x0D, (byte) 0xEF, (byte) 0x00, // |
| 82 | + // Entropy part |
| 83 | + (byte) 0x10, (byte) 0x20, (byte) 0x30, (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70, (byte) 0x80, (byte) 0x90, (byte) 0x10 // |
| 84 | +}; |
| 85 | +assert ULID.isValidBinary(ulidBinary); |
| 86 | +long ts = ULID.getTimestampBinary(ulidBinary); |
| 87 | +assert ts == 1320636247808L; |
| 88 | +byte[] entropy = ULID.getEntropyBinary(ulidBinary); |
69 | 89 | ```
|
70 | 90 |
|
71 | 91 | Monotonic ULID generation example:
|
72 | 92 |
|
73 | 93 | ```java
|
74 | 94 | MonotonicULID ulid = new MonotonicULID();
|
75 |
| -String ulid1 = ulid.next(); |
76 |
| -String ulid2 = ulid.next(); |
77 |
| -String ulid3 = ulid.next(); |
| 95 | +String ulidString = ulid.generate(); // Generate ULID in string representation |
| 96 | +byte[] ulidBinary = ulid.generateBinary(); // Generate ULID in binary representation |
78 | 97 | ```
|
79 | 98 |
|
80 | 99 | ## Develop
|
|
0 commit comments