Generate reproducible UUIDs based of a sequence of numbers, with the possibility of generating sub UUIDs from the parent UUID.
In your build.sbt add:
libraryDependencies += "io.github.bcarter97" %% "id-generator" % "x.x.x"Then you can use the Generator like so:
import io.github.bcarter97.Generator
val generator = Generator()
generator.primaryId(5)
// PrimaryId(index=5, uuid=e4da3b7f-bbce-3345-9777-2b0674a318d5, numSubIds=1)As well as using generator.id(n) as above, there are a number of ways to generate a PrimaryId.
To sample a random PrimaryId (starting from the first index), use sample. This will automatically increment the sample index until the specified maxIndex is reached, at which point it will loop back around.
val generator = Generator(2)
val id1 = generator.sample()
// PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9)
val id2 = generator.sample()
// PrimaryId(index=2, uuid=c81e728d-9d4c-3f63-af06-7f89cc14862c, numSubIds=9)
val id3 = generator.sample()
// PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9) <-- same as id1💡 Using
id()without an index will call the underlyingsamplemethod.
You can sample a range of PrimaryIds by passing a number to sample:
val ids = generator.sample(2)
// Vector(PrimaryId(index=0, uuid=cfcd2084-95d5-35ef-a6e7-dff9f98764da, numSubIds=9), PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9))Similarly, you can specify the range of UUIDs to generate, to make the list reproducible:
val ids = generator.ids(5, 8)
// Vector(PrimaryId(index=5, uuid=e4da3b7f-bbce-3345-9777-2b0674a318d5, numSubIds=1), PrimaryId(index=6, uuid=1679091c-5a88-3faf-afb5-e6087eb1b2dc, numSubIds=9), PrimaryId(index=7, uuid=8f14e45f-ceea-367a-9a36-dedd4bea2543, numSubIds=6))SubIds are derived from a PrimaryId. Each PrimaryId has a random, reproducible number of SubIds. Every generated SubId contains the PrimaryId that generated it.
To get a list of subIds from a parentId:
val id = generator.sample()
val subIds = id.subIds
// Vector(SubId(index=1, uuid=6512bd43-d9ca-36e0-ac99-0b0a82652dca, primaryId=PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9)) ... )Any one of these subIds can find its parentId.
val id = generator.sample()
// PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9)
val subId = id.subIds.head
// SubId(index=1, uuid=6512bd43-d9ca-36e0-ac99-0b0a82652dca, primaryId=PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9))
val originalId = generator.subIdFromUuid(subId.uuid)
// Some(SubId(index=1, uuid=6512bd43-d9ca-36e0-ac99-0b0a82652dca, primaryId=PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9)))