@@ -21,6 +21,8 @@ import spray.json.deserializationError
2121import spray .json .JsString
2222import spray .json .JsValue
2323import spray .json .RootJsonFormat
24+
25+ import scala .annotation .tailrec
2426import scala .util .Try
2527
2628/**
@@ -34,7 +36,7 @@ import scala.util.Try
3436 *
3537 * @param (major, minor, patch) for the semantic version
3638 */
37- protected [core] class SemVer private (private val version : (Int , Int , Int )) extends AnyVal {
39+ protected [core] class SemVer private (private val version : (Int , Int , Int )) extends AnyVal with Ordered [ SemVer ] {
3840
3941 protected [core] def major = version._1
4042 protected [core] def minor = version._2
@@ -46,6 +48,24 @@ protected[core] class SemVer private (private val version: (Int, Int, Int)) exte
4648
4749 protected [entity] def toJson = JsString (toString)
4850 override def toString = s " $major. $minor. $patch"
51+
52+ protected [core] def versionList = Seq (major, minor, patch)
53+
54+ override def compare (that : SemVer ): Int = {
55+ compareVersion(that)
56+ }
57+
58+ @ tailrec
59+ private def compareVersion (that : SemVer , depth : Int = 0 ): Int = {
60+ require(depth >= 0 && depth <= 2 , " depth exceed the limit of 0 to 2" )
61+ val result = versionList(depth) - that.versionList(depth)
62+ if (result != 0 )
63+ result
64+ else if (depth == 2 )
65+ 0
66+ else
67+ compareVersion(that, depth + 1 )
68+ }
4969}
5070
5171protected [core] object SemVer {
0 commit comments