Skip to content

Commit

Permalink
32932 Add uuidv7 support
Browse files Browse the repository at this point in the history
Added utility class UUIDHelper
  • Loading branch information
cgendreau committed Feb 14, 2024
1 parent 5660b18 commit 2e15e35
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
12 changes: 9 additions & 3 deletions dina-base-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<postgresql.version>42.4.3</postgresql.version>
<jsoup.version>1.15.3</jsoup.version>
<commons-io.version>2.15.1</commons-io.version>
<java-uuid-generator.version>4.3.0</java-uuid-generator.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -152,9 +153,14 @@
<artifactId>querydsl-core</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>${java-uuid-generator.version}</version>
</dependency>

<!-- Spring mybatis -->
Expand Down
32 changes: 32 additions & 0 deletions dina-base-api/src/main/java/ca/gc/aafc/dina/util/UUIDHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ca.gc.aafc.dina.util;

import java.util.UUID;

import com.fasterxml.uuid.Generators;

/**
* Helper class to handle UUID version 7.
*/
public final class UUIDHelper {

private UUIDHelper() {
// utility class
}

public static UUID generateUUIDv7() {
return Generators.timeBasedEpochGenerator().generate();
}

/**
* Checks if the provided UUID is of version 7.
* @param uuid
* @return is provided UUID version 7. If uuid is null false is returned.
*/
public static boolean isUUIDv7(UUID uuid) {
if(uuid == null) {
return false;
}
return uuid.version() == 7;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ca.gc.aafc.dina.util;

import java.util.UUID;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class UUIDHelperTest {

@Test
public void testUUIDHelper() {
UUID uuidv7 = UUIDHelper.generateUUIDv7();

assertTrue(UUIDHelper.isUUIDv7(uuidv7));
assertFalse(UUIDHelper.isUUIDv7(UUID.randomUUID()));
}
}

0 comments on commit 2e15e35

Please sign in to comment.