DroidFS/app/src/main/java/sushi/hardcore/droidfs/util/Version.kt

29 lines
954 B
Kotlin
Raw Normal View History

2023-08-20 14:56:46 +02:00
package sushi.hardcore.droidfs.util
import java.lang.Integer.max
class Version(inputVersion: String) : Comparable<Version> {
2023-09-06 19:27:41 +02:00
private val version: String
2023-08-20 14:56:46 +02:00
init {
val regex = "[0-9]+(\\.[0-9]+)*".toRegex()
val match = regex.find(inputVersion) ?: throw IllegalArgumentException("Invalid version format")
version = match.value
}
fun split() = version.split(".").toTypedArray()
override fun compareTo(other: Version) =
(split() to other.split()).let { (split, otherSplit) ->
val length = max(split.size, otherSplit.size)
for (i in 0 until length) {
val part = if (i < split.size) split[i].toInt() else 0
val otherPart = if (i < otherSplit.size) otherSplit[i].toInt() else 0
if (part < otherPart) return -1
if (part > otherPart) return 1
}
0
}
2023-09-19 13:47:59 +02:00
override fun toString() = version
2023-08-20 14:56:46 +02:00
}