File tree Expand file tree Collapse file tree 3 files changed +72
-1
lines changed
main/java/com/fasterxml/uuid
test/java/com/fasterxml/uuid Expand file tree Collapse file tree 3 files changed +72
-1
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -113,6 +113,11 @@ public class UUIDTimer
113
113
*/
114
114
protected final Random _random ;
115
115
116
+ /**
117
+ * Clock used to get the time when a timestamp is requested.
118
+ */
119
+ protected final UUIDClock _clock ;
120
+
116
121
// // // Clock state:
117
122
118
123
/**
@@ -158,9 +163,15 @@ public class UUIDTimer
158
163
private int _clockCounter = 0 ;
159
164
160
165
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
161
171
{
162
172
_random = rnd ;
163
173
_syncer = sync ;
174
+ _clock = clock ;
164
175
initCounters (rnd );
165
176
_lastSystemTimestamp = 0L ;
166
177
// This may get overwritten by the synchronizer
@@ -213,7 +224,7 @@ public int getClockSequence() {
213
224
*/
214
225
public synchronized long getTimestamp ()
215
226
{
216
- long systime = System .currentTimeMillis ();
227
+ long systime = _clock .currentTimeMillis ();
217
228
/* Let's first verify that the system time is not going backwards;
218
229
* independent of whether we can use it:
219
230
*/
Original file line number Diff line number Diff line change 22
22
import java .util .Arrays ;
23
23
import java .util .Comparator ;
24
24
import java .util .HashSet ;
25
+ import java .util .Random ;
25
26
import java .util .Set ;
26
27
27
28
import junit .framework .Test ;
@@ -187,6 +188,31 @@ public void testGetTimestamp() throws IOException
187
188
checkUUIDTimerLongArrayForCorrectCreationTime (
188
189
uuid_timer_array_of_longs , start_time , end_time );
189
190
}
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
+ }
190
216
191
217
/**************************************************************************
192
218
* Begin private helper functions for use in tests
You can’t perform that action at this time.
0 commit comments