From 483ba2f1607f44e5d114173c9aa6588b8f6a494f Mon Sep 17 00:00:00 2001 From: NthPortal Date: Sun, 23 Jul 2017 07:14:00 -0400 Subject: [PATCH] Improve conversions between versions Rename `to` to `toOptionOf`, and add `to` method which throws an exception instead of returning an Option. --- .../versions/ExtendedVersionBase.scala | 25 +++++++++++++--- .../com/nthportal/versions/VersionBase.scala | 22 +++++++++++++- .../versions/v2/ExtendedVersionTest.scala | 18 ++++++++--- .../nthportal/versions/v2/VersionTest.scala | 18 ++++++++--- .../versions/v3/ExtendedVersionTest.scala | 18 ++++++++--- .../nthportal/versions/v3/VersionTest.scala | 18 ++++++++--- .../versions/v4/ExtendedVersionTest.scala | 18 ++++++++--- .../nthportal/versions/v4/VersionTest.scala | 18 ++++++++--- .../variable/ExtendedOfSizeTest.scala | 24 +++++++++++++++ .../variable/ExtendedVersionTest.scala | 30 ++++++++++++++----- .../versions/variable/OfSizeTest.scala | 24 +++++++++++++++ .../versions/variable/VersionTest.scala | 30 ++++++++++++++----- 12 files changed, 218 insertions(+), 45 deletions(-) diff --git a/src/main/scala/com/nthportal/versions/ExtendedVersionBase.scala b/src/main/scala/com/nthportal/versions/ExtendedVersionBase.scala index cc00992..5211a65 100644 --- a/src/main/scala/com/nthportal/versions/ExtendedVersionBase.scala +++ b/src/main/scala/com/nthportal/versions/ExtendedVersionBase.scala @@ -30,13 +30,30 @@ trait ExtendedVersionBase[V <: VersionBase[V, EV], E, EV[X] <: ExtendedVersionBa * * @param companion a [[ExtendedVersionCompanion companion]] of the type * of version to which this should be converted + * @return this version converted to the other type, if it can be + * represented by the other type + * @throws IllegalArgumentException if this version cannot be converted + * to the other type + */ + @throws[IllegalArgumentException] + def to[V2 <: VersionBase[V2, EV2], EV2[X] <: ExtendedVersionBase[V2, X, EV2]] + (companion: ExtendedVersionCompanion[V2, EV2]): EV2[E] = { + if (companion eq version.extendedCompanion) this.asInstanceOf[EV2[E]] + else companion(version to companion.baseCompanion, extension, extensionDef) + } + + /** + * Converts this extended version to another type, if possible. + * + * @param companion a [[ExtendedVersionCompanion companion]] of the type + * of version to which this should be converted * @return an [[Option]] containing this version converted to * the other type, if it can be represented by the other type */ - def to[V2 <: VersionBase[V2, EV2], EV2[X] <: ExtendedVersionBase[V2, X, EV2]] + def toOptionOf[V2 <: VersionBase[V2, EV2], EV2[X] <: ExtendedVersionBase[V2, X, EV2]] (companion: ExtendedVersionCompanion[V2, EV2]): Option[EV2[E]] = { if (companion eq version.extendedCompanion) Some(this.asInstanceOf[EV2[E]]) - else companion.baseCompanion.versionFromSeq.lift(version.toSeq) map { companion(_, extension, extensionDef) } + else version toOptionOf companion.baseCompanion map { companion(_, extension, extensionDef) } } /** @@ -55,8 +72,8 @@ trait ExtendedVersionBase[V <: VersionBase[V, EV], E, EV[X] <: ExtendedVersionBa require(extensionDef.ordering == that.extensionDef.ordering, "cannot compare extended versions with different extension orderings") implicit val eOrd = extensionDef.ordering - (this.version compare that.version) thenCompare (this.extension, that.extension) + (this.version compare that.version) thenCompare(this.extension, that.extension) } - override def toString = s"$version${extensionDef.extToString(extension)}" + override def toString = s"$version${ extensionDef.extToString(extension) }" } diff --git a/src/main/scala/com/nthportal/versions/VersionBase.scala b/src/main/scala/com/nthportal/versions/VersionBase.scala index 04dcdd7..b2e7188 100644 --- a/src/main/scala/com/nthportal/versions/VersionBase.scala +++ b/src/main/scala/com/nthportal/versions/VersionBase.scala @@ -38,10 +38,30 @@ trait VersionBase[V <: VersionBase[V, EV], EV[E] <: ExtendedVersionBase[V, E, EV * * @param companion a [[VersionCompanion companion]] of the type of version * to which this should be converted + * @return this version converted to the other type, if it can be + * represented by the other type + * @throws IllegalArgumentException if this version cannot be converted + * to the other type + */ + @throws[IllegalArgumentException] + def to[V2 <: VersionBase[V2, EV2], EV2[E] <: ExtendedVersionBase[V2, E, EV2]] + (companion: VersionCompanion[V2, EV2]): V2 = { + if (companion eq this.companion) this.asInstanceOf[V2] + else { + companion.versionFromSeq.applyOrElse(toSeq, + (_: Seq[Int]) => throw new IllegalArgumentException(s"$this cannot be converted to $companion")) + } + } + + /** + * Converts this version to another type, if possible. + * + * @param companion a [[VersionCompanion companion]] of the type of version + * to which this should be converted * @return an [[Option]] containing this version converted to * the other type, if it can be represented by the other type */ - def to[V2 <: VersionBase[V2, EV2], EV2[E] <: ExtendedVersionBase[V2, E, EV2]] + def toOptionOf[V2 <: VersionBase[V2, EV2], EV2[E] <: ExtendedVersionBase[V2, E, EV2]] (companion: VersionCompanion[V2, EV2]): Option[V2] = { if (companion eq this.companion) Some(this.asInstanceOf[V2]) else companion.versionFromSeq.lift(toSeq) diff --git a/src/test/scala/com/nthportal/versions/v2/ExtendedVersionTest.scala b/src/test/scala/com/nthportal/versions/v2/ExtendedVersionTest.scala index 6e8a5be..b13fd45 100644 --- a/src/test/scala/com/nthportal/versions/v2/ExtendedVersionTest.scala +++ b/src/test/scala/com/nthportal/versions/v2/ExtendedVersionTest.scala @@ -80,10 +80,20 @@ class ExtendedVersionTest extends SimpleSpec { it should "convert to other types correctly" in { val ev = Version(1, 3) -- Snapshot - ev.to(ExtendedVersion).value shouldEqual ev - ev.to(variable.ExtendedVersion).value shouldEqual variable.Version(1, 3) -- Snapshot + ev to ExtendedVersion shouldEqual ev + ev to variable.ExtendedVersion shouldEqual variable.Version(1, 3) -- Snapshot - ev.to(v3.ExtendedVersion) shouldBe empty - ev.to(v4.ExtendedVersion) shouldBe empty + an [IllegalArgumentException] should be thrownBy { ev to v3.ExtendedVersion } + an [IllegalArgumentException] should be thrownBy { ev to v4.ExtendedVersion } + } + + it should "convert as an option to other types correctly" in { + val ev = Version(1, 3) -- Snapshot + + ev.toOptionOf(ExtendedVersion).value shouldEqual ev + ev.toOptionOf(variable.ExtendedVersion).value shouldEqual variable.Version(1, 3) -- Snapshot + + ev toOptionOf v3.ExtendedVersion shouldBe empty + ev toOptionOf v4.ExtendedVersion shouldBe empty } } diff --git a/src/test/scala/com/nthportal/versions/v2/VersionTest.scala b/src/test/scala/com/nthportal/versions/v2/VersionTest.scala index 761ba84..b8e6bc5 100644 --- a/src/test/scala/com/nthportal/versions/v2/VersionTest.scala +++ b/src/test/scala/com/nthportal/versions/v2/VersionTest.scala @@ -69,10 +69,20 @@ class VersionTest extends SimpleSpec { it should "convert to other types correctly" in { val v = Version(1, 3) - v.to(Version).value shouldEqual v - v.to(variable.Version).value shouldEqual variable.Version(1, 3) + v to Version shouldEqual v + v to variable.Version shouldEqual variable.Version(1, 3) - v.to(v3.Version) shouldBe empty - v.to(v4.Version) shouldBe empty + an [IllegalArgumentException] should be thrownBy { v to v3.Version } + an [IllegalArgumentException] should be thrownBy { v to v4.Version } + } + + it should "convert as an option to other types correctly" in { + val v = Version(1, 3) + + v.toOptionOf(Version).value shouldEqual v + v.toOptionOf(variable.Version).value shouldEqual variable.Version(1, 3) + + v toOptionOf v3.Version shouldBe empty + v toOptionOf v4.Version shouldBe empty } } diff --git a/src/test/scala/com/nthportal/versions/v3/ExtendedVersionTest.scala b/src/test/scala/com/nthportal/versions/v3/ExtendedVersionTest.scala index 9fe2b74..c05f1e2 100644 --- a/src/test/scala/com/nthportal/versions/v3/ExtendedVersionTest.scala +++ b/src/test/scala/com/nthportal/versions/v3/ExtendedVersionTest.scala @@ -80,10 +80,20 @@ class ExtendedVersionTest extends SimpleSpec { it should "convert to other types correctly" in { val ev = Version(1, 2, 5) -- Snapshot - ev.to(ExtendedVersion).value shouldEqual ev - ev.to(variable.ExtendedVersion).value shouldEqual variable.Version(1, 2, 5) -- Snapshot + ev to ExtendedVersion shouldEqual ev + ev to variable.ExtendedVersion shouldEqual variable.Version(1, 2, 5) -- Snapshot - ev.to(v2.ExtendedVersion) shouldBe empty - ev.to(v4.ExtendedVersion) shouldBe empty + an [IllegalArgumentException] should be thrownBy { ev to v2.ExtendedVersion } + an [IllegalArgumentException] should be thrownBy { ev to v4.ExtendedVersion } + } + + it should "convert as an option to other types correctly" in { + val ev = Version(1, 2, 5) -- Snapshot + + ev.toOptionOf(ExtendedVersion).value shouldEqual ev + ev.toOptionOf(variable.ExtendedVersion).value shouldEqual variable.Version(1, 2, 5) -- Snapshot + + ev toOptionOf v2.ExtendedVersion shouldBe empty + ev toOptionOf v4.ExtendedVersion shouldBe empty } } diff --git a/src/test/scala/com/nthportal/versions/v3/VersionTest.scala b/src/test/scala/com/nthportal/versions/v3/VersionTest.scala index 25cd82a..3a134f8 100644 --- a/src/test/scala/com/nthportal/versions/v3/VersionTest.scala +++ b/src/test/scala/com/nthportal/versions/v3/VersionTest.scala @@ -72,10 +72,20 @@ class VersionTest extends SimpleSpec { it should "convert to other types correctly" in { val v = Version(1, 2, 5) - v.to(Version).value shouldEqual v - v.to(variable.Version).value shouldEqual variable.Version(1, 2, 5) + v to Version shouldEqual v + v to variable.Version shouldEqual variable.Version(1, 2, 5) - v.to(v2.Version) shouldBe empty - v.to(v4.Version) shouldBe empty + an [IllegalArgumentException] should be thrownBy { v to v2.Version } + an [IllegalArgumentException] should be thrownBy { v to v4.Version } + } + + it should "convert as an option to other types correctly" in { + val v = Version(1, 2, 5) + + v.toOptionOf(Version).value shouldEqual v + v.toOptionOf(variable.Version).value shouldEqual variable.Version(1, 2, 5) + + v toOptionOf v2.Version shouldBe empty + v toOptionOf v4.Version shouldBe empty } } diff --git a/src/test/scala/com/nthportal/versions/v4/ExtendedVersionTest.scala b/src/test/scala/com/nthportal/versions/v4/ExtendedVersionTest.scala index a397abb..5db900c 100644 --- a/src/test/scala/com/nthportal/versions/v4/ExtendedVersionTest.scala +++ b/src/test/scala/com/nthportal/versions/v4/ExtendedVersionTest.scala @@ -80,10 +80,20 @@ class ExtendedVersionTest extends SimpleSpec { it should "convert to other types correctly" in { val ev = Version(1, 2, 5, 4) -- Snapshot - ev.to(ExtendedVersion).value shouldEqual ev - ev.to(variable.ExtendedVersion).value shouldEqual variable.Version(1, 2, 5, 4) -- Snapshot + ev to ExtendedVersion shouldEqual ev + ev to variable.ExtendedVersion shouldEqual variable.Version(1, 2, 5, 4) -- Snapshot - ev.to(v2.ExtendedVersion) shouldBe empty - ev.to(v3.ExtendedVersion) shouldBe empty + an [IllegalArgumentException] should be thrownBy { ev to v2.ExtendedVersion } + an [IllegalArgumentException] should be thrownBy { ev to v3.ExtendedVersion } + } + + it should "convert as an option to other types correctly" in { + val ev = Version(1, 2, 5, 4) -- Snapshot + + ev.toOptionOf(ExtendedVersion).value shouldEqual ev + ev.toOptionOf(variable.ExtendedVersion).value shouldEqual variable.Version(1, 2, 5, 4) -- Snapshot + + ev toOptionOf v2.ExtendedVersion shouldBe empty + ev toOptionOf v3.ExtendedVersion shouldBe empty } } diff --git a/src/test/scala/com/nthportal/versions/v4/VersionTest.scala b/src/test/scala/com/nthportal/versions/v4/VersionTest.scala index 5f5a1fa..0e16d8a 100644 --- a/src/test/scala/com/nthportal/versions/v4/VersionTest.scala +++ b/src/test/scala/com/nthportal/versions/v4/VersionTest.scala @@ -75,10 +75,20 @@ class VersionTest extends SimpleSpec { it should "convert to other types correctly" in { val v = Version(1, 2, 5, 4) - v.to(Version).value shouldEqual v - v.to(variable.Version).value shouldEqual variable.Version(1, 2, 5, 4) + v to Version shouldEqual v + v to variable.Version shouldEqual variable.Version(1, 2, 5, 4) - v.to(v2.Version) shouldBe empty - v.to(v3.Version) shouldBe empty + an [IllegalArgumentException] should be thrownBy { v to v2.Version } + an [IllegalArgumentException] should be thrownBy { v to v3.Version } + } + + it should "convert as an option to other types correctly" in { + val v = Version(1, 2, 5, 4) + + v.toOptionOf(Version).value shouldEqual v + v.toOptionOf(variable.Version).value shouldEqual variable.Version(1, 2, 5, 4) + + v toOptionOf v2.Version shouldBe empty + v toOptionOf v3.Version shouldBe empty } } diff --git a/src/test/scala/com/nthportal/versions/variable/ExtendedOfSizeTest.scala b/src/test/scala/com/nthportal/versions/variable/ExtendedOfSizeTest.scala index fc66808..30e8fb2 100644 --- a/src/test/scala/com/nthportal/versions/variable/ExtendedOfSizeTest.scala +++ b/src/test/scala/com/nthportal/versions/variable/ExtendedOfSizeTest.scala @@ -61,4 +61,28 @@ class ExtendedOfSizeTest extends SimpleSpec { EV unapply "1.2.5-SNAPSHOT" shouldBe empty EV unapply "1.0.0" shouldBe empty } + + it should "convert to other types correctly" in { + val ev1 = Version(1, 2, 5) -- Snapshot + + ev1 to ExtendedVersion.ofSize(2 to 3) shouldEqual ev1 + an [IllegalArgumentException] should be thrownBy { ev1 to ExtendedVersion.ofSize(1 to 2) } + + val ev2 = Version.ofSize(1 to 2)(1) -- Snapshot + + ev2 to ExtendedVersion shouldEqual ev2 + an [IllegalArgumentException] should be thrownBy { ev2 to ExtendedVersion.ofSize(2 to 3) } + } + + it should "convert as an option to other types correctly" in { + val v1 = Version(1, 2, 5) -- Snapshot + + v1.toOptionOf(ExtendedVersion.ofSize(2 to 3)).value shouldEqual v1 + v1 toOptionOf ExtendedVersion.ofSize(1 to 2) shouldBe empty + + val v2 = Version.ofSize(1 to 2)(1) -- Snapshot + + v2.toOptionOf(ExtendedVersion).value shouldEqual v2 + v2 toOptionOf ExtendedVersion.ofSize(2 to 3) shouldBe empty + } } diff --git a/src/test/scala/com/nthportal/versions/variable/ExtendedVersionTest.scala b/src/test/scala/com/nthportal/versions/variable/ExtendedVersionTest.scala index bea86d1..83468ee 100644 --- a/src/test/scala/com/nthportal/versions/variable/ExtendedVersionTest.scala +++ b/src/test/scala/com/nthportal/versions/variable/ExtendedVersionTest.scala @@ -77,16 +77,30 @@ class ExtendedVersionTest extends SimpleSpec { } it should "convert to other types correctly" in { - (Version(1, 2, 5, 4, 16) -- Snapshot).to(ExtendedVersion).value shouldEqual Version(1, 2, 5, 4, 16) -- Snapshot - (Version(1, 3) -- Snapshot).to(v2.ExtendedVersion).value shouldEqual v2.Version(1, 3) -- Snapshot - (Version(1, 2, 5) -- Snapshot).to(v3.ExtendedVersion).value shouldEqual v3.Version(1, 2, 5) -- Snapshot - (Version(1, 2, 5, 4) -- Snapshot).to(v4.ExtendedVersion).value shouldEqual v4.Version(1, 2, 5, 4) -- Snapshot + Version(1, 2, 5, 4, 16) -- Snapshot to ExtendedVersion shouldEqual Version(1, 2, 5, 4, 16) -- Snapshot + Version(1, 3) -- Snapshot to v2.ExtendedVersion shouldEqual v2.Version(1, 3) -- Snapshot + Version(1, 2, 5) -- Snapshot to v3.ExtendedVersion shouldEqual v3.Version(1, 2, 5) -- Snapshot + Version(1, 2, 5, 4) -- Snapshot to v4.ExtendedVersion shouldEqual v4.Version(1, 2, 5, 4) -- Snapshot val ev = Version(1) -- Snapshot - ev.to(ExtendedVersion).value shouldEqual ev - ev.to(v2.ExtendedVersion) shouldBe empty - ev.to(v3.ExtendedVersion) shouldBe empty - ev.to(v4.ExtendedVersion) shouldBe empty + ev to ExtendedVersion shouldEqual ev + an [IllegalArgumentException] should be thrownBy { ev to v2.ExtendedVersion } + an [IllegalArgumentException] should be thrownBy { ev to v3.ExtendedVersion } + an [IllegalArgumentException] should be thrownBy { ev to v4.ExtendedVersion } + } + + it should "convert as an option to other types correctly" in { + (Version(1, 2, 5, 4, 16) -- Snapshot).toOptionOf(ExtendedVersion).value shouldEqual Version(1, 2, 5, 4, 16) -- Snapshot + (Version(1, 3) -- Snapshot).toOptionOf(v2.ExtendedVersion).value shouldEqual v2.Version(1, 3) -- Snapshot + (Version(1, 2, 5) -- Snapshot).toOptionOf(v3.ExtendedVersion).value shouldEqual v3.Version(1, 2, 5) -- Snapshot + (Version(1, 2, 5, 4) -- Snapshot).toOptionOf(v4.ExtendedVersion).value shouldEqual v4.Version(1, 2, 5, 4) -- Snapshot + + val ev = Version(1) -- Snapshot + + ev.toOptionOf(ExtendedVersion).value shouldEqual ev + ev toOptionOf v2.ExtendedVersion shouldBe empty + ev toOptionOf v3.ExtendedVersion shouldBe empty + ev toOptionOf v4.ExtendedVersion shouldBe empty } } diff --git a/src/test/scala/com/nthportal/versions/variable/OfSizeTest.scala b/src/test/scala/com/nthportal/versions/variable/OfSizeTest.scala index f67d247..9a1653f 100644 --- a/src/test/scala/com/nthportal/versions/variable/OfSizeTest.scala +++ b/src/test/scala/com/nthportal/versions/variable/OfSizeTest.scala @@ -77,4 +77,28 @@ class OfSizeTest extends SimpleSpec { V unapplySeq "1.f" shouldBe empty V unapplySeq "really not a version" shouldBe empty } + + it should "convert to other types correctly" in { + val v1 = Version(1, 2, 5) + + v1 to Version.ofSize(2 to 3) shouldEqual v1 + an [IllegalArgumentException] should be thrownBy { v1 to Version.ofSize(1 to 2) } + + val v2 = Version.ofSize(1 to 2)(1) + + v2 to Version shouldEqual v2 + an [IllegalArgumentException] should be thrownBy { v2 to Version.ofSize(2 to 3) } + } + + it should "convert as an option to other types correctly" in { + val v1 = Version(1, 2, 5) + + v1.toOptionOf(Version.ofSize(2 to 3)).value shouldEqual v1 + v1 toOptionOf Version.ofSize(1 to 2) shouldBe empty + + val v2 = Version.ofSize(1 to 2)(1) + + v2.toOptionOf(Version).value shouldEqual v2 + v2 toOptionOf Version.ofSize(2 to 3) shouldBe empty + } } diff --git a/src/test/scala/com/nthportal/versions/variable/VersionTest.scala b/src/test/scala/com/nthportal/versions/variable/VersionTest.scala index b3f79c3..3826dbf 100644 --- a/src/test/scala/com/nthportal/versions/variable/VersionTest.scala +++ b/src/test/scala/com/nthportal/versions/variable/VersionTest.scala @@ -57,16 +57,30 @@ class VersionTest extends SimpleSpec { } it should "convert to other types correctly" in { - Version(1, 2, 5, 4, 16).to(Version).value shouldEqual Version(1, 2, 5, 4, 16) - Version(1, 3).to(v2.Version).value shouldEqual v2.Version(1, 3) - Version(1, 2, 5).to(v3.Version).value shouldEqual v3.Version(1, 2, 5) - Version(1, 2, 5, 4).to(v4.Version).value shouldEqual v4.Version(1, 2, 5, 4) + Version(1, 2, 5, 4, 16) to Version shouldEqual Version(1, 2, 5, 4, 16) + Version(1, 3) to v2.Version shouldEqual v2.Version(1, 3) + Version(1, 2, 5) to v3.Version shouldEqual v3.Version(1, 2, 5) + Version(1, 2, 5, 4) to v4.Version shouldEqual v4.Version(1, 2, 5, 4) val v = Version(1) - v.to(Version).value shouldEqual v - v.to(v2.Version) shouldBe empty - v.to(v3.Version) shouldBe empty - v.to(v4.Version) shouldBe empty + v to Version shouldEqual v + an [IllegalArgumentException] should be thrownBy { v to v2.Version } + an [IllegalArgumentException] should be thrownBy { v to v3.Version } + an [IllegalArgumentException] should be thrownBy { v to v4.Version } + } + + it should "convert as an option to other types correctly" in { + Version(1, 2, 5, 4, 16).toOptionOf(Version).value shouldEqual Version(1, 2, 5, 4, 16) + Version(1, 3).toOptionOf(v2.Version).value shouldEqual v2.Version(1, 3) + Version(1, 2, 5).toOptionOf(v3.Version).value shouldEqual v3.Version(1, 2, 5) + Version(1, 2, 5, 4).toOptionOf(v4.Version).value shouldEqual v4.Version(1, 2, 5, 4) + + val v = Version(1) + + v.toOptionOf(Version).value shouldEqual v + v toOptionOf v2.Version shouldBe empty + v toOptionOf v3.Version shouldBe empty + v toOptionOf v4.Version shouldBe empty } }