aboutsummaryrefslogtreecommitdiff
path: root/basetypes
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-01-07 15:57:48 +0100
committerLinnea Gräf <nea@nea.moe>2025-01-07 15:57:48 +0100
commitdc19995f2b11ba595775b7224df200e365e7c4bf (patch)
tree0baee487f75db329c73e9125b030f0b6d5dd741b /basetypes
parent0ee8a4fdfedf20d543f42ab52a2f24af089271ac (diff)
downloadLocalTransactionLedger-dc19995f2b11ba595775b7224df200e365e7c4bf.tar.gz
LocalTransactionLedger-dc19995f2b11ba595775b7224df200e365e7c4bf.tar.bz2
LocalTransactionLedger-dc19995f2b11ba595775b7224df200e365e7c4bf.zip
refactor: Extract database to its own module
Diffstat (limited to 'basetypes')
-rw-r--r--basetypes/build.gradle.kts12
-rw-r--r--basetypes/src/main/kotlin/moe/nea/ledger/utils/ULIDWrapper.kt27
-rw-r--r--basetypes/src/main/kotlin/moe/nea/ledger/utils/UUIDUtil.kt41
3 files changed, 80 insertions, 0 deletions
diff --git a/basetypes/build.gradle.kts b/basetypes/build.gradle.kts
new file mode 100644
index 0000000..f4b1a8b
--- /dev/null
+++ b/basetypes/build.gradle.kts
@@ -0,0 +1,12 @@
+plugins {
+ `java-library`
+ kotlin("jvm")
+}
+
+java {
+ toolchain.languageVersion.set(JavaLanguageVersion.of(8))
+}
+
+dependencies {
+ implementation("io.azam.ulidj:ulidj:1.0.4")
+}
diff --git a/basetypes/src/main/kotlin/moe/nea/ledger/utils/ULIDWrapper.kt b/basetypes/src/main/kotlin/moe/nea/ledger/utils/ULIDWrapper.kt
new file mode 100644
index 0000000..b8c5d3b
--- /dev/null
+++ b/basetypes/src/main/kotlin/moe/nea/ledger/utils/ULIDWrapper.kt
@@ -0,0 +1,27 @@
+package moe.nea.ledger.utils
+
+import io.azam.ulidj.ULID
+import java.time.Instant
+import kotlin.random.Random
+
+@JvmInline
+value class ULIDWrapper(
+ val wrapped: String
+) {
+ companion object {
+ fun createULIDAt(timestamp: Instant): ULIDWrapper {
+ return ULIDWrapper(ULID.generate(
+ timestamp.toEpochMilli(),
+ Random.nextBytes(10)
+ ))
+ }
+ }
+
+ fun getTimestamp(): Instant {
+ return Instant.ofEpochMilli(ULID.getTimestamp(wrapped))
+ }
+
+ init {
+ require(ULID.isValid(wrapped))
+ }
+} \ No newline at end of file
diff --git a/basetypes/src/main/kotlin/moe/nea/ledger/utils/UUIDUtil.kt b/basetypes/src/main/kotlin/moe/nea/ledger/utils/UUIDUtil.kt
new file mode 100644
index 0000000..92a29f7
--- /dev/null
+++ b/basetypes/src/main/kotlin/moe/nea/ledger/utils/UUIDUtil.kt
@@ -0,0 +1,41 @@
+package moe.nea.ledger.utils
+
+import java.util.UUID
+import kotlin.uuid.ExperimentalUuidApi
+import kotlin.uuid.Uuid
+import kotlin.uuid.toJavaUuid
+
+object UUIDUtil {
+ @OptIn(ExperimentalUuidApi::class)
+ fun parsePotentiallyDashlessUUID(str: String): UUID {
+ val bytes = ByteArray(16)
+ var i = -1
+ var bi = 0
+ while (++i < str.length) {
+ val char = str[i]
+ if (char == '-') {
+ if (bi != 4 && bi != 6 && bi != 8 && bi != 10) {
+ error("Unexpected dash in UUID: $str")
+ }
+ continue
+ }
+ val current = parseHexDigit(str, char)
+ ++i
+ if (i >= str.length)
+ error("Unexpectedly short UUID: $str")
+ val next = parseHexDigit(str, str[i])
+ bytes[bi++] = (current * 16 or next).toByte()
+ }
+ if (bi != 16)
+ error("Unexpectedly short UUID: $str")
+ return Uuid.fromByteArray(bytes).toJavaUuid()
+ }
+
+ private fun parseHexDigit(str: String, char: Char): Int {
+ val d = char - '0'
+ if (d < 10) return d
+ val h = char - 'a'
+ if (h < 6) return 10 + h
+ error("Unexpected hex digit $char in UUID: $str")
+ }
+} \ No newline at end of file