Skip to content

New factory method to create TimeBasedEpochRandomGenerator #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,8 @@ Pavel Raev (magdel@github)
Maia Everett (Maia-Everett@github)
* Contributed #85: Fix `LazyRandom` for native code generation tools
[5.0.0]

Daniel Albuquerque (worldtiki@github)
* Contributed #99: New factory method to create TimeBasedEpochRandomGenerator
[5.1.0]

5 changes: 5 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Project: java-uuid-generator
Releases
============================================================================

5.1.0 (not yet released)

#99: New factory method to create TimeBasedEpochRandomGenerator
(contributed by Daniel A)

5.0.0 (23-Feb-2024)

#53: Increase JDK baseline to JDK 8
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/fasterxml/uuid/Generators.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public static NameBasedGenerator nameBasedGenerator(UUID namespace, MessageDiges
/**
* Factory method for constructing UUID generator that generates UUID using
* version 7 (Unix Epoch time+random based).
*<p>
* NOTE: calls within same millisecond produce very similar values; this may be
* unsafe in some environments.
*<p>
* No additional external synchronization is used.
*/
public static TimeBasedEpochGenerator timeBasedEpochGenerator()
{
Expand Down Expand Up @@ -166,6 +171,24 @@ public static TimeBasedEpochGenerator timeBasedEpochGenerator(Random random,
return new TimeBasedEpochGenerator(random, clock);
}

// // Epoch Time+random generation

/**
* Factory method for constructing UUID generator that generates UUID using
* version 7 (Unix Epoch time+random based).
*<p>
* Calls within same millisecond use additional per-call randomness to try to create
* more distinct values, compared to {@link #timeBasedEpochGenerator(Random)}
*<p>
* No additional external synchronization is used.
*
* @since 5.1
*/
public static TimeBasedEpochRandomGenerator timeBasedEpochRandomGenerator()
{
return timeBasedEpochRandomGenerator(null);
}

/**
* Factory method for constructing UUID generator that generates UUID using
* version 7 (Unix Epoch time+random based), using specified {@link Random}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/fasterxml/uuid/impl/UUIDUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public void testExtractTimestampUUIDEpochBased() {
}
}

public void testExtractTimestampUUIDEpochRandomBased() {
TimeBasedEpochRandomGenerator generator = Generators.timeBasedEpochRandomGenerator();
final Random rnd = new Random(3);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok so this Random is just for timestamp, but otherwise we get sort of arbitrary SecureRandom.
I guess that's about what we can verify.

for (int i = 0; i < TEST_REPS; i++) {
long rawTimestamp = rnd.nextLong() >>> 16;
UUID uuid = generator.construct(rawTimestamp);
assertEquals(rawTimestamp, UUIDUtil.extractTimestamp(uuid));
}
}

public void testExtractTimestampUUIDOnOtherValues() {
assertEquals(0L, UUIDUtil.extractTimestamp(null));
assertEquals(0L, UUIDUtil.extractTimestamp(UUID.fromString("00000000-0000-0000-0000-000000000000")));
Expand Down