Skip to content

Commit

Permalink
Refactor UUIDv7 factory method with an instant parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiolimace committed Jul 7, 2024
1 parent d68abc9 commit 02d6c28
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
8 changes: 1 addition & 7 deletions src/main/java/com/github/f4b6a3/uuid/UuidCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package com.github.f4b6a3.uuid;

import java.security.SecureRandom;
import java.time.Instant;
import java.util.Objects;
import java.util.UUID;
Expand Down Expand Up @@ -605,12 +604,7 @@ public static UUID getTimeOrderedEpochPlusN() {
* @since 5.3.3
*/
public static UUID getTimeOrderedEpoch(Instant instant) {
Objects.requireNonNull(instant, "Null instant");
final long time = instant.toEpochMilli();
SecureRandom random = new SecureRandom();
final long msb = (time << 16) | (random.nextLong() & 0x0fffL) | 0x7000L;
final long lsb = (random.nextLong() & 0x3fffffffffffffffL) | 0x8000000000000000L;
return new UUID(msb, lsb);
return UUID7.create(Parameters.builder().withInstant(instant).build());
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/github/f4b6a3/uuid/factory/UuidFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.github.f4b6a3.uuid.factory;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Arrays;
import java.util.Objects;
import java.util.UUID;
Expand Down Expand Up @@ -100,6 +101,11 @@ public UuidVersion getVersion() {
*/
public static class Parameters {

/**
* Instant to be used.
*/
private final Instant instant;

/**
* Name space byte array.
*/
Expand Down Expand Up @@ -127,12 +133,17 @@ public static class Parameters {
*/
public Parameters(Builder builder) {
Objects.requireNonNull(builder);
this.instant = builder.instant;
this.namespace = builder.namespace;
this.name = builder.name;
this.localDomain = builder.localDomain;
this.localIdentifier = builder.localIdentifier;
}

public Instant getInstant() {
return this.instant;
}

/**
* Get the name space bytes.
*
Expand Down Expand Up @@ -183,6 +194,11 @@ public static Builder builder() {
*/
public static class Builder {

/**
* Instant to be used.
*/
private Instant instant;

/**
* Name space byte array.
*/
Expand All @@ -206,6 +222,17 @@ public static class Builder {
private Builder() {
}

/**
* Use the instant provided.
*
* @param instant an instant
* @return the builder
*/
public Builder withInstant(Instant instant) {
this.instant = instant;
return this;
}

/**
* Use the name space UUID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
package com.github.f4b6a3.uuid.factory.standard;

import java.time.Clock;
import java.time.Instant;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.function.LongSupplier;
import java.util.function.Supplier;

import com.github.f4b6a3.uuid.enums.UuidVersion;
import com.github.f4b6a3.uuid.factory.AbstCombFactory;
Expand Down Expand Up @@ -251,11 +253,26 @@ public static Builder builder() {
*/
@Override
public UUID create() {
UUID uuid = this.uuidFunction.get();
UUID uuid = this.uuidFunction.apply(null);
return toUuid(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
}

static abstract class UuidFunction implements Supplier<UUID> {
/**
* Returns a time-ordered unique identifier (UUIDv7) for a given instant.
* <p>
* The random component is generated with each method invocation.
*
* @return a UUIDv7
* @param instant a given instant
*/
@Override
public UUID create(Parameters parameters) {
Objects.requireNonNull(parameters.getInstant(), "Null instant");
UUID uuid = this.uuidFunction.apply(parameters.getInstant());
return toUuid(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
}

static abstract class UuidFunction implements Function<Instant, UUID> {

protected long msb = 0L; // most significant bits
protected long lsb = 0L; // least significant bits
Expand All @@ -276,10 +293,16 @@ public UuidFunction(IRandom random, LongSupplier timeFunction) {
}

@Override
public UUID get() {
public UUID apply(Instant instant) {
lock.lock();
try {

if (instant != null) {
// The user provided the time.
reset(instant.toEpochMilli());
return new UUID(this.msb, this.lsb);
}

final long lastTime = this.time();
final long time = timeFunction.getAsLong();

Expand Down

0 comments on commit 02d6c28

Please sign in to comment.