Skip to content

Commit aa73bd3

Browse files
authored
New factory method to create TimeBasedEpochRandomGenerator (#99)
1 parent 1dd4f2c commit aa73bd3

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

release-notes/CREDITS

+5
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,8 @@ Pavel Raev (magdel@github)
143143
Maia Everett (Maia-Everett@github)
144144
* Contributed #85: Fix `LazyRandom` for native code generation tools
145145
[5.0.0]
146+
147+
Daniel Albuquerque (worldtiki@github)
148+
* Contributed #99: New factory method to create TimeBasedEpochRandomGenerator
149+
[5.1.0]
150+

release-notes/VERSION

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Project: java-uuid-generator
44
Releases
55
============================================================================
66

7+
5.1.0 (not yet released)
8+
9+
#99: New factory method to create TimeBasedEpochRandomGenerator
10+
(contributed by Daniel A)
11+
712
5.0.0 (23-Feb-2024)
813

914
#53: Increase JDK baseline to JDK 8

src/main/java/com/fasterxml/uuid/Generators.java

+23
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public static NameBasedGenerator nameBasedGenerator(UUID namespace, MessageDiges
126126
/**
127127
* Factory method for constructing UUID generator that generates UUID using
128128
* version 7 (Unix Epoch time+random based).
129+
*<p>
130+
* NOTE: calls within same millisecond produce very similar values; this may be
131+
* unsafe in some environments.
132+
*<p>
133+
* No additional external synchronization is used.
129134
*/
130135
public static TimeBasedEpochGenerator timeBasedEpochGenerator()
131136
{
@@ -166,6 +171,24 @@ public static TimeBasedEpochGenerator timeBasedEpochGenerator(Random random,
166171
return new TimeBasedEpochGenerator(random, clock);
167172
}
168173

174+
// // Epoch Time+random generation
175+
176+
/**
177+
* Factory method for constructing UUID generator that generates UUID using
178+
* version 7 (Unix Epoch time+random based).
179+
*<p>
180+
* Calls within same millisecond use additional per-call randomness to try to create
181+
* more distinct values, compared to {@link #timeBasedEpochGenerator(Random)}
182+
*<p>
183+
* No additional external synchronization is used.
184+
*
185+
* @since 5.1
186+
*/
187+
public static TimeBasedEpochRandomGenerator timeBasedEpochRandomGenerator()
188+
{
189+
return timeBasedEpochRandomGenerator(null);
190+
}
191+
169192
/**
170193
* Factory method for constructing UUID generator that generates UUID using
171194
* version 7 (Unix Epoch time+random based), using specified {@link Random}

src/test/java/com/fasterxml/uuid/impl/UUIDUtilTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ public void testExtractTimestampUUIDEpochBased() {
6161
}
6262
}
6363

64+
public void testExtractTimestampUUIDEpochRandomBased() {
65+
TimeBasedEpochRandomGenerator generator = Generators.timeBasedEpochRandomGenerator();
66+
final Random rnd = new Random(3);
67+
for (int i = 0; i < TEST_REPS; i++) {
68+
long rawTimestamp = rnd.nextLong() >>> 16;
69+
UUID uuid = generator.construct(rawTimestamp);
70+
assertEquals(rawTimestamp, UUIDUtil.extractTimestamp(uuid));
71+
}
72+
}
73+
6474
public void testExtractTimestampUUIDOnOtherValues() {
6575
assertEquals(0L, UUIDUtil.extractTimestamp(null));
6676
assertEquals(0L, UUIDUtil.extractTimestamp(UUID.fromString("00000000-0000-0000-0000-000000000000")));

0 commit comments

Comments
 (0)