
What?
ULID implementation in Kotin.
See kDoc to see the specification.
Install
Kotlin DSL
Add maven repository.
repositories {
mavenCentral()
}
If you want to use snapshots, add another repository.
repositories {
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
}
Add dependency.
implementation("io.github.reonaore:ulidk:0.1.0")
Usage
To generate a ULID, call randomULID().
import io.onare.ulidk.ULID
val ulid: ULID = ULID.randomULID()
val ulidString = ulid.toString()
val ulidBinary = ulid.binary
Decode from string
import io.onare.ulidk.ULID
val ulid: ULID = ULID.fromString("01ARZ3NDEKTSV4RRFFQ69G5FAV").getOrThrow()
val timestamp: Long = ulid.timestamp()
val entropy: ByteArray = ulid.entropy()
Monotonic ULIDs
import io.onare.ulidk.ULID
val ulidGen = ULID.MonotonicGenerator(ULID.randomULID(150000))
ulidGen()
ulidGen()
ulidGen()
ulidGen()
ulidGen()
ulidGen(100000)
UUID compatibility
import io.onare.ulidk.ULID
import java.util.*
val uuid = UUID.randomUUID()
val ulid = ULID.fromUUID(uuid)
assert(uuid.toString() == ulid.toUUID().toString())
Test
./gradlew test
Benchmark
./gradlew benchmark
result
on MacBook Air M1 2020, 8GB memory
Benchmark Mode Cnt Score Error Units
TestBenchmark.decodeThroughput thrpt 5 2870482.675 ± 28929.819 ops/s
TestBenchmark.generationThroughput thrpt 5 2222172.000 ± 337147.404 ops/s
TestBenchmark.monoGenerationThroughput thrpt 5 7301193.431 ± 2717363.333 ops/s
TestBenchmark.uuidGenerationThroughput thrpt 5 1997510.100 ± 2425175.330 ops/s
TestBenchmark.decodeAverage avgt 5 526.944 ± 926.595 ns/op
TestBenchmark.generationAverage avgt 5 550.906 ± 568.759 ns/op
TestBenchmark.monoGenerationAverage avgt 5 33.213 ± 1.528 ns/op
TestBenchmark.uuidGenerationAverage avgt 5 337.830 ± 91.290 ns/op