aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuicommon/domain/Id.scala
blob: e2382458d5b5db06aa7fc1d3bbdd4088baff1ff0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package xyz.driver.pdsuicommon.domain

import java.util.UUID

import xyz.driver.pdsuicommon.logging._

sealed trait Id[+T]

final case class CompoundId[Id1 <: Id[_], Id2 <: Id[_]](part1: Id1, part2: Id2) extends Id[(Id1, Id2)]

final case class LongId[+T](id: Long) extends Id[T] {
  override def toString: String = id.toString

  def is(longId: Long): Boolean = {
    id == longId
  }
}

object LongId {
  implicit def toPhiString[T](x: LongId[T]): PhiString = Unsafe(s"LongId(${x.id})")
}

final case class StringId[+T](id: String) extends Id[T] {
  override def toString: String = id

  def is(stringId: String): Boolean = {
    id == stringId
  }
}

object StringId {
  implicit def toPhiString[T](x: StringId[T]): PhiString = Unsafe(s"StringId(${x.id})")
}

final case class UuidId[+T](id: UUID) extends Id[T] {
  override def toString: String = id.toString
}

object UuidId {

  /**
    * @note May fail, if `string` is invalid UUID.
    */
  def apply[T](string: String): UuidId[T] = new UuidId[T](UUID.fromString(string))

  def apply[T](): UuidId[T] = new UuidId[T](UUID.randomUUID())

  implicit def ordering[T] = Ordering.by[UuidId[T], String](_.toString)

  implicit def toPhiString[T](x: UuidId[T]): PhiString = Unsafe(s"UuidId(${x.id})")
}