Skip to content

Commit f67d3e4

Browse files
committed
Refactor AbstRandomBasedFactory
1 parent 67e2bf6 commit f67d3e4

File tree

9 files changed

+65
-57
lines changed

9 files changed

+65
-57
lines changed

src/main/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactory.java

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.github.f4b6a3.uuid.factory;
2626

2727
import java.security.SecureRandom;
28+
import java.util.Objects;
2829
import java.util.Random;
2930
import java.util.UUID;
3031
import java.util.concurrent.ThreadLocalRandom;
@@ -95,7 +96,7 @@ protected abstract static class Builder<T, B extends Builder<T, B>> {
9596
*/
9697
protected IRandom getRandom() {
9798
if (this.random == null) {
98-
this.random = new ByteRandom(new DefaultRandomFunction());
99+
this.random = new SafeRandom(new DefaultRandomFunction());
99100
}
100101
return this.random;
101102
}
@@ -110,9 +111,9 @@ protected IRandom getRandom() {
110111
public B withRandom(Random random) {
111112
if (random != null) {
112113
if (random instanceof SecureRandom) {
113-
this.random = new ByteRandom(random);
114+
this.random = new SafeRandom(random);
114115
} else {
115-
this.random = new LongRandom(random);
116+
this.random = new FastRandom(random);
116117
}
117118
}
118119
return (B) this;
@@ -121,11 +122,28 @@ public B withRandom(Random random) {
121122
/**
122123
* Set the random generator with a fast algorithm.
123124
*
125+
* Use it to replace the {@link DefaultRandomFunction} with
126+
* {@link ThreadLocalRandom}.
127+
*
124128
* @return the generator
125129
*/
126130
@SuppressWarnings("unchecked")
127131
public B withFastRandom() {
128-
this.random = new LongRandom(() -> ThreadLocalRandom.current().nextLong());
132+
this.random = new FastRandom();
133+
return (B) this;
134+
}
135+
136+
/**
137+
* Set the random generator with a safe algorithm.
138+
*
139+
* Use it to replace the {@link DefaultRandomFunction} with
140+
* {@link SecureRandom}.
141+
*
142+
* @return the generator
143+
*/
144+
@SuppressWarnings("unchecked")
145+
public B withSafeRandom() {
146+
this.random = new SafeRandom();
129147
return (B) this;
130148
}
131149

@@ -137,7 +155,7 @@ public B withFastRandom() {
137155
*/
138156
@SuppressWarnings("unchecked")
139157
public B withRandomFunction(LongSupplier randomFunction) {
140-
this.random = new LongRandom(randomFunction);
158+
this.random = new FastRandom(randomFunction);
141159
return (B) this;
142160
}
143161

@@ -149,7 +167,7 @@ public B withRandomFunction(LongSupplier randomFunction) {
149167
*/
150168
@SuppressWarnings("unchecked")
151169
public B withRandomFunction(IntFunction<byte[]> randomFunction) {
152-
this.random = new ByteRandom(randomFunction);
170+
this.random = new SafeRandom(randomFunction);
153171
return (B) this;
154172
}
155173

@@ -185,33 +203,33 @@ protected static interface IRandom {
185203
/**
186204
* A long random generator.
187205
*/
188-
protected static final class LongRandom implements IRandom {
206+
protected static final class FastRandom implements IRandom {
189207

190208
private final LongSupplier randomFunction;
191209

192210
/**
193211
* Default constructor.
194212
*/
195-
public LongRandom() {
196-
this(newRandomFunction(null));
213+
public FastRandom() {
214+
this(newFastFunction(null));
197215
}
198216

199217
/**
200218
* Constructor with a random.
201219
*
202220
* @param random a random
203221
*/
204-
public LongRandom(Random random) {
205-
this(newRandomFunction(random));
222+
public FastRandom(Random random) {
223+
this(newFastFunction(Objects.requireNonNull(random)));
206224
}
207225

208226
/**
209227
* Constructor with a function which returns random numbers.
210228
*
211229
* @param randomFunction a function
212230
*/
213-
public LongRandom(LongSupplier randomFunction) {
214-
this.randomFunction = randomFunction != null ? randomFunction : newRandomFunction(null);
231+
public FastRandom(LongSupplier randomFunction) {
232+
this.randomFunction = Objects.requireNonNull(randomFunction);
215233
}
216234

217235
@Override
@@ -244,42 +262,44 @@ public byte[] nextBytes(int length) {
244262
* @param random a random
245263
* @return a function
246264
*/
247-
protected static LongSupplier newRandomFunction(Random random) {
248-
final Random entropy = random != null ? random : new SecureRandom();
249-
return entropy::nextLong;
265+
private static LongSupplier newFastFunction(Random random) {
266+
if (random != null) {
267+
return () -> random.nextLong();
268+
}
269+
return () -> ThreadLocalRandom.current().nextLong();
250270
}
251271
}
252272

253273
/**
254274
* A byte random generator.
255275
*/
256-
protected static final class ByteRandom implements IRandom {
276+
protected static final class SafeRandom implements IRandom {
257277

258278
private final IntFunction<byte[]> randomFunction;
259279

260280
/**
261281
* Default constructor.
262282
*/
263-
public ByteRandom() {
264-
this(newRandomFunction(null));
283+
public SafeRandom() {
284+
this(newSafeFunction(null));
265285
}
266286

267287
/**
268288
* Constructor with a random.
269289
*
270290
* @param random a random
271291
*/
272-
public ByteRandom(Random random) {
273-
this(newRandomFunction(random));
292+
public SafeRandom(Random random) {
293+
this(newSafeFunction(Objects.requireNonNull(random)));
274294
}
275295

276296
/**
277297
* Constructor with a function which returns random numbers.
278298
*
279299
* @param randomFunction a function
280300
*/
281-
public ByteRandom(IntFunction<byte[]> randomFunction) {
282-
this.randomFunction = randomFunction != null ? randomFunction : newRandomFunction(null);
301+
public SafeRandom(IntFunction<byte[]> randomFunction) {
302+
this.randomFunction = Objects.requireNonNull(randomFunction);
283303
}
284304

285305
@Override
@@ -299,7 +319,7 @@ public byte[] nextBytes(int length) {
299319
* @param random a random
300320
* @return a function
301321
*/
302-
protected static IntFunction<byte[]> newRandomFunction(Random random) {
322+
private static IntFunction<byte[]> newSafeFunction(Random random) {
303323
final Random entropy = random != null ? random : new SecureRandom();
304324
return (final int length) -> {
305325
final byte[] bytes = new byte[length];

src/main/java/com/github/f4b6a3/uuid/factory/nonstandard/PrefixCombFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public UUID create() {
156156
lock.lock();
157157
try {
158158
final long time = timeFunction.getAsLong();
159-
if (this.random instanceof ByteRandom) {
159+
if (this.random instanceof SafeRandom) {
160160
final byte[] bytes = this.random.nextBytes(10);
161161
final long long1 = ByteUtil.toNumber(bytes, 0, 2);
162162
final long long2 = ByteUtil.toNumber(bytes, 2, 10);

src/main/java/com/github/f4b6a3/uuid/factory/nonstandard/ShortPrefixCombFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public UUID create() {
197197
lock.lock();
198198
try {
199199
final long time = timeFunction.getAsLong() / interval;
200-
if (this.random instanceof ByteRandom) {
200+
if (this.random instanceof SafeRandom) {
201201
final byte[] bytes = this.random.nextBytes(14);
202202
final long long1 = ByteUtil.toNumber(bytes, 0, 6);
203203
final long long2 = ByteUtil.toNumber(bytes, 6, 14);

src/main/java/com/github/f4b6a3/uuid/factory/nonstandard/ShortSuffixCombFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public UUID create() {
197197
lock.lock();
198198
try {
199199
final long time = timeFunction.getAsLong() / interval;
200-
if (this.random instanceof ByteRandom) {
200+
if (this.random instanceof SafeRandom) {
201201
final byte[] bytes = this.random.nextBytes(14);
202202
final long long1 = ByteUtil.toNumber(bytes, 0, 8);
203203
final long long2 = ByteUtil.toNumber(bytes, 8, 14);

src/main/java/com/github/f4b6a3/uuid/factory/nonstandard/SuffixCombFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public UUID create() {
159159
lock.lock();
160160
try {
161161
final long time = timeFunction.getAsLong();
162-
if (this.random instanceof ByteRandom) {
162+
if (this.random instanceof SafeRandom) {
163163
final byte[] bytes = this.random.nextBytes(10);
164164
final long long1 = ByteUtil.toNumber(bytes, 0, 8);
165165
final long long2 = ByteUtil.toNumber(bytes, 8, 10);

src/main/java/com/github/f4b6a3/uuid/factory/rfc4122/RandomBasedFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public static Builder builder() {
117117
public UUID create() {
118118
lock.lock();
119119
try {
120-
if (this.random instanceof ByteRandom) {
120+
if (this.random instanceof SafeRandom) {
121121
final byte[] bytes = this.random.nextBytes(16);
122122
final long msb = ByteUtil.toNumber(bytes, 0, 8);
123123
final long lsb = ByteUtil.toNumber(bytes, 8, 16);

src/main/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeOrderedEpochFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ long time() {
328328
}
329329

330330
void reset(final long time) {
331-
if (random instanceof ByteRandom) {
331+
if (random instanceof SafeRandom) {
332332
final byte[] bytes = random.nextBytes(10);
333333
this.msb = (time << 16) | (ByteUtil.toNumber(bytes, 0, 2));
334334
this.lsb = ByteUtil.toNumber(bytes, 2, 10);
@@ -358,7 +358,7 @@ void increment() {
358358
}
359359

360360
// then randomize the lower 48 bits
361-
if (random instanceof ByteRandom) {
361+
if (random instanceof SafeRandom) {
362362
final byte[] bytes = random.nextBytes(6);
363363
this.lsb |= ByteUtil.toNumber(bytes);
364364
} else {
@@ -409,7 +409,7 @@ void increment() {
409409

410410
private LongSupplier customPlusNFunction(IRandom random, Long incrementMax) {
411411
if (incrementMax == INCREMENT_MAX_DEFAULT) {
412-
if (random instanceof ByteRandom) {
412+
if (random instanceof SafeRandom) {
413413
return () -> {
414414
// return n, where 1 <= n <= 2^32
415415
final byte[] bytes = random.nextBytes(4);
@@ -423,7 +423,7 @@ private LongSupplier customPlusNFunction(IRandom random, Long incrementMax) {
423423
}
424424
} else {
425425
final long positive = 0x7fffffffffffffffL;
426-
if (random instanceof ByteRandom) {
426+
if (random instanceof SafeRandom) {
427427
// the minimum number of bits and bytes for incrementMax
428428
final int bits = (int) Math.ceil(Math.log(incrementMax) / Math.log(2));
429429
final int size = ((bits - 1) / Byte.SIZE) + 1;

src/test/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactoryTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void testByteRandomNextLong() {
3030
byte[] bytes = new byte[Long.BYTES];
3131
(new Random()).nextBytes(bytes);
3232
long number = ByteBuffer.wrap(bytes).getLong();
33-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.ByteRandom((x) -> bytes);
33+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.SafeRandom((x) -> bytes);
3434
assertEquals(number, random.nextLong());
3535
}
3636

@@ -44,7 +44,7 @@ public void testByteRandomNextLong() {
4444
ByteBuffer buffer1 = ByteBuffer.wrap(bytes);
4545
ByteBuffer buffer2 = ByteBuffer.wrap(bytes);
4646

47-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.ByteRandom((x) -> {
47+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.SafeRandom((x) -> {
4848
byte[] octects = new byte[x];
4949
buffer1.get(octects);
5050
return octects;
@@ -62,7 +62,7 @@ public void testByteRandomNextBytes() {
6262
for (int i = 0; i < 10; i++) {
6363
byte[] bytes = new byte[Long.BYTES];
6464
(new Random()).nextBytes(bytes);
65-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.ByteRandom((x) -> bytes);
65+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.SafeRandom((x) -> bytes);
6666
assertEquals(Arrays.toString(bytes), Arrays.toString(random.nextBytes(Long.BYTES)));
6767
}
6868

@@ -76,7 +76,7 @@ public void testByteRandomNextBytes() {
7676
ByteBuffer buffer1 = ByteBuffer.wrap(bytes);
7777
ByteBuffer buffer2 = ByteBuffer.wrap(bytes);
7878

79-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.ByteRandom((x) -> {
79+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.SafeRandom((x) -> {
8080
byte[] octects = new byte[x];
8181
buffer1.get(octects);
8282
return octects;
@@ -97,7 +97,7 @@ public void testLogRandomNextLong() {
9797
byte[] bytes = new byte[Long.BYTES];
9898
(new Random()).nextBytes(bytes);
9999
long number = ByteBuffer.wrap(bytes).getLong();
100-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.LongRandom(() -> number);
100+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> number);
101101
assertEquals(number, random.nextLong());
102102
}
103103

@@ -111,7 +111,7 @@ public void testLogRandomNextLong() {
111111
ByteBuffer buffer1 = ByteBuffer.wrap(bytes);
112112
ByteBuffer buffer2 = ByteBuffer.wrap(bytes);
113113

114-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.LongRandom(() -> buffer1.getLong());
114+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> buffer1.getLong());
115115

116116
for (int j = 0; j < ints; j++) {
117117
assertEquals(buffer2.getLong(), random.nextLong());
@@ -127,7 +127,7 @@ public void testLogRandomNextBytes() {
127127
byte[] bytes = new byte[Long.BYTES];
128128
(new Random()).nextBytes(bytes);
129129
long number = ByteBuffer.wrap(bytes).getLong();
130-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.LongRandom(() -> number);
130+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> number);
131131
assertEquals(Arrays.toString(bytes), Arrays.toString(random.nextBytes(Long.BYTES)));
132132
}
133133

@@ -141,7 +141,7 @@ public void testLogRandomNextBytes() {
141141
ByteBuffer buffer1 = ByteBuffer.wrap(bytes);
142142
ByteBuffer buffer2 = ByteBuffer.wrap(bytes);
143143

144-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.LongRandom(() -> buffer1.getLong());
144+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> buffer1.getLong());
145145

146146
for (int j = 0; j < ints; j++) {
147147
byte[] octects = new byte[Long.BYTES];
@@ -156,15 +156,15 @@ public void testLongRandom() {
156156

157157
{
158158
long nextLong = 0x1122334455667788L;
159-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.LongRandom(() -> nextLong);
159+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> nextLong);
160160
byte[] bytes = random.nextBytes(Long.BYTES);
161161
ByteBuffer buffer = ByteBuffer.wrap(bytes);
162162
assertEquals(nextLong, buffer.getLong());
163163
}
164164

165165
{
166166
long nextLong = ThreadLocalRandom.current().nextLong();
167-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.LongRandom(() -> nextLong);
167+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> nextLong);
168168
byte[] bytes = random.nextBytes(Long.BYTES);
169169
ByteBuffer buffer = ByteBuffer.wrap(bytes);
170170
assertEquals(nextLong, buffer.getLong());
@@ -183,7 +183,7 @@ public void testLongRandom() {
183183
ByteBuffer buffer2 = ByteBuffer.allocate(octects);
184184

185185
AtomicInteger x = new AtomicInteger();
186-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.LongRandom(
186+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(
187187
() -> nextLong[x.getAndIncrement()]);
188188

189189
for (int i = 0; i < nextLong.length; i++) {
@@ -210,7 +210,7 @@ public void testLongRandom() {
210210
ByteBuffer buffer2 = ByteBuffer.allocate(octects);
211211

212212
AtomicInteger x = new AtomicInteger();
213-
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.LongRandom(
213+
AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(
214214
() -> nextLong[x.getAndIncrement()]);
215215

216216
for (int i = 0; i < nextLong.length; i++) {

src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeOrderedEpochFactoryTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,6 @@ public void testWithRandomNull() {
8888
assertNotNull(factory.create());
8989
}
9090

91-
@Test
92-
public void testWithRandomFunctionNull() {
93-
{
94-
TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory((LongSupplier) null);
95-
assertNotNull(factory.create());
96-
}
97-
{
98-
TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory((IntFunction<byte[]>) null);
99-
assertNotNull(factory.create());
100-
}
101-
}
102-
10391
@Test
10492
public void testGetTimeOrderedEpoch() {
10593

0 commit comments

Comments
 (0)