diff options
author | Stewart Stewart <stewinsalot@gmail.com> | 2016-12-19 22:37:31 -0500 |
---|---|---|
committer | Stewart Stewart <stewinsalot@gmail.com> | 2016-12-19 22:37:31 -0500 |
commit | 2555bc7b3bc5b6f9057c85217ae5e1cfaa2e77c0 (patch) | |
tree | 2fe616b99dcb0785d5959258f97fe221ac4479ec /src | |
parent | e0a4590247199fb4f9ad49ea6520f104b2622c51 (diff) | |
parent | dd8cbb3ada14a4f05d5683b90a5b83dc4b3b35f5 (diff) | |
download | driver-core-2555bc7b3bc5b6f9057c85217ae5e1cfaa2e77c0.tar.gz driver-core-2555bc7b3bc5b6f9057c85217ae5e1cfaa2e77c0.tar.bz2 driver-core-2555bc7b3bc5b6f9057c85217ae5e1cfaa2e77c0.zip |
Merge branch 'master' into dal-improvements
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/xyz/driver/core/core.scala | 4 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/core/time.scala | 11 | ||||
-rw-r--r-- | src/test/scala/xyz/driver/core/CoreTest.scala | 21 |
3 files changed, 32 insertions, 4 deletions
diff --git a/src/main/scala/xyz/driver/core/core.scala b/src/main/scala/xyz/driver/core/core.scala index 2b61689..8c13aeb 100644 --- a/src/main/scala/xyz/driver/core/core.scala +++ b/src/main/scala/xyz/driver/core/core.scala @@ -37,6 +37,10 @@ package core { object Id { implicit def idEqual[T]: Equal[Id[T]] = Equal.equal[Id[T]](_ == _) implicit def idOrdering[T]: Ordering[Id[T]] = Ordering.by[Id[T], String](_.value) + + object Mapper { + def apply[A, B]: (Id[A] => Id[B]) = (id: Id[A]) => Id[B](id.value) + } } final case class Name[+Tag](value: String) extends AnyVal { diff --git a/src/main/scala/xyz/driver/core/time.scala b/src/main/scala/xyz/driver/core/time.scala index cdeb3a6..5dc16a0 100644 --- a/src/main/scala/xyz/driver/core/time.scala +++ b/src/main/scala/xyz/driver/core/time.scala @@ -21,9 +21,9 @@ object time { final case class Time(millis: Long) extends AnyVal { - def isBefore(anotherTime: Time): Boolean = millis < anotherTime.millis + def isBefore(anotherTime: Time): Boolean = implicitly[Ordering[Time]].lt(this, anotherTime) - def isAfter(anotherTime: Time): Boolean = millis > anotherTime.millis + def isAfter(anotherTime: Time): Boolean = implicitly[Ordering[Time]].gt(this, anotherTime) def advanceBy(duration: Duration): Time = Time(millis + duration.toMillis) @@ -34,12 +34,15 @@ object time { } } + object Time { + + implicit def timeOrdering: Ordering[Time] = Ordering.by(_.millis) + } + final case class TimeRange(start: Time, end: Time) { def duration: Duration = FiniteDuration(end.millis - start.millis, MILLISECONDS) } - implicit def timeOrdering: Ordering[Time] = Ordering.by(_.millis) - def startOfMonth(time: Time) = { Time(make(new GregorianCalendar()) { cal => cal.setTime(new Date(time.millis)) diff --git a/src/test/scala/xyz/driver/core/CoreTest.scala b/src/test/scala/xyz/driver/core/CoreTest.scala index 3eb9eaa..da9fd9a 100644 --- a/src/test/scala/xyz/driver/core/CoreTest.scala +++ b/src/test/scala/xyz/driver/core/CoreTest.scala @@ -39,6 +39,27 @@ class CoreTest extends FlatSpec with Matchers with MockitoSugar { theSameElementsInOrderAs(Seq(Id[String]("1"), Id[String]("2"), Id[String]("3"), Id[String]("4"))) } + it should "have type-safe conversions" in { + final case class X(id: Id[X]) + final case class Y(id: Id[Y]) + final case class Z(id: Id[Z]) + + implicit val xy = Id.Mapper[X, Y] + implicit val yx = Id.Mapper[Y, X] + implicit val yz = Id.Mapper[Y, Z] + implicit val zy = Id.Mapper[Z, Y] + + // The real test is that the following statements compile: + val x = X(Id("0")) + val y = Y(x.id) + val z = Z(y.id) + val y2 = Y(z.id) + val x2 = X(y2.id) + + (x2 === x) should be(true) + (y2 === y) should be(true) + } + "Name" should "have equality and ordering working correctly" in { (Name[String]("foo") === Name[String]("foo")) should be(true) |