11package com.coder.gateway.sdk
22
33
4- class CoderSemVer (private val major : Long = 0 , private val minor : Long = 0 ) {
4+ class CoderSemVer (private val major : Long = 0 , private val minor : Long = 0 , private val patch : Long = 0 ) : Comparable<CoderSemVer> {
55
66 init {
77 require(major >= 0 ) { " Coder major version must be a positive number" }
88 require(minor >= 0 ) { " Coder minor version must be a positive number" }
9+ require(patch >= 0 ) { " Coder minor version must be a positive number" }
910 }
1011
11- fun isCompatibleWith (other : CoderSemVer ): Boolean {
12- // in the initial development phase minor changes when there are API incompatibilities
13- if (this .major == 0L ) {
14- if (other.major > 0 ) return false
15- return this .minor == other.minor
16- }
17- return this .major <= other.major
12+ fun isInClosedRange (start : CoderSemVer , endInclusive : CoderSemVer ) = this in start.. endInclusive
13+
14+
15+ override fun toString (): String {
16+ return " CoderSemVer(major=$major , minor=$minor )"
1817 }
1918
2019 override fun equals (other : Any? ): Boolean {
@@ -25,20 +24,28 @@ class CoderSemVer(private val major: Long = 0, private val minor: Long = 0) {
2524
2625 if (major != other.major) return false
2726 if (minor != other.minor) return false
27+ if (patch != other.patch) return false
2828
2929 return true
3030 }
3131
3232 override fun hashCode (): Int {
3333 var result = major.hashCode()
3434 result = 31 * result + minor.hashCode()
35+ result = 31 * result + patch.hashCode()
3536 return result
3637 }
3738
38- override fun toString (): String {
39- return " CoderSemVer(major=$major , minor=$minor )"
40- }
39+ override fun compareTo (other : CoderSemVer ): Int {
40+ if (major > other.major) return 1
41+ if (major < other.major) return - 1
42+ if (minor > other.minor) return 1
43+ if (minor < other.minor) return - 1
44+ if (patch > other.patch) return 1
45+ if (patch < other.patch) return - 1
4146
47+ return 0
48+ }
4249
4350 companion object {
4451 private val pattern = """ ^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$""" .toRegex()
@@ -52,6 +59,7 @@ class CoderSemVer(private val major: Long = 0, private val minor: Long = 0) {
5259 return CoderSemVer (
5360 if (matchResult.groupValues[1 ].isNotEmpty()) matchResult.groupValues[1 ].toLong() else 0 ,
5461 if (matchResult.groupValues[2 ].isNotEmpty()) matchResult.groupValues[2 ].toLong() else 0 ,
62+ if (matchResult.groupValues[3 ].isNotEmpty()) matchResult.groupValues[3 ].toLong() else 0 ,
5563 )
5664 }
5765 }
0 commit comments