-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added utility class UUIDHelper
- Loading branch information
Showing
3 changed files
with
60 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
dina-base-api/src/main/java/ca/gc/aafc/dina/util/UUIDHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
dina-base-api/src/test/java/ca/gc/aafc/dina/util/UUIDHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |