Skip to content

Commit

Permalink
New factory method to create TimeBasedEpochRandomGenerator (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
worldtiki authored Feb 28, 2024
1 parent 1dd4f2c commit aa73bd3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
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);
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

0 comments on commit aa73bd3

Please sign in to comment.