Skip to content

Instantly share code, notes, and snippets.

@goldobin
Last active October 8, 2015 15:31
Show Gist options
  • Save goldobin/a3253f117b471202b1b3 to your computer and use it in GitHub Desktop.
Save goldobin/a3253f117b471202b1b3 to your computer and use it in GitHub Desktop.
import java.util.UUID
import java.nio.ByteBuffer
object UuidConverters {
implicit class BigIntToUuid(v: BigInt) {
def toUuid: UUID = {
val bb: ByteBuffer = ByteBuffer.wrap(v.toByteArray)
val high: Long = bb.getLong
val low: Long = bb.getLong
new UUID(high, low)
}
}
implicit class UuidToBigInt(v: UUID) {
def toBigInt: BigInt = {
val bb = ByteBuffer.allocate(16)
bb.putLong(v.getMostSignificantBits)
bb.putLong(v.getLeastSignificantBits)
BigInt(bb.array)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment