Skip to content

Instantly share code, notes, and snippets.

@andyczerwonka
Forked from carymrobbins/UUID.scala
Created May 18, 2022 04:32
Show Gist options
  • Save andyczerwonka/bb4451169f2153556a4ec5c9d289cb5c to your computer and use it in GitHub Desktop.
Save andyczerwonka/bb4451169f2153556a4ec5c9d289cb5c to your computer and use it in GitHub Desktop.
import scala.util.Try
/**
* Type-safe UUID wrapper that encodes the type it relates to. This class explicitly hides its
* UUID value to prevent converting it between types.
*/
class UUID[A] private(private val uuid: UUID.Raw) {
override def toString = uuid.toString
}
object UUID {
type Raw = java.util.UUID
def fromRaw[A](uuid: Raw): UUID[A] = new UUID(uuid)
def gen[A](): UUID[A] = fromRaw(java.util.UUID.randomUUID())
def genRaw(): java.util.UUID = java.util.UUID.randomUUID()
def fromString[A](s: String): Either[String, UUID[A]] = fromStringRaw(s).right.map(fromRaw)
def fromStringRaw(s: String): Either[String, java.util.UUID] = {
Try { java.util.UUID.fromString(s) }.toOption.toRight(s"Invalid UUID string: $s")
}
def unsafeFromString[A](s: String): UUID[A] = fromRaw(unsafeFromStringRaw(s))
def unsafeFromStringRaw(s: String): Raw = fromStringRaw(s).fold({ e => throw new RuntimeException(e) }, identity)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment