diff --git a/src/main/java/com/github/f4b6a3/ulid/UlidCreator.java b/src/main/java/com/github/f4b6a3/ulid/UlidCreator.java index 6c215bf..f862d89 100644 --- a/src/main/java/com/github/f4b6a3/ulid/UlidCreator.java +++ b/src/main/java/com/github/f4b6a3/ulid/UlidCreator.java @@ -59,7 +59,8 @@ public final class UlidCreator { *
* The random component is reset for each new ULID generated. * - * @param time a number of milliseconds since 1970-01-01 (Unix epoch). + * @param time the current time in milliseconds, measured from the UNIX epoch of + * 1970-01-01T00:00Z (UTC) * @return a ULID */ public static Ulid getUlid(final long time) { @@ -84,7 +85,8 @@ public final class UlidCreator { * The random component is incremented for each new ULID generated in the same * millisecond. * - * @param time a number of milliseconds since 1970-01-01 (Unix epoch). + * @param time the current time in milliseconds, measured from the UNIX epoch of + * 1970-01-01T00:00Z (UTC) * @return a ULID */ public static Ulid getMonotonicUlid(final long time) { @@ -107,7 +109,8 @@ public final class UlidCreator { * Ulid ulid = UlidCreator.getHashUlid(time, name); * } * - * @param time a number of milliseconds since 1970-01-01 (Unix epoch). + * @param time the time in milliseconds, measured from the UNIX epoch of + * 1970-01-01T00:00Z (UTC) * @param string a string to be hashed using SHA-256 algorithm. * @return a ULID * @since 5.2.0 @@ -133,7 +136,8 @@ public final class UlidCreator { * Ulid ulid = UlidCreator.getHashUlid(time, bytes); * } * - * @param time a number of milliseconds since 1970-01-01 (Unix epoch). + * @param time the time in milliseconds, measured from the UNIX epoch of + * 1970-01-01T00:00Z (UTC) * @param bytes a byte array to be hashed using SHA-256 algorithm. * @return a ULID * @since 5.2.0 diff --git a/src/main/java/com/github/f4b6a3/ulid/UlidFactory.java b/src/main/java/com/github/f4b6a3/ulid/UlidFactory.java index 071dc36..9ec5e97 100644 --- a/src/main/java/com/github/f4b6a3/ulid/UlidFactory.java +++ b/src/main/java/com/github/f4b6a3/ulid/UlidFactory.java @@ -26,6 +26,7 @@ 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; import java.util.function.LongFunction; @@ -42,14 +43,14 @@ import java.util.function.LongSupplier; * Instances of this class can behave in one of two ways: monotonic or * non-monotonic (default). *
- * If the factory is monotonic, the random component is incremented by 1 If more + * If the factory is monotonic, the random component is incremented by 1 if more * than one ULID is generated within the same millisecond. *
* The maximum ULIDs that can be generated per millisecond is 2^80.
*/
public final class UlidFactory {
- private final LongSupplier timeMillisNow; // for tests
+ private final LongSupplier timeFunction;
private final LongFunction
- * It is equivalent to {@code new UlidFactory()}.
+ * It is equivalent to the default constructor {@code new UlidFactory()}.
*
* @return {@link UlidFactory}
*/
public static UlidFactory newInstance() {
- return new UlidFactory(new UlidFunction(IRandom.newInstance()));
+ return new UlidFactory(new UlidFunction());
}
/**
@@ -94,7 +100,7 @@ public final class UlidFactory {
* @return {@link UlidFactory}
*/
public static UlidFactory newInstance(Random random) {
- return new UlidFactory(new UlidFunction(IRandom.newInstance(random)));
+ return new UlidFactory(new UlidFunction(random));
}
/**
@@ -106,7 +112,7 @@ public final class UlidFactory {
* @return {@link UlidFactory}
*/
public static UlidFactory newInstance(LongSupplier randomFunction) {
- return new UlidFactory(new UlidFunction(IRandom.newInstance(randomFunction)));
+ return new UlidFactory(new UlidFunction(randomFunction));
}
/**
@@ -118,7 +124,7 @@ public final class UlidFactory {
* @return {@link UlidFactory}
*/
public static UlidFactory newInstance(IntFunction
- * The given random function must return a long value.
*
- * @param randomFunction a random function that returns a long value
- * @param timeMillisNow a function that returns the current time as
- * milliseconds from epoch
+ * @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}
*/
- public static UlidFactory newMonotonicInstance(LongSupplier randomFunction, LongSupplier timeMillisNow) {
- return new UlidFactory(new MonotonicFunction(IRandom.newInstance(randomFunction), timeMillisNow),
- timeMillisNow);
+ static UlidFactory newMonotonicInstance(Random random, Clock clock) {
+ Objects.requireNonNull(clock, "Clock instant must not be null");
+ return new UlidFactory(new MonotonicFunction(random), clock::millis);
}
/**
@@ -197,11 +190,14 @@ public final class UlidFactory {
* The given random function must return a long value.
*
* @param randomFunction a random function that returns a long value
- * @param clock a custom clock instance for tests
+ * @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) {
- return UlidFactory.newMonotonicInstance(randomFunction, clock::millis);
+ Objects.requireNonNull(clock, "Clock instant must not be null");
+ return new UlidFactory(new MonotonicFunction(randomFunction), clock::millis);
}
/**
@@ -210,26 +206,56 @@ public final class UlidFactory {
* The given random function must return a byte array.
*
* @param randomFunction a random function that returns a byte array
- * @param timeMillisNow a function that returns the current time as
- * milliseconds from epoch
- * @return {@link UlidFactory}
- */
- public static UlidFactory newMonotonicInstance(IntFunction
- * The given random function must return a byte array.
- *
- * @param randomFunction a random function that returns a byte array
- * @param clock a custom clock instance for tests
+ * @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
+ * The given random function must return a long value.
+ *
+ * @param randomFunction a random function that returns a long value
+ * @param timeFunction a function that returns the current time in
+ * milliseconds, measured from the UNIX epoch of
+ * 1970-01-01T00:00Z (UTC)
+ * @return {@link UlidFactory}
+ */
+ public static UlidFactory newMonotonicInstance(LongSupplier randomFunction, LongSupplier timeFunction) {
+ return new UlidFactory(new MonotonicFunction(randomFunction), timeFunction);
+ }
+
+ /**
+ * Returns a new monotonic factory.
+ *
+ * The given random function must return a byte array.
+ *
+ * @param randomFunction a random function that returns a byte array
+ * @param timeFunction a function that returns the current time in
+ * milliseconds, measured from the UNIX epoch of
+ * 1970-01-01T00:00Z (UTC)
+ * @return {@link UlidFactory}
+ */
+ public static UlidFactory newMonotonicInstance(IntFunction