Update UlidFactory.java
This commit is contained in:
parent
bc4d66bac1
commit
280822cc38
|
@ -25,7 +25,6 @@
|
|||
package com.github.f4b6a3.ulid;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Clock;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
import java.util.function.IntFunction;
|
||||
|
@ -170,52 +169,6 @@ public final class UlidFactory {
|
|||
return new UlidFactory(new MonotonicFunction(randomFunction));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new monotonic factory.
|
||||
*
|
||||
* @param random a {@link Random} generator
|
||||
* @param clock a clock instance that provides the current time in
|
||||
* milliseconds, measured from the UNIX epoch of 1970-01-01T00:00Z
|
||||
* (UTC)
|
||||
* @return {@link UlidFactory}
|
||||
*/
|
||||
static UlidFactory newMonotonicInstance(Random random, Clock clock) {
|
||||
Objects.requireNonNull(clock, "Clock instant must not be null");
|
||||
return new UlidFactory(new MonotonicFunction(random), clock::millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new monotonic factory.
|
||||
* <p>
|
||||
* The given random function must return a long value.
|
||||
*
|
||||
* @param randomFunction a random function that returns a long value
|
||||
* @param clock a clock instance that provides the current time in
|
||||
* milliseconds, measured from the UNIX epoch of
|
||||
* 1970-01-01T00:00Z (UTC)
|
||||
* @return {@link UlidFactory}
|
||||
*/
|
||||
static UlidFactory newMonotonicInstance(LongSupplier randomFunction, Clock clock) {
|
||||
Objects.requireNonNull(clock, "Clock instant must not be null");
|
||||
return new UlidFactory(new MonotonicFunction(randomFunction), clock::millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new monotonic factory.
|
||||
* <p>
|
||||
* The given random function must return a byte array.
|
||||
*
|
||||
* @param randomFunction a random function that returns a byte array
|
||||
* @param clock a clock instance that provides the current time in
|
||||
* milliseconds, measured from the UNIX epoch of
|
||||
* 1970-01-01T00:00Z (UTC)
|
||||
* @return {@link UlidFactory}
|
||||
*/
|
||||
static UlidFactory newMonotonicInstance(IntFunction<byte[]> randomFunction, Clock clock) {
|
||||
Objects.requireNonNull(clock, "Clock instant must not be null");
|
||||
return new UlidFactory(new MonotonicFunction(randomFunction), clock::millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new monotonic factory.
|
||||
*
|
||||
|
|
|
@ -6,11 +6,11 @@ import static org.junit.Assert.*;
|
|||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.IntFunction;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
|
@ -41,32 +41,11 @@ public class UlidFactoryMonotonicTest extends UlidFactoryTest {
|
|||
long time = Instant.parse("2021-12-31T23:59:59.000Z").toEpochMilli();
|
||||
long times[] = { time + 0, time + 1, time + 2, time + 3, time + 4 - diff, time + 5 - diff, time + 6 - diff };
|
||||
|
||||
Clock clock = new Clock() {
|
||||
private int i;
|
||||
|
||||
@Override
|
||||
public long millis() {
|
||||
return times[i++ % times.length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZoneId getZone() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Clock withZone(ZoneId zone) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant instant() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
AtomicInteger i = new AtomicInteger();
|
||||
LongSupplier timeFunction = () -> times[i.getAndIncrement() % times.length];
|
||||
|
||||
LongSupplier randomFunction = () -> 0;
|
||||
UlidFactory factory = UlidFactory.newMonotonicInstance(randomFunction, clock);
|
||||
UlidFactory factory = UlidFactory.newMonotonicInstance(randomFunction, timeFunction);
|
||||
|
||||
Ulid ulid1 = factory.create();
|
||||
Ulid ulid2 = factory.create();
|
||||
|
@ -119,32 +98,11 @@ public class UlidFactoryMonotonicTest extends UlidFactoryTest {
|
|||
long leap = time - 1000; // moving the clock hands 1 second backwards
|
||||
long times[] = { time, leap };
|
||||
|
||||
Clock clock = new Clock() {
|
||||
private int i;
|
||||
|
||||
@Override
|
||||
public long millis() {
|
||||
return times[i++ % times.length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZoneId getZone() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Clock withZone(ZoneId zone) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant instant() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
AtomicInteger i = new AtomicInteger();
|
||||
LongSupplier timeFunction = () -> times[i.getAndIncrement() % times.length];
|
||||
|
||||
LongSupplier randomFunction = () -> 0;
|
||||
UlidFactory factory = UlidFactory.newMonotonicInstance(randomFunction, clock);
|
||||
UlidFactory factory = UlidFactory.newMonotonicInstance(randomFunction, timeFunction);
|
||||
|
||||
// the clock moved normally
|
||||
Ulid ulid1 = factory.create();
|
||||
|
@ -167,32 +125,11 @@ public class UlidFactoryMonotonicTest extends UlidFactoryTest {
|
|||
long time = Instant.parse("2021-12-31T23:59:59.999Z").toEpochMilli();
|
||||
long times[] = { time + 1, time + 2, time + 3, time, time, time };
|
||||
|
||||
Clock clock = new Clock() {
|
||||
private int i;
|
||||
|
||||
@Override
|
||||
public long millis() {
|
||||
return times[i++ % times.length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZoneId getZone() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Clock withZone(ZoneId zone) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant instant() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
AtomicInteger i = new AtomicInteger();
|
||||
LongSupplier timeFunction = () -> times[i.getAndIncrement() % times.length];
|
||||
|
||||
LongSupplier randomSupplier = () -> 0xffffffffffffffffL;
|
||||
UlidFactory factory = UlidFactory.newMonotonicInstance(randomSupplier, clock);
|
||||
UlidFactory factory = UlidFactory.newMonotonicInstance(randomSupplier, timeFunction);
|
||||
|
||||
Ulid ulid1 = factory.create();
|
||||
Ulid ulid2 = factory.create();
|
||||
|
@ -287,7 +224,7 @@ public class UlidFactoryMonotonicTest extends UlidFactoryTest {
|
|||
{
|
||||
SplittableRandom random = new SplittableRandom();
|
||||
LongSupplier function = () -> random.nextLong();
|
||||
UlidFactory factory = UlidFactory.newMonotonicInstance(function, Clock.systemDefaultZone());
|
||||
UlidFactory factory = UlidFactory.newMonotonicInstance(function, () -> Clock.systemUTC().millis());
|
||||
assertNotNull(factory.create());
|
||||
}
|
||||
{
|
||||
|
@ -305,7 +242,7 @@ public class UlidFactoryMonotonicTest extends UlidFactoryTest {
|
|||
ThreadLocalRandom.current().nextBytes(bytes);
|
||||
return bytes;
|
||||
};
|
||||
UlidFactory factory = UlidFactory.newMonotonicInstance(function, Clock.systemDefaultZone());
|
||||
UlidFactory factory = UlidFactory.newMonotonicInstance(function, () -> Clock.systemUTC().millis());
|
||||
assertNotNull(factory.create());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue