Skip to content

Commit 41eadaa

Browse files
committed
Add customisable clock to UUIDTimer
1 parent e9c0a17 commit 41eadaa

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* JUG Java Uuid Generator
2+
*
3+
* Copyright (c) 2002 Tatu Saloranta, tatu.saloranta@iki.fi
4+
*
5+
* Licensed under the License specified in the file LICENSE which is
6+
* included with the source code.
7+
* You may not use this file except in compliance with the License.
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.fasterxml.uuid;
17+
18+
/**
19+
* UUIDClock is used by UUIDTimer to get the current time. The default
20+
* implementation returns the time from the system clock, but this class can
21+
* be overriden to return any number. This is useful when UUIDs with past or
22+
* future timestamps should be generated, or when UUIDs must be generated in
23+
* a reproducible manner.
24+
*/
25+
public class UUIDClock
26+
{
27+
/**
28+
* Returns the current time in milliseconds.
29+
*/
30+
public long currentTimeMillis()
31+
{
32+
return System.currentTimeMillis();
33+
}
34+
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public class UUIDTimer
113113
*/
114114
protected final Random _random;
115115

116+
/**
117+
* Clock used to get the time when a timestamp is requested.
118+
*/
119+
protected final UUIDClock _clock;
120+
116121
// // // Clock state:
117122

118123
/**
@@ -158,9 +163,15 @@ public class UUIDTimer
158163
private int _clockCounter = 0;
159164

160165
public UUIDTimer(Random rnd, TimestampSynchronizer sync) throws IOException
166+
{
167+
this(rnd, sync, new UUIDClock());
168+
}
169+
170+
public UUIDTimer(Random rnd, TimestampSynchronizer sync, UUIDClock clock) throws IOException
161171
{
162172
_random = rnd;
163173
_syncer = sync;
174+
_clock = clock;
164175
initCounters(rnd);
165176
_lastSystemTimestamp = 0L;
166177
// This may get overwritten by the synchronizer
@@ -213,7 +224,7 @@ public int getClockSequence() {
213224
*/
214225
public synchronized long getTimestamp()
215226
{
216-
long systime = System.currentTimeMillis();
227+
long systime = _clock.currentTimeMillis();
217228
/* Let's first verify that the system time is not going backwards;
218229
* independent of whether we can use it:
219230
*/

src/test/java/com/fasterxml/uuid/UUIDTimerTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Arrays;
2323
import java.util.Comparator;
2424
import java.util.HashSet;
25+
import java.util.Random;
2526
import java.util.Set;
2627

2728
import junit.framework.Test;
@@ -187,6 +188,31 @@ public void testGetTimestamp() throws IOException
187188
checkUUIDTimerLongArrayForCorrectCreationTime(
188189
uuid_timer_array_of_longs, start_time, end_time);
189190
}
191+
192+
/**
193+
* Test of reproducibility of getTimestamp method, of class
194+
* com.fasterxml.uuid.UUIDTimer.
195+
*/
196+
public void testGetTimestampReproducible() throws IOException
197+
{
198+
final long seed = new Random().nextLong();
199+
final long time = new Random().nextLong();
200+
201+
final UUIDTimer timer1 = new UUIDTimer(new Random(seed), null, new UUIDClock() {
202+
@Override
203+
public long currentTimeMillis() {
204+
return time;
205+
}
206+
});
207+
final UUIDTimer timer2 = new UUIDTimer(new Random(seed), null, new UUIDClock() {
208+
@Override
209+
public long currentTimeMillis() {
210+
return time;
211+
}
212+
});
213+
214+
assertEquals(timer1.getTimestamp(), timer2.getTimestamp());
215+
}
190216

191217
/**************************************************************************
192218
* Begin private helper functions for use in tests

0 commit comments

Comments
 (0)