Skip to content

Commit

Permalink
Deprecate versionNumber with version (#395)
Browse files Browse the repository at this point in the history
* Add klib api

* Simplify Gradle setup

* Deprecate versionNumber with version

* Fix codestyle
  • Loading branch information
hfhbd authored Aug 30, 2024
1 parent 1f7d72a commit 9e1e458
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 32 deletions.
1 change: 1 addition & 0 deletions kotlinx-uuid-core/api/android/kotlinx-uuid-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class app/softwork/uuid/UuidKt {
public static final fun getNode (Lkotlin/uuid/Uuid;)J
public static final fun getTimeStamp (Lkotlin/uuid/Uuid;)J
public static final fun getVariant (Lkotlin/uuid/Uuid;)I
public static final fun getVersion (Lkotlin/uuid/Uuid;)I
public static final fun getVersionNumber (Lkotlin/uuid/Uuid;)I
public static final fun isRfcVariant (Lkotlin/uuid/Uuid;)Z
public static final fun isValidUuidString (Lkotlin/uuid/Uuid$Companion;Ljava/lang/String;)Z
Expand Down
1 change: 1 addition & 0 deletions kotlinx-uuid-core/api/jvm/kotlinx-uuid-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class app/softwork/uuid/UuidKt {
public static final fun getNode (Lkotlin/uuid/Uuid;)J
public static final fun getTimeStamp (Lkotlin/uuid/Uuid;)J
public static final fun getVariant (Lkotlin/uuid/Uuid;)I
public static final fun getVersion (Lkotlin/uuid/Uuid;)I
public static final fun getVersionNumber (Lkotlin/uuid/Uuid;)I
public static final fun isRfcVariant (Lkotlin/uuid/Uuid;)Z
public static final fun isValidUuidString (Lkotlin/uuid/Uuid$Companion;Ljava/lang/String;)Z
Expand Down
2 changes: 2 additions & 0 deletions kotlinx-uuid-core/api/kotlinx-uuid-core.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ final val app.softwork.uuid/unixTimeStamp // app.softwork.uuid/unixTimeStamp|@ko
final fun (kotlin.uuid/Uuid).<get-unixTimeStamp>(): kotlin/Long // app.softwork.uuid/unixTimeStamp.<get-unixTimeStamp>|<get-unixTimeStamp>@kotlin.uuid.Uuid(){}[0]
final val app.softwork.uuid/variant // app.softwork.uuid/variant|@kotlin.uuid.Uuid{}variant[0]
final fun (kotlin.uuid/Uuid).<get-variant>(): kotlin/Int // app.softwork.uuid/variant.<get-variant>|<get-variant>@kotlin.uuid.Uuid(){}[0]
final val app.softwork.uuid/version // app.softwork.uuid/version|@kotlin.uuid.Uuid{}version[0]
final fun (kotlin.uuid/Uuid).<get-version>(): kotlin/Int // app.softwork.uuid/version.<get-version>|<get-version>@kotlin.uuid.Uuid(){}[0]
final val app.softwork.uuid/versionNumber // app.softwork.uuid/versionNumber|@kotlin.uuid.Uuid{}versionNumber[0]
final fun (kotlin.uuid/Uuid).<get-versionNumber>(): kotlin/Int // app.softwork.uuid/versionNumber.<get-versionNumber>|<get-versionNumber>@kotlin.uuid.Uuid(){}[0]

Expand Down
16 changes: 11 additions & 5 deletions kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/Uuid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public fun Uuid.Companion.from(
}

/**
* A 60-bits non-negative number. Depending on the Uuid version it could have different semantics:
* A 60-bits non-negative number. Depending on the Uuid [version] it could have different semantics:
* - UTC time
* - a number constructed from the namespace
* - a random number
Expand All @@ -51,23 +51,29 @@ public val Uuid.clockSequence: Int
get() = toLongs { _, clockSequenceVariantAndNodeRaw -> (clockSequenceVariantAndNodeRaw shr 48 and 0x1fff).toInt() }

/**
* Uuid [numeric version](https://tools.ietf.org/html/rfc4122#section-4.1.3) in range `[0..15]
* Uuid [numeric version](https://tools.ietf.org/html/rfc4122#section-4.1.3) in range `[0..15]`
*/
public val Uuid.versionNumber: Int
@Deprecated("Use version instead", replaceWith = ReplaceWith("version"))
public val Uuid.versionNumber: Int get() = version

/**
* Uuid [numeric version](https://tools.ietf.org/html/rfc4122#section-4.1.3) in range `[0..15]`
*/
public val Uuid.version: Int
get() = toLongs { timeStampAndVersionRaw, _ ->
(timeStampAndVersionRaw and 0xf000L shr 12).toInt()
}

/**
* Uuid variant in range `[0..7]`, similar to version
* Uuid variant in range `[0..7]`, similar to [version]
*/
public val Uuid.variant: Int
get() = toLongs { _, clockSequenceVariantAndNodeRaw ->
(clockSequenceVariantAndNodeRaw ushr 61).toInt()
}

/**
* Uuid variant specified and documented by the RFC
* Uuid [variant] specified and documented by the RFC
*/
public val Uuid.isRfcVariant: Boolean get() = variant == 4 || variant == 5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ class GenerationTest {
fun testGenerateFromName() {
val baseUuid = Uuid.parse(SOME_UUID_STRING)
val generated = Uuid.generateUuid(baseUuid, "test")
assertEquals(5, generated.versionNumber)
assertEquals(5, generated.version)
assertEquals("9dc3df60-4ed1-5ea9-9e66-5c2030d5827b", generated.toString())
}

@Test
fun testGenerateFromBytes() {
val generated = Uuid.generateUuid(SOME_UUID_STRING.explodeToBytes().toByteArray())
assertEquals(5, generated.versionNumber)
assertEquals(5, generated.version)
assertEquals("29e5befd-ca93-58bf-9ef0-30f7da112935", generated.toString())
}

private fun Uuid.assertRandomGenerated() {
assertTrue(isRfcVariant)
assertEquals(4, versionNumber)
assertEquals(4, version)
}

private fun String.explodeToBytes(): List<Byte> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UuidTest {
fun testZero() {
assertEquals("00000000-0000-0000-0000-000000000000", Uuid.NIL.toString())
assertEquals(0, Uuid.NIL.variant)
assertEquals(0, Uuid.NIL.versionNumber)
assertEquals(0, Uuid.NIL.version)
assertEquals(0, Uuid.NIL.timeStamp)
assertEquals(0, Uuid.NIL.clockSequence)
assertEquals(0, Uuid.NIL.node)
Expand All @@ -28,7 +28,7 @@ class UuidTest {
fun testMax() {
assertEquals("ffffffff-ffff-ffff-ffff-ffffffffffff", Uuid.MAX.toString())
assertEquals(7, Uuid.MAX.variant)
assertEquals(15, Uuid.MAX.versionNumber)
assertEquals(15, Uuid.MAX.version)
assertEquals(1152921504606846975, Uuid.MAX.timeStamp)
assertEquals(8191, Uuid.MAX.clockSequence)
assertEquals(281474976710655, Uuid.MAX.node)
Expand All @@ -39,7 +39,7 @@ class UuidTest {
fun testConstructingFromString() {
val uuid = Uuid.parse(UUID_STRING)

assertEquals(1, uuid.versionNumber)
assertEquals(1, uuid.version)
assertEquals(5, uuid.variant)
assertEquals("3d3e99b1b3e4567", uuid.timeStamp.toString(16))
assertEquals("476", uuid.clockSequence.toString(16))
Expand All @@ -53,7 +53,7 @@ class UuidTest {
fun testConstructingFromStringAllFf() {
val uuid = Uuid.parse(UUID_STRING_ALL_FF)

assertEquals(0xf, uuid.versionNumber)
assertEquals(0xf, uuid.version)
assertEquals(7, uuid.variant)
assertEquals("fffffffffffffff", uuid.timeStamp.toString(16))
assertEquals("1fff", uuid.clockSequence.toString(16))
Expand All @@ -65,7 +65,7 @@ class UuidTest {
val first = Uuid.parse(SOME_UUID_STRING)
val second = Uuid.from(
timeStamp = first.timeStamp,
versionNumber = first.versionNumber,
versionNumber = first.version,
clockSequence = first.clockSequence,
node = first.node,
variant = first.variant
Expand All @@ -79,7 +79,7 @@ class UuidTest {
val first = Uuid.parse(SOME_UUID_STRING)
val second = Uuid.from(
timeStamp = first.timeStamp,
versionNumber = first.versionNumber,
versionNumber = first.version,
clockSequence = first.clockSequence,
node = first.node
)
Expand All @@ -92,7 +92,7 @@ class UuidTest {
val first = Uuid.parse(UUID_STRING_ALL_FF)
val second = Uuid.from(
timeStamp = first.timeStamp,
versionNumber = first.versionNumber,
versionNumber = first.version,
clockSequence = first.clockSequence,
node = first.node,
variant = first.variant
Expand All @@ -113,31 +113,31 @@ class UuidTest {
}

assertFailsWith<IllegalArgumentException> {
Uuid.from(first.versionNumber, first.timeStamp, Int.MAX_VALUE, first.node, first.variant)
Uuid.from(first.version, first.timeStamp, Int.MAX_VALUE, first.node, first.variant)
}

assertFailsWith<IllegalArgumentException> {
Uuid.from(first.versionNumber, first.timeStamp, first.clockSequence, Long.MAX_VALUE, first.variant)
Uuid.from(first.version, first.timeStamp, first.clockSequence, Long.MAX_VALUE, first.variant)
}

assertFailsWith<IllegalArgumentException> {
Uuid.from(-1, first.timeStamp, first.clockSequence, first.node, first.variant)
}

assertFailsWith<IllegalArgumentException> {
Uuid.from(first.versionNumber, -1, first.clockSequence, first.node, first.variant)
Uuid.from(first.version, -1, first.clockSequence, first.node, first.variant)
}

assertFailsWith<IllegalArgumentException> {
Uuid.from(first.versionNumber, first.timeStamp, -1, first.node, first.variant)
Uuid.from(first.version, first.timeStamp, -1, first.node, first.variant)
}

assertFailsWith<IllegalArgumentException> {
Uuid.from(first.versionNumber, first.timeStamp, first.clockSequence, -1, first.variant)
Uuid.from(first.version, first.timeStamp, first.clockSequence, -1, first.variant)
}

assertFailsWith<IllegalArgumentException> {
Uuid.from(first.versionNumber, first.timeStamp, first.clockSequence, first.node, -1)
Uuid.from(first.version, first.timeStamp, first.clockSequence, first.node, -1)
}
}

Expand All @@ -154,12 +154,12 @@ class UuidTest {

@Test
fun testVersionNumbers() {
assertEquals(1, Uuid.parse("1b3e4567-e99b-13d3-a476-446657420000").versionNumber)
assertEquals(2, Uuid.parse("1b3e4567-e99b-23d3-a476-446657420000").versionNumber)
assertEquals(3, Uuid.parse("1b3e4567-e99b-33d3-a476-446657420000").versionNumber)
assertEquals(4, Uuid.parse("1b3e4567-e99b-43d3-a476-446657420000").versionNumber)
assertEquals(5, Uuid.parse("1b3e4567-e99b-53d3-a476-446657420000").versionNumber)
assertEquals(0xf, Uuid.parse("1b3e4567-e99b-f3d3-a476-446657420000").versionNumber)
assertEquals(1, Uuid.parse("1b3e4567-e99b-13d3-a476-446657420000").version)
assertEquals(2, Uuid.parse("1b3e4567-e99b-23d3-a476-446657420000").version)
assertEquals(3, Uuid.parse("1b3e4567-e99b-33d3-a476-446657420000").version)
assertEquals(4, Uuid.parse("1b3e4567-e99b-43d3-a476-446657420000").version)
assertEquals(5, Uuid.parse("1b3e4567-e99b-53d3-a476-446657420000").version)
assertEquals(0xf, Uuid.parse("1b3e4567-e99b-f3d3-a476-446657420000").version)
}

@Test
Expand All @@ -174,7 +174,7 @@ class UuidTest {

@Test
fun testRandomCreation() {
assertEquals(4, Uuid.random().versionNumber)
assertEquals(4, Random.nextUuid().versionNumber)
assertEquals(4, Uuid.random().version)
assertEquals(4, Random.nextUuid().version)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Uuidv7Test {

assertEquals(1645557742000, uuid.unixTimeStamp)
assertEquals("017f22e2-79b0-7a1e-ad73-08cd2f61d78a", uuid.toString())
assertEquals(7, uuid.versionNumber)
assertEquals(7, uuid.version)
assertEquals(5, uuid.variant)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.softwork.uuid.datetime

import app.softwork.uuid.versionNumber
import app.softwork.uuid.version
import kotlinx.datetime.Instant
import kotlin.random.Random
import kotlin.test.Test
Expand All @@ -14,6 +14,6 @@ class InstantTest {
val uuid = Uuidv7(timeStamp = timestamp, random = Random(4242))
assertEquals(timestamp, uuid.instant)
assertEquals("0170621e-0ef0-7a1e-ad73-08cd2f61d78a", uuid.toString())
assertEquals(7, uuid.versionNumber)
assertEquals(7, uuid.version)
}
}

0 comments on commit 9e1e458

Please sign in to comment.