Skip to content

Commit 5732814

Browse files
committed
Support bigger numbers for major and minor
- and also implement equals and hashCode
1 parent c744988 commit 5732814

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/main/kotlin/com/coder/gateway/sdk/CoderSemVer.kt

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.coder.gateway.sdk
22

33

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) {
55

66
init {
77
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) {
1010

1111
fun isCompatibleWith(other: CoderSemVer): Boolean {
1212
// in the initial development phase minor changes when there are API incompatibilities
13-
if (this.major == 0) {
13+
if (this.major == 0L) {
1414
if (other.major > 0) return false
1515
return this.minor == other.minor
1616
}
1717
return this.major <= other.major
1818
}
1919

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+
2043
companion object {
2144
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()
2245

46+
@JvmStatic
2347
fun isValidVersion(semVer: String) = pattern.matchEntire(semVer.trimStart('v')) != null
2448

49+
@JvmStatic
2550
fun parse(semVer: String): CoderSemVer {
2651
val matchResult = pattern.matchEntire(semVer.trimStart('v')) ?: throw IllegalArgumentException("$semVer could not be parsed")
2752
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,
3055
)
3156
}
3257
}

0 commit comments

Comments
 (0)