1
1
package com.coder.gateway.sdk
2
2
3
3
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> {
5
5
6
6
init {
7
7
require(major >= 0 ) { " Coder major version must be a positive number" }
8
8
require(minor >= 0 ) { " Coder minor version must be a positive number" }
9
+ require(patch >= 0 ) { " Coder minor version must be a positive number" }
9
10
}
10
11
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 )"
18
17
}
19
18
20
19
override fun equals (other : Any? ): Boolean {
@@ -25,20 +24,28 @@ class CoderSemVer(private val major: Long = 0, private val minor: Long = 0) {
25
24
26
25
if (major != other.major) return false
27
26
if (minor != other.minor) return false
27
+ if (patch != other.patch) return false
28
28
29
29
return true
30
30
}
31
31
32
32
override fun hashCode (): Int {
33
33
var result = major.hashCode()
34
34
result = 31 * result + minor.hashCode()
35
+ result = 31 * result + patch.hashCode()
35
36
return result
36
37
}
37
38
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
41
46
47
+ return 0
48
+ }
42
49
43
50
companion object {
44
51
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) {
52
59
return CoderSemVer (
53
60
if (matchResult.groupValues[1 ].isNotEmpty()) matchResult.groupValues[1 ].toLong() else 0 ,
54
61
if (matchResult.groupValues[2 ].isNotEmpty()) matchResult.groupValues[2 ].toLong() else 0 ,
62
+ if (matchResult.groupValues[3 ].isNotEmpty()) matchResult.groupValues[3 ].toLong() else 0 ,
55
63
)
56
64
}
57
65
}
0 commit comments