Skip to content

Instantly share code, notes, and snippets.

@jmcardon
Created August 14, 2018 23:45
Show Gist options
  • Save jmcardon/a072ebeeb7c6d7c7a8fbec670700aa56 to your computer and use it in GitHub Desktop.
Save jmcardon/a072ebeeb7c6d7c7a8fbec670700aa56 to your computer and use it in GitHub Desktop.
package io.chrisdavenport
import cats._
import cats.implicits._
import cats.effect.{IO, Sync}
import java.util.UUID
abstract class FUUIDModule {
type FUUID
def fromUUID(f: UUID): FUUID
def toUUID(f: FUUID): UUID
def fromString(s: String): Option[FUUID]
def randomFUUID[F[_]: Sync]: F[FUUID]
}
private[chrisdavenport] object FUUIDImpl extends FUUIDModule {
override type FUUID = UUID
override def fromUUID(f: UUID): UUID = f
override def toUUID(f: UUID): UUID = f
override def fromString(s: String): Option[UUID] =
try {
Some(UUID.fromString(s))
} catch {
case e: Throwable if !e.isInstanceOf[VirtualMachineError] => None
}
override def randomFUUID[F[_]: Sync]: F[UUID] = Sync[F].delay(fromUUID(UUID.randomUUID))
}
object fuuid {
val FUUID: FUUIDModule = FUUIDImpl
type FUUID = FUUID.FUUID
implicit val instancesFUUID: Hash[FUUID] with Order[FUUID] with Show[FUUID] =
new Hash[FUUID] with Order[FUUID] with Show[FUUID] {
override def show(t: FUUID): String = FUUID.toUUID(t).toString
override def eqv(x: FUUID, y: FUUID): Boolean =
FUUID.toUUID(x).equals(FUUID.toUUID(y))
override def hash(x: FUUID): Int = FUUID.toUUID(x).hashCode()
override def compare(x: FUUID, y: FUUID): Int =
FUUID.toUUID(x).compareTo(FUUID.toUUID(y))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment