Skip to content

Commit f796aa1

Browse files
mpiekutowskiSpace Team
authored andcommitted
[Klib] Add KlibMetadataVersion
To avoid the direct usage of `MetadataVersion` in kotlinx-metadata-klib ^KT-81409
1 parent c52b083 commit f796aa1

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.metadata.klib
7+
8+
/**
9+
* KLIB-specific metadata version. Supposed to be used mainly with the logic related to kotlinx-metadata-klib library.
10+
*/
11+
class KlibMetadataVersion(val major: Int, val minor: Int, val patch: Int) : Comparable<KlibMetadataVersion> {
12+
constructor(intArray: IntArray) : this(
13+
intArray.getOrNull(0) ?: error("Major version is expected"),
14+
intArray.getOrNull(1) ?: error("Minor version is expected"),
15+
intArray.getOrNull(2) ?: 0,
16+
) {
17+
require(intArray.size <= 3) { "Metadata version should have no more than 3 components" }
18+
}
19+
20+
init {
21+
require(major >= 0) { "Major version should be not less than 0" }
22+
require(minor >= 0) { "Minor version should be not less than 0" }
23+
require(patch >= 0) { "Patch version should be not less than 0" }
24+
}
25+
26+
override fun compareTo(other: KlibMetadataVersion): Int {
27+
val majors = major.compareTo(other.major)
28+
if (majors != 0) return majors
29+
val minors = minor.compareTo(other.minor)
30+
return if (minors != 0) minors else patch.compareTo(other.patch)
31+
}
32+
33+
override fun toString(): String = "$major.$minor.$patch"
34+
35+
override fun hashCode(): Int {
36+
var result = major
37+
result = 31 * result + minor
38+
result = 31 * result + patch
39+
return result
40+
}
41+
42+
override fun equals(other: Any?): Boolean {
43+
if (this === other) return true
44+
if (javaClass != other?.javaClass) return false
45+
46+
other as KlibMetadataVersion
47+
48+
if (major != other.major) return false
49+
if (minor != other.minor) return false
50+
if (patch != other.patch) return false
51+
52+
return true
53+
}
54+
}

0 commit comments

Comments
 (0)