1
1
package com.coder.gateway.sdk
2
2
3
3
4
- class CoderSemVer (private val major : Int = 0 , private val minor : Int = 0 ) {
4
+ class CoderSemVer (private val major : Long = 0 , private val minor : Long = 0 ) {
5
5
6
6
init {
7
7
require(major >= 0 ) { " Coder major version must be a positive number" }
@@ -10,23 +10,48 @@ class CoderSemVer(private val major: Int = 0, private val minor: Int = 0) {
10
10
11
11
fun isCompatibleWith (other : CoderSemVer ): Boolean {
12
12
// in the initial development phase minor changes when there are API incompatibilities
13
- if (this .major == 0 ) {
13
+ if (this .major == 0L ) {
14
14
if (other.major > 0 ) return false
15
15
return this .minor == other.minor
16
16
}
17
17
return this .major <= other.major
18
18
}
19
19
20
+ override fun equals (other : Any? ): Boolean {
21
+ if (this == = other) return true
22
+ if (javaClass != other?.javaClass) return false
23
+
24
+ other as CoderSemVer
25
+
26
+ if (major != other.major) return false
27
+ if (minor != other.minor) return false
28
+
29
+ return true
30
+ }
31
+
32
+ override fun hashCode (): Int {
33
+ var result = major.hashCode()
34
+ result = 31 * result + minor.hashCode()
35
+ return result
36
+ }
37
+
38
+ override fun toString (): String {
39
+ return " CoderSemVer(major=$major , minor=$minor )"
40
+ }
41
+
42
+
20
43
companion object {
21
44
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()
22
45
46
+ @JvmStatic
23
47
fun isValidVersion (semVer : String ) = pattern.matchEntire(semVer.trimStart(' v' )) != null
24
48
49
+ @JvmStatic
25
50
fun parse (semVer : String ): CoderSemVer {
26
51
val matchResult = pattern.matchEntire(semVer.trimStart(' v' )) ? : throw IllegalArgumentException (" $semVer could not be parsed" )
27
52
return CoderSemVer (
28
- if (matchResult.groupValues[1 ].isNotEmpty()) matchResult.groupValues[1 ].toInt () else 0 ,
29
- if (matchResult.groupValues[2 ].isNotEmpty()) matchResult.groupValues[2 ].toInt () else 0 ,
53
+ if (matchResult.groupValues[1 ].isNotEmpty()) matchResult.groupValues[1 ].toLong () else 0 ,
54
+ if (matchResult.groupValues[2 ].isNotEmpty()) matchResult.groupValues[2 ].toLong () else 0 ,
30
55
)
31
56
}
32
57
}
0 commit comments