Skip to content

Commit 7ddb95a

Browse files
author
BranchPredictor
authored
Add construct() methods to specify the milliseconds being used for time-based UUID generation (#84)
1 parent f1ef2b5 commit 7ddb95a

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/main/java/com/fasterxml/uuid/impl/TimeBasedEpochGenerator.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,22 @@ public TimeBasedEpochGenerator(Random rnd, UUIDClock clock)
9797
/* UUID generation
9898
/**********************************************************************
9999
*/
100-
100+
101101
@Override
102102
public UUID generate()
103+
{
104+
return construct(_clock.currentTimeMillis());
105+
}
106+
107+
/**
108+
* @since 4.3
109+
* @param rawTimestamp unix epoch millis
110+
* @return unix epoch time based UUID
111+
*/
112+
public UUID construct(long rawTimestamp)
103113
{
104114
lock.lock();
105115
try {
106-
long rawTimestamp = _clock.currentTimeMillis();
107116
if (rawTimestamp == _lastTimestamp) {
108117
boolean c = true;
109118
for (int i = ENTROPY_BYTE_LENGTH - 1; i >= 0; i--) {

src/main/java/com/fasterxml/uuid/impl/TimeBasedGenerator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public TimeBasedGenerator(EthernetAddress ethAddr, UUIDTimer timer)
9090
public UUID generate()
9191
{
9292
final long rawTimestamp = _timer.getTimestamp();
93+
return construct(rawTimestamp);
94+
}
95+
96+
/**
97+
* @since 4.3
98+
* @param rawTimestamp unix epoch millis
99+
* @return unix epoch time based UUID
100+
*/
101+
public UUID construct(long rawTimestamp)
102+
{
93103
// Time field components are kind of shuffled, need to slice:
94104
int clockHi = (int) (rawTimestamp >>> 32);
95105
int clockLo = (int) rawTimestamp;

src/main/java/com/fasterxml/uuid/impl/TimeBasedReorderedGenerator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,16 @@ public UUID generate()
9696
{
9797
// Ok, get 60-bit timestamp (4 MSB are ignored)
9898
final long rawTimestamp = _timer.getTimestamp();
99+
return construct(rawTimestamp);
100+
}
99101

102+
/**
103+
* @since 4.3
104+
* @param rawTimestamp unix epoch millis
105+
* @return unix epoch time based UUID
106+
*/
107+
public UUID construct(long rawTimestamp)
108+
{
100109
// First: discard 4 MSB, next 32 bits (top of 60-bit timestamp) form the
101110
// highest 32-bit segments
102111
final long timestampHigh = (rawTimestamp >>> 28) << 32;

0 commit comments

Comments
 (0)