Skip to content

Instantly share code, notes, and snippets.

@Zuchos
Created September 14, 2018 12:29
Show Gist options
  • Save Zuchos/726f8063385b6d4256d93b01a3c70b59 to your computer and use it in GitHub Desktop.
Save Zuchos/726f8063385b6d4256d93b01a3c70b59 to your computer and use it in GitHub Desktop.
//create instance of it
val generator:StringIdGenerator = PrettyIdGenerator.singleNode
//generate ids
val stringId = generator.nextId()
stringId shouldNot be(empty)
stringId should fullyMatch regex """[A-Z]{4}-[0-9]{5}-[A-Z]{4}-[0-9]{5}"""
//or it might be used just for encoding existing ids
val prettifier = IdPrettifier.default
val id = prettifier.prettify(100L) //id = AAAA-00000-AAAA-01007
id should be("AAAA-00000-AAAA-01007")
//get seed
val origin = prettifier.toIdSeed(id) // 100L
origin should be(Right(100L))
//use custom prettifier
val customPrettifier = IdPrettifier.custom(encoder = new AlphabetCodec(new Alphabet("ABC")), partsSize = 4, delimiter = '_', leadingZeros = false)
val customId = customPrettifier.prettify(1234567L) //BCAACAB_5671
//construct custom PrettyIdGenerator
val idGenerator:IdGenerator = new IdGenerator {
override def nextId(): Long = ???
override def idBaseAt(timestamp: Long): Long = ???
}
val customGenerator = new PrettyIdGenerator(idGenerator, customPrettifier)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment