From 21442408e5326e6db61e080fd5972c6c471e4df6 Mon Sep 17 00:00:00 2001 From: struga Date: Mon, 30 Oct 2023 14:15:31 +0300 Subject: [PATCH] Add local id gen (#72) --- pom.xml | 2 +- .../flow/lib/service/LocalIdGenerator.java | 18 ++++++++++++++ .../lib/service/LocalIdGeneratorTest.java | 24 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/main/java/dev/vality/adapter/flow/lib/service/LocalIdGenerator.java create mode 100644 src/test/java/dev/vality/adapter/flow/lib/service/LocalIdGeneratorTest.java diff --git a/pom.xml b/pom.xml index 9d6cb99..2638d27 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ adapter-flow-lib - 0.2.1 + 0.2.2 jar adapter-flow-lib diff --git a/src/main/java/dev/vality/adapter/flow/lib/service/LocalIdGenerator.java b/src/main/java/dev/vality/adapter/flow/lib/service/LocalIdGenerator.java new file mode 100644 index 0000000..4f76c7f --- /dev/null +++ b/src/main/java/dev/vality/adapter/flow/lib/service/LocalIdGenerator.java @@ -0,0 +1,18 @@ +package dev.vality.adapter.flow.lib.service; + +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; + +import java.nio.charset.StandardCharsets; +import java.util.UUID; + +@RequiredArgsConstructor +public class LocalIdGenerator implements IdGenerator { + + @SneakyThrows + @Override + public Long get(String invoiceId) { + return UUID.nameUUIDFromBytes(invoiceId.getBytes(StandardCharsets.UTF_8)).getMostSignificantBits() & + Long.MAX_VALUE; + } +} diff --git a/src/test/java/dev/vality/adapter/flow/lib/service/LocalIdGeneratorTest.java b/src/test/java/dev/vality/adapter/flow/lib/service/LocalIdGeneratorTest.java new file mode 100644 index 0000000..56e3144 --- /dev/null +++ b/src/test/java/dev/vality/adapter/flow/lib/service/LocalIdGeneratorTest.java @@ -0,0 +1,24 @@ +package dev.vality.adapter.flow.lib.service; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class LocalIdGeneratorTest { + + public static final String TEST = "test"; + public static final String TEST_2 = "test_2"; + + @Test + void get() { + LocalIdGenerator localIdGenerator = new LocalIdGenerator(); + + Long idTest1 = localIdGenerator.get(TEST); + Long idTest2 = localIdGenerator.get(TEST); + + Assertions.assertEquals(idTest1, idTest2); + + Long idTest3 = localIdGenerator.get(TEST_2); + + Assertions.assertNotEquals(idTest1, idTest3); + } +} \ No newline at end of file