Skip to content

Commit 826da3a

Browse files
committed
Move string conversion to dedicated val rather than computing it in toString
1 parent 2205885 commit 826da3a

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/main/kotlin/com/demonwav/mcdev/platform/forge/version/ForgeVersion.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ForgeVersion private constructor(private val map: Map<*, *>) {
3333
}
3434
}
3535

36-
return recommended.toString()
36+
return recommended.versionString
3737
}
3838

3939
fun getPromo(version: String): Double? {

src/main/kotlin/com/demonwav/mcdev/util/SemanticVersion.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import com.demonwav.mcdev.util.SemanticVersion.Companion.VersionPart.ReleasePart
1919
* to the version ranking with decreasing priority from left to right.
2020
*/
2121
class SemanticVersion(val parts: List<VersionPart>) : Comparable<SemanticVersion> {
22+
val versionString = parts.map { it.versionString }.joinToString(".");
23+
2224
override fun compareTo(other: SemanticVersion): Int {
2325
// Zipping limits the compared parts to the shorter version, then we perform a component-wise comparison
2426
// Short-circuits if any component of this version is smaller/older
@@ -39,8 +41,6 @@ class SemanticVersion(val parts: List<VersionPart>) : Comparable<SemanticVersion
3941

4042
override fun hashCode() = parts.hashCode()
4143

42-
override fun toString() = parts.map { it.toString() }.joinToString(".")
43-
4444
companion object {
4545
/**
4646
* Parses a version string into a comparable representation.
@@ -71,24 +71,26 @@ class SemanticVersion(val parts: List<VersionPart>) : Comparable<SemanticVersion
7171
}
7272

7373
sealed class VersionPart : Comparable<VersionPart> {
74+
abstract val versionString: String
75+
7476
data class ReleasePart(val version: Int) : VersionPart() {
77+
override val versionString = version.toString()
78+
7579
override fun compareTo(other: VersionPart) =
7680
when (other) {
7781
is PreReleasePart -> if (version != other.version) version - other.version else 1
7882
is ReleasePart -> version - other.version
7983
}
80-
81-
override fun toString() = version.toString()
8284
}
8385

8486
data class PreReleasePart(val version: Int, val pre: Int) : VersionPart() {
87+
override val versionString = "${version}_pre$pre"
88+
8589
override fun compareTo(other: VersionPart) =
8690
when (other) {
8791
is PreReleasePart -> if (version != other.version) version - other.version else pre - other.pre
8892
is ReleasePart -> if (version != other.version) version - other.version else -1
8993
}
90-
91-
override fun toString() = "${version}_pre$pre"
9294
}
9395
}
9496
}

0 commit comments

Comments
 (0)