Skip to content
This repository has been archived by the owner on Oct 10, 2021. It is now read-only.

Commit

Permalink
Improve conversions between versions
Browse files Browse the repository at this point in the history
Rename `to` to `toOptionOf`, and add `to` method
which throws an exception instead of returning an Option.
  • Loading branch information
NthPortal committed Jul 23, 2017
1 parent ced1cb4 commit 483ba2f
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 45 deletions.
25 changes: 21 additions & 4 deletions src/main/scala/com/nthportal/versions/ExtendedVersionBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}

/**
Expand All @@ -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) }"
}
22 changes: 21 additions & 1 deletion src/main/scala/com/nthportal/versions/VersionBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 14 additions & 4 deletions src/test/scala/com/nthportal/versions/v2/ExtendedVersionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
18 changes: 14 additions & 4 deletions src/test/scala/com/nthportal/versions/v2/VersionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
18 changes: 14 additions & 4 deletions src/test/scala/com/nthportal/versions/v3/ExtendedVersionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
18 changes: 14 additions & 4 deletions src/test/scala/com/nthportal/versions/v3/VersionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
18 changes: 14 additions & 4 deletions src/test/scala/com/nthportal/versions/v4/ExtendedVersionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
18 changes: 14 additions & 4 deletions src/test/scala/com/nthportal/versions/v4/VersionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
24 changes: 24 additions & 0 deletions src/test/scala/com/nthportal/versions/variable/OfSizeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
30 changes: 22 additions & 8 deletions src/test/scala/com/nthportal/versions/variable/VersionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 483ba2f

Please sign in to comment.