From a70438f4f8ccecdc3d72b4577b8face62ca0c32f Mon Sep 17 00:00:00 2001 From: Philip Wedemann <22521688+hfhbd@users.noreply.github.com> Date: Sat, 17 Aug 2024 14:16:08 +0200 Subject: [PATCH] Change package name to app.softwork (#379) * change package to app.softwork * Rename UUID to Uuid * Fix docs --- README.md | 9 +-- detekt-baseline.xml | 59 ++++++++++--------- .../src/main/kotlin/dokkaLicensee.gradle.kts | 5 ++ kotlinx-uuid-core/README.md | 40 +++---------- .../api/android/kotlinx-uuid-core.api | 32 +++++----- .../api/jvm/kotlinx-uuid-core.api | 30 +++++----- kotlinx-uuid-core/build.gradle.kts | 2 +- .../softwork}/uuid/internal/ParcelableTest.kt | 4 +- .../softwork/uuid}/UuidParceler.kt | 2 +- .../kotlin/app/softwork/uuid/Converter.kt | 20 +++++++ .../kotlin/kotlinx/uuid/Converter.kt | 19 ------ .../softwork}/uuid/NsUUIDConvertingTest.kt | 2 +- .../softwork}/uuid/BinarySerializer.kt | 8 +-- .../softwork}/uuid/NameBasedGenerator.kt | 16 ++--- .../softwork}/uuid/RandomGenerator.kt | 6 +- .../UUID.kt => app/softwork/uuid/Uuid.kt} | 26 ++++---- .../kotlin/app/softwork/uuid/Uuidv7.kt | 36 +++++++++++ .../softwork}/uuid/internal/SHA1.kt | 2 +- .../commonMain/kotlin/kotlinx/uuid/UUID7.kt | 42 ------------- .../softwork}/uuid/BinarySerializationTest.kt | 2 +- .../softwork}/uuid/GenerationTest.kt | 8 +-- .../softwork}/uuid/SHA1Test.kt | 4 +- .../softwork/uuid/UuidTest.kt} | 16 ++--- .../kotlin/app/softwork/uuid/Uuidv7Test.kt | 16 +++++ .../kotlin/kotlinx/uuid/UUIDv7Test.kt | 18 ------ .../kotlin/app/softwork/uuid/Converting.kt | 20 +++++++ .../jvmMain/kotlin/kotlinx/uuid/Converting.kt | 18 ------ .../softwork}/uuid/JavaConvertingTest.kt | 2 +- .../softwork}/uuid/JavaSpecificTests.kt | 2 +- kotlinx-uuid-datetime/README.md | 4 +- .../api/kotlinx-uuid-datetime.api | 6 +- .../kotlin/app/softwork/uuid/datetime/Dsl.kt | 16 +++++ .../kotlin/kotlinx/uuid/datetime/Dsl.kt | 16 ----- .../softwork}/uuid/datetime/InstantTest.kt | 8 +-- kotlinx-uuid-sqldelight/README.md | 6 +- .../api/kotlinx-uuid-sqldelight.api | 8 +-- .../uuid/sqldelight/UuidByteArrayAdapter.kt | 2 +- .../uuid/sqldelight/UuidStringAdapter.kt | 2 +- 38 files changed, 255 insertions(+), 279 deletions(-) rename kotlinx-uuid-core/src/androidInstrumentedTest/kotlin/{kotlinx => app/softwork}/uuid/internal/ParcelableTest.kt (93%) rename kotlinx-uuid-core/src/androidMain/kotlin/{kotlinx/uuid/internal => app/softwork/uuid}/UuidParceler.kt (94%) create mode 100644 kotlinx-uuid-core/src/appleMain/kotlin/app/softwork/uuid/Converter.kt delete mode 100644 kotlinx-uuid-core/src/appleMain/kotlin/kotlinx/uuid/Converter.kt rename kotlinx-uuid-core/src/appleTest/kotlin/{kotlinx => app/softwork}/uuid/NsUUIDConvertingTest.kt (96%) rename kotlinx-uuid-core/src/commonMain/kotlin/{kotlinx => app/softwork}/uuid/BinarySerializer.kt (86%) rename kotlinx-uuid-core/src/commonMain/kotlin/{kotlinx => app/softwork}/uuid/NameBasedGenerator.kt (75%) rename kotlinx-uuid-core/src/commonMain/kotlin/{kotlinx => app/softwork}/uuid/RandomGenerator.kt (85%) rename kotlinx-uuid-core/src/commonMain/kotlin/{kotlinx/uuid/UUID.kt => app/softwork/uuid/Uuid.kt} (80%) create mode 100644 kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/Uuidv7.kt rename kotlinx-uuid-core/src/commonMain/kotlin/{kotlinx => app/softwork}/uuid/internal/SHA1.kt (99%) delete mode 100644 kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/UUID7.kt rename kotlinx-uuid-core/src/commonTest/kotlin/{kotlinx => app/softwork}/uuid/BinarySerializationTest.kt (98%) rename kotlinx-uuid-core/src/commonTest/kotlin/{kotlinx => app/softwork}/uuid/GenerationTest.kt (87%) rename kotlinx-uuid-core/src/commonTest/kotlin/{kotlinx => app/softwork}/uuid/SHA1Test.kt (97%) rename kotlinx-uuid-core/src/commonTest/kotlin/{kotlinx/uuid/UUIDTest.kt => app/softwork/uuid/UuidTest.kt} (93%) create mode 100644 kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/Uuidv7Test.kt delete mode 100644 kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/UUIDv7Test.kt create mode 100644 kotlinx-uuid-core/src/jvmMain/kotlin/app/softwork/uuid/Converting.kt delete mode 100644 kotlinx-uuid-core/src/jvmMain/kotlin/kotlinx/uuid/Converting.kt rename kotlinx-uuid-core/src/jvmTest/kotlin/{kotlinx => app/softwork}/uuid/JavaConvertingTest.kt (96%) rename kotlinx-uuid-core/src/jvmTest/kotlin/{kotlinx => app/softwork}/uuid/JavaSpecificTests.kt (94%) create mode 100644 kotlinx-uuid-datetime/src/commonMain/kotlin/app/softwork/uuid/datetime/Dsl.kt delete mode 100644 kotlinx-uuid-datetime/src/commonMain/kotlin/kotlinx/uuid/datetime/Dsl.kt rename kotlinx-uuid-datetime/src/commonTest/kotlin/{kotlinx => app/softwork}/uuid/datetime/InstantTest.kt (64%) rename kotlinx-uuid-sqldelight/src/commonMain/kotlin/{kotlinx => app/softwork}/uuid/sqldelight/UuidByteArrayAdapter.kt (88%) rename kotlinx-uuid-sqldelight/src/commonMain/kotlin/{kotlinx => app/softwork}/uuid/sqldelight/UuidStringAdapter.kt (87%) diff --git a/README.md b/README.md index b631315..2f53537 100644 --- a/README.md +++ b/README.md @@ -3,18 +3,11 @@ > #### This is a fork from https://github.com/cy6erGn0m/kotlinx-uuid, released under Apache 2. > #### The main implementation was thankfully provided by [Sergey Mashkov (cy6erGn0m)](https://github.com/cy6erGn0m)! -`kotlinx-uuid` is a multiplatform (MPP) [Kotlin](https://kotlinlang.org) library introducing support -for [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier). +`kotlinx-uuid` adds helper methods to [Uuid](kotlin.uuid.Uuid) added to Kotlin stdlib starting 2.0.20. - [Source code](https://github.com/hfhbd/kotlinx-uuid) - [Docs](https://uuid.softwork.app) -The main class `UUID` is serializable out of the box, so the library depends -on [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization). If you don't need serialization, you don't -need to apply the plugin. - -Supported platforms are `jvm`, `js(IR)` and all tier 1, 2 and 3 native targets. - ## Install This package is uploaded to `mavenCentral`. diff --git a/detekt-baseline.xml b/detekt-baseline.xml index a4c99bc..acb580f 100644 --- a/detekt-baseline.xml +++ b/detekt-baseline.xml @@ -2,8 +2,8 @@ - FunctionNaming:Dsl.kt$public fun UUIDv7(timeStamp: Instant = Clock.System.now(), random: Random): Uuid - FunctionNaming:UUID7.kt$public fun UUIDv7(timeStamp: Long, random: Random): Uuid + FunctionNaming:Dsl.kt$public fun Uuidv7(timeStamp: Instant = Clock.System.now(), random: Random): Uuid + FunctionNaming:Uuidv7.kt$public fun Uuidv7(timeStamp: Long, random: Random): Uuid MagicNumber:KotlinConfig.kt$8 MagicNumber:NameBasedGenerator.kt$0x3f MagicNumber:NameBasedGenerator.kt$4 @@ -44,31 +44,36 @@ MagicNumber:SHA1.kt$SHA1.IntArrayView$3 MagicNumber:SHA1.kt$SHA1.IntArrayView$4 MagicNumber:SHA1.kt$SHA1.IntArrayView$8 - MagicNumber:UUID.kt$0x0fffL - MagicNumber:UUID.kt$0x1fff - MagicNumber:UUID.kt$0xf000L - MagicNumber:UUID.kt$0xffff00000000L - MagicNumber:UUID.kt$0xffff0000L - MagicNumber:UUID.kt$0xffffffffffffL - MagicNumber:UUID.kt$12 - MagicNumber:UUID.kt$13 - MagicNumber:UUID.kt$15 - MagicNumber:UUID.kt$16 - MagicNumber:UUID.kt$32 - MagicNumber:UUID.kt$4 - MagicNumber:UUID.kt$48 - MagicNumber:UUID.kt$5 - MagicNumber:UUID.kt$60 - MagicNumber:UUID.kt$61 - MagicNumber:UUID.kt$7 - MagicNumber:UUID7.kt$0x0fffL - MagicNumber:UUID7.kt$0xffff0000L - MagicNumber:UUID7.kt$16 - MagicNumber:UUID7.kt$28672 - MagicNumber:UUID7.kt$32 - MagicNumber:UUID7.kt$4095 - MagicNumber:UUID7.kt$48 - MagicNumber:UUID7.kt$62 + MagicNumber:Uuid.kt$0x0fffL + MagicNumber:Uuid.kt$0x1fff + MagicNumber:Uuid.kt$0xf000L + MagicNumber:Uuid.kt$0xffff00000000L + MagicNumber:Uuid.kt$0xffff0000L + MagicNumber:Uuid.kt$0xffffffffffffL + MagicNumber:Uuid.kt$12 + MagicNumber:Uuid.kt$13 + MagicNumber:Uuid.kt$15 + MagicNumber:Uuid.kt$16 + MagicNumber:Uuid.kt$32 + MagicNumber:Uuid.kt$4 + MagicNumber:Uuid.kt$48 + MagicNumber:Uuid.kt$5 + MagicNumber:Uuid.kt$60 + MagicNumber:Uuid.kt$61 + MagicNumber:Uuid.kt$7 + MagicNumber:Uuidv7.kt$0x3F + MagicNumber:Uuidv7.kt$0x70 + MagicNumber:Uuidv7.kt$0x80 + MagicNumber:Uuidv7.kt$0xFF + MagicNumber:Uuidv7.kt$16 + MagicNumber:Uuidv7.kt$24 + MagicNumber:Uuidv7.kt$3 + MagicNumber:Uuidv7.kt$32 + MagicNumber:Uuidv7.kt$4 + MagicNumber:Uuidv7.kt$40 + MagicNumber:Uuidv7.kt$5 + MagicNumber:Uuidv7.kt$6 + MagicNumber:Uuidv7.kt$8 TooManyFunctions:SHA1.kt$SHA1 diff --git a/gradle/build-logic/src/main/kotlin/dokkaLicensee.gradle.kts b/gradle/build-logic/src/main/kotlin/dokkaLicensee.gradle.kts index 4fc1f53..e9a51fb 100644 --- a/gradle/build-logic/src/main/kotlin/dokkaLicensee.gradle.kts +++ b/gradle/build-logic/src/main/kotlin/dokkaLicensee.gradle.kts @@ -19,6 +19,11 @@ tasks.named("dokkaHtmlPartial") { } } externalDocumentationLink("https://kotlinlang.org/api/kotlinx.serialization/") + externalDocumentationLink( + url = "https://kotlinlang.org/api/kotlinx-datetime/", + packageListUrl = "https://kotlinlang.org/api/kotlinx-datetime/kotlinx-datetime/package-list", + ) + externalDocumentationLink("https://cashapp.github.io/sqldelight/2.0.2/2.x/") } } diff --git a/kotlinx-uuid-core/README.md b/kotlinx-uuid-core/README.md index a04bcbb..bfd621e 100644 --- a/kotlinx-uuid-core/README.md +++ b/kotlinx-uuid-core/README.md @@ -1,37 +1,21 @@ # Module kotlinx-uuid-core -This core module contains the serializable UUID class. - -## Creating from a UUID string - -```kotlin -val uuid = UUID("1b3e4567-e99b-13d3-a476-446657420000") -val guid = UUID("{1b3e4567-e99b-13d3-a476-446657420000}") -``` - -## Generating UUID4 using random - -```kotlin -// using a default SecureRandom implementation -val uuid = UUID() - -// use custom Kotlin Random instance -val uuid = UUID.generateUUID(yourRandom) -``` +This core module contains several helper methods for the [Uuid](kotlin.uuid.Uuid) class, like `timestamp`, `variant`, +`version`. ## Generating UUID5 using hash `kotlinx-uuid` provides the ability to generate uuids by hashing names (Only SHA-1 is supported at the moment). ```kotlin -val appNamespace = UUID("my-app-uuid") -val agentId = UUID.generateUUID(appNamespace, "agentId") +val appNamespace = Uuid.parse("my-app-uuid") +val agentId = Uuid.generateUuid(appNamespace, "agentId") ``` -The other alternative is to generate UUID by hashing bytes (similar to `java.util.UUID.nameUUIDFromBytes`). +The other alternative is to generate Uuid by hashing bytes (similar to `java.util.UUID.nameUUIDFromBytes`). ```kotlin -val uuid = UUID.generateUUID(bytes) +val uuid = Uuid.generateUUID(bytes) ``` > Note that unlike `java.util.UUID`, kotlinx's generateUUID @@ -51,15 +35,9 @@ uuid.unixTimeStamp // 42 ## Serializing (kotlinx.serialization) -There are two serializers for `UUID`: the default one and the binary. - -The default serializer does always serialize UUIDs as string primitives. - -```kotlin -Json.encodeToString(uuid) == "\"1b3e4567-e99b-13d3-a476-446657420000\"" -``` - -The additional serializer is useful for binary formats. Because they are not human-readable, and it's possible to reduce +There is also a binary serializers for `Uuid`. +This additional serializer is useful for binary formats. Because they are not human-readable, and it's possible to +reduce size. ```kotlin diff --git a/kotlinx-uuid-core/api/android/kotlinx-uuid-core.api b/kotlinx-uuid-core/api/android/kotlinx-uuid-core.api index e6c8bc0..4ab80fd 100644 --- a/kotlinx-uuid-core/api/android/kotlinx-uuid-core.api +++ b/kotlinx-uuid-core/api/android/kotlinx-uuid-core.api @@ -1,5 +1,5 @@ -public final class kotlinx/uuid/BinarySerializer : kotlinx/serialization/KSerializer { - public static final field INSTANCE Lkotlinx/uuid/BinarySerializer; +public final class app/softwork/uuid/BinarySerializer : kotlinx/serialization/KSerializer { + public static final field INSTANCE Lapp/softwork/uuid/BinarySerializer; public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object; public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lkotlin/uuid/Uuid; public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor; @@ -7,22 +7,17 @@ public final class kotlinx/uuid/BinarySerializer : kotlinx/serialization/KSerial public fun serialize (Lkotlinx/serialization/encoding/Encoder;Lkotlin/uuid/Uuid;)V } -public final class kotlinx/uuid/NameBasedGeneratorKt { - public static final fun generateUUID (Lkotlin/uuid/Uuid$Companion;Lkotlin/uuid/Uuid;Ljava/lang/String;)Lkotlin/uuid/Uuid; - public static final fun generateUUID (Lkotlin/uuid/Uuid$Companion;[B)Lkotlin/uuid/Uuid; +public final class app/softwork/uuid/NameBasedGeneratorKt { + public static final fun generateUuid (Lkotlin/uuid/Uuid$Companion;Lkotlin/uuid/Uuid;Ljava/lang/String;)Lkotlin/uuid/Uuid; + public static final fun generateUuid (Lkotlin/uuid/Uuid$Companion;[B)Lkotlin/uuid/Uuid; } -public final class kotlinx/uuid/RandomGeneratorKt { +public final class app/softwork/uuid/RandomGeneratorKt { public static final fun nextUuid (Lkotlin/random/Random;)Lkotlin/uuid/Uuid; public static final fun random (Lkotlin/uuid/Uuid$Companion;Lkotlin/random/Random;)Lkotlin/uuid/Uuid; } -public final class kotlinx/uuid/UUID7Kt { - public static final fun UUIDv7 (JLkotlin/random/Random;)Lkotlin/uuid/Uuid; - public static final fun getUnixTimeStamp (Lkotlin/uuid/Uuid;)J -} - -public final class kotlinx/uuid/UUIDKt { +public final class app/softwork/uuid/UuidKt { public static final fun from (Lkotlin/uuid/Uuid$Companion;IJIJI)Lkotlin/uuid/Uuid; public static synthetic fun from$default (Lkotlin/uuid/Uuid$Companion;IJIJIILjava/lang/Object;)Lkotlin/uuid/Uuid; public static final fun getClockSequence (Lkotlin/uuid/Uuid;)I @@ -31,13 +26,13 @@ public final class kotlinx/uuid/UUIDKt { public static final fun getVariant (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 - public static final fun toUUIDOrNull (Ljava/lang/String;)Lkotlin/uuid/Uuid; + public static final fun isValidUuidString (Lkotlin/uuid/Uuid$Companion;Ljava/lang/String;)Z public static final fun toUuid (Ljava/lang/String;)Lkotlin/uuid/Uuid; + public static final fun toUuidOrNull (Ljava/lang/String;)Lkotlin/uuid/Uuid; } -public final class kotlinx/uuid/internal/UuidParceler : kotlinx/parcelize/Parceler { - public static final field INSTANCE Lkotlinx/uuid/internal/UuidParceler; +public final class app/softwork/uuid/UuidParceler : kotlinx/parcelize/Parceler { + public static final field INSTANCE Lapp/softwork/uuid/UuidParceler; public synthetic fun create (Landroid/os/Parcel;)Ljava/lang/Object; public fun create (Landroid/os/Parcel;)Lkotlin/uuid/Uuid; public fun equals (Ljava/lang/Object;)Z @@ -49,3 +44,8 @@ public final class kotlinx/uuid/internal/UuidParceler : kotlinx/parcelize/Parcel public fun write (Lkotlin/uuid/Uuid;Landroid/os/Parcel;I)V } +public final class app/softwork/uuid/Uuidv7Kt { + public static final fun Uuidv7 (JLkotlin/random/Random;)Lkotlin/uuid/Uuid; + public static final fun getUnixTimeStamp (Lkotlin/uuid/Uuid;)J +} + diff --git a/kotlinx-uuid-core/api/jvm/kotlinx-uuid-core.api b/kotlinx-uuid-core/api/jvm/kotlinx-uuid-core.api index 2597f01..634187e 100644 --- a/kotlinx-uuid-core/api/jvm/kotlinx-uuid-core.api +++ b/kotlinx-uuid-core/api/jvm/kotlinx-uuid-core.api @@ -1,5 +1,5 @@ -public final class kotlinx/uuid/BinarySerializer : kotlinx/serialization/KSerializer { - public static final field INSTANCE Lkotlinx/uuid/BinarySerializer; +public final class app/softwork/uuid/BinarySerializer : kotlinx/serialization/KSerializer { + public static final field INSTANCE Lapp/softwork/uuid/BinarySerializer; public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object; public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lkotlin/uuid/Uuid; public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor; @@ -7,27 +7,22 @@ public final class kotlinx/uuid/BinarySerializer : kotlinx/serialization/KSerial public fun serialize (Lkotlinx/serialization/encoding/Encoder;Lkotlin/uuid/Uuid;)V } -public final class kotlinx/uuid/ConvertingKt { +public final class app/softwork/uuid/ConvertingKt { public static final fun toJavaUUID (Lkotlin/uuid/Uuid;)Ljava/util/UUID; public static final fun toKotlinUuid (Ljava/util/UUID;)Lkotlin/uuid/Uuid; } -public final class kotlinx/uuid/NameBasedGeneratorKt { - public static final fun generateUUID (Lkotlin/uuid/Uuid$Companion;Lkotlin/uuid/Uuid;Ljava/lang/String;)Lkotlin/uuid/Uuid; - public static final fun generateUUID (Lkotlin/uuid/Uuid$Companion;[B)Lkotlin/uuid/Uuid; +public final class app/softwork/uuid/NameBasedGeneratorKt { + public static final fun generateUuid (Lkotlin/uuid/Uuid$Companion;Lkotlin/uuid/Uuid;Ljava/lang/String;)Lkotlin/uuid/Uuid; + public static final fun generateUuid (Lkotlin/uuid/Uuid$Companion;[B)Lkotlin/uuid/Uuid; } -public final class kotlinx/uuid/RandomGeneratorKt { +public final class app/softwork/uuid/RandomGeneratorKt { public static final fun nextUuid (Lkotlin/random/Random;)Lkotlin/uuid/Uuid; public static final fun random (Lkotlin/uuid/Uuid$Companion;Lkotlin/random/Random;)Lkotlin/uuid/Uuid; } -public final class kotlinx/uuid/UUID7Kt { - public static final fun UUIDv7 (JLkotlin/random/Random;)Lkotlin/uuid/Uuid; - public static final fun getUnixTimeStamp (Lkotlin/uuid/Uuid;)J -} - -public final class kotlinx/uuid/UUIDKt { +public final class app/softwork/uuid/UuidKt { public static final fun from (Lkotlin/uuid/Uuid$Companion;IJIJI)Lkotlin/uuid/Uuid; public static synthetic fun from$default (Lkotlin/uuid/Uuid$Companion;IJIJIILjava/lang/Object;)Lkotlin/uuid/Uuid; public static final fun getClockSequence (Lkotlin/uuid/Uuid;)I @@ -36,8 +31,13 @@ public final class kotlinx/uuid/UUIDKt { public static final fun getVariant (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 - public static final fun toUUIDOrNull (Ljava/lang/String;)Lkotlin/uuid/Uuid; + public static final fun isValidUuidString (Lkotlin/uuid/Uuid$Companion;Ljava/lang/String;)Z public static final fun toUuid (Ljava/lang/String;)Lkotlin/uuid/Uuid; + public static final fun toUuidOrNull (Ljava/lang/String;)Lkotlin/uuid/Uuid; +} + +public final class app/softwork/uuid/Uuidv7Kt { + public static final fun Uuidv7 (JLkotlin/random/Random;)Lkotlin/uuid/Uuid; + public static final fun getUnixTimeStamp (Lkotlin/uuid/Uuid;)J } diff --git a/kotlinx-uuid-core/build.gradle.kts b/kotlinx-uuid-core/build.gradle.kts index 3d9e3f8..55b985e 100644 --- a/kotlinx-uuid-core/build.gradle.kts +++ b/kotlinx-uuid-core/build.gradle.kts @@ -34,7 +34,7 @@ kotlin { } android { - namespace = "kotlinx.uuid" + namespace = "app.softwork.uuid" compileSdk = 34 defaultConfig { diff --git a/kotlinx-uuid-core/src/androidInstrumentedTest/kotlin/kotlinx/uuid/internal/ParcelableTest.kt b/kotlinx-uuid-core/src/androidInstrumentedTest/kotlin/app/softwork/uuid/internal/ParcelableTest.kt similarity index 93% rename from kotlinx-uuid-core/src/androidInstrumentedTest/kotlin/kotlinx/uuid/internal/ParcelableTest.kt rename to kotlinx-uuid-core/src/androidInstrumentedTest/kotlin/app/softwork/uuid/internal/ParcelableTest.kt index d0abea6..a83a8da 100644 --- a/kotlinx-uuid-core/src/androidInstrumentedTest/kotlin/kotlinx/uuid/internal/ParcelableTest.kt +++ b/kotlinx-uuid-core/src/androidInstrumentedTest/kotlin/app/softwork/uuid/internal/ParcelableTest.kt @@ -1,11 +1,11 @@ -package kotlinx.uuid.internal +package app.softwork.uuid.internal import android.os.Parcel import android.os.Parcelable +import app.softwork.uuid.* import kotlinx.parcelize.Parcelize import kotlinx.parcelize.TypeParceler import kotlinx.parcelize.parcelableCreator -import kotlinx.uuid.* import kotlin.test.* import kotlin.uuid.Uuid diff --git a/kotlinx-uuid-core/src/androidMain/kotlin/kotlinx/uuid/internal/UuidParceler.kt b/kotlinx-uuid-core/src/androidMain/kotlin/app/softwork/uuid/UuidParceler.kt similarity index 94% rename from kotlinx-uuid-core/src/androidMain/kotlin/kotlinx/uuid/internal/UuidParceler.kt rename to kotlinx-uuid-core/src/androidMain/kotlin/app/softwork/uuid/UuidParceler.kt index 46b102f..e9b662f 100644 --- a/kotlinx-uuid-core/src/androidMain/kotlin/kotlinx/uuid/internal/UuidParceler.kt +++ b/kotlinx-uuid-core/src/androidMain/kotlin/app/softwork/uuid/UuidParceler.kt @@ -1,4 +1,4 @@ -package kotlinx.uuid.internal +package app.softwork.uuid import android.os.Parcel import kotlinx.parcelize.Parceler diff --git a/kotlinx-uuid-core/src/appleMain/kotlin/app/softwork/uuid/Converter.kt b/kotlinx-uuid-core/src/appleMain/kotlin/app/softwork/uuid/Converter.kt new file mode 100644 index 0000000..dd9acdf --- /dev/null +++ b/kotlinx-uuid-core/src/appleMain/kotlin/app/softwork/uuid/Converter.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package app.softwork.uuid + +import platform.Foundation.NSUUID +import kotlin.uuid.Uuid + +/** + * Converts this [NSUUID] value to a [Uuid] value + * by using the [UUIDString](platform.Foundation.NSUUID.UUIDString) representation. + */ +public fun NSUUID.toKotlinUuid(): Uuid = Uuid.parse(UUIDString) + +/** + * Converts this [Uuid] value to a [NSUUID] value + * by using the default [kotlin.uuid.Uuid.toString] representation. + */ +public fun Uuid.toNsUUID(): NSUUID = NSUUID(toString()) diff --git a/kotlinx-uuid-core/src/appleMain/kotlin/kotlinx/uuid/Converter.kt b/kotlinx-uuid-core/src/appleMain/kotlin/kotlinx/uuid/Converter.kt deleted file mode 100644 index 2121eae..0000000 --- a/kotlinx-uuid-core/src/appleMain/kotlin/kotlinx/uuid/Converter.kt +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.uuid - -import kotlin.uuid.Uuid - -/** - * Converts this [platform.Foundation.NSUUID][platform.Foundation.NSUUID] value to a [kotlin.uuid.UUID][Uuid] value - * by using the [UUIDString][platform.Foundation.NSUUID.UUIDString] representation. - */ -public fun platform.Foundation.NSUUID.toKotlinUuid(): Uuid = Uuid.parse(UUIDString) - -/** - * Converts this [kotlin.uuid.Uuid][Uuid] value to a [platform.Foundation.NSUUID][platform.Foundation.NSUUID] value - * by using the default [toString] representation. - */ -public fun Uuid.toNsUUID(): platform.Foundation.NSUUID = platform.Foundation.NSUUID(toString()) diff --git a/kotlinx-uuid-core/src/appleTest/kotlin/kotlinx/uuid/NsUUIDConvertingTest.kt b/kotlinx-uuid-core/src/appleTest/kotlin/app/softwork/uuid/NsUUIDConvertingTest.kt similarity index 96% rename from kotlinx-uuid-core/src/appleTest/kotlin/kotlinx/uuid/NsUUIDConvertingTest.kt rename to kotlinx-uuid-core/src/appleTest/kotlin/app/softwork/uuid/NsUUIDConvertingTest.kt index 6bad1d6..5494cf7 100644 --- a/kotlinx-uuid-core/src/appleTest/kotlin/kotlinx/uuid/NsUUIDConvertingTest.kt +++ b/kotlinx-uuid-core/src/appleTest/kotlin/app/softwork/uuid/NsUUIDConvertingTest.kt @@ -6,7 +6,7 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid import kotlin.test.* import kotlin.uuid.Uuid diff --git a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/BinarySerializer.kt b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/BinarySerializer.kt similarity index 86% rename from kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/BinarySerializer.kt rename to kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/BinarySerializer.kt index 52a08c1..35ce1ca 100644 --- a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/BinarySerializer.kt +++ b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/BinarySerializer.kt @@ -2,7 +2,7 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid import kotlinx.serialization.* import kotlinx.serialization.builtins.* @@ -13,9 +13,9 @@ import kotlin.uuid.Uuid /** * This serializer is useful with binary formats to reduce size. You may also use it with * text formats like json, but it is not that useful (because there will be no size gain) - * and a serialized UUID is not human-readable. + * and a serialized Uuid is not human-readable. * - * With this serializer, a UUID is represented as an array of long with two elements. + * With this serializer, a Uuid is represented as an array of long with two elements. * * Example: * ```kotlin @@ -35,7 +35,7 @@ public object BinarySerializer : KSerializer { override fun deserialize(decoder: Decoder): Uuid { return decoder.decodeSerializableValue(serializer).let { array -> if (array.size != 2) { - throw SerializationException("UUID array should consist of 2 elements") + throw SerializationException("Uuid array should consist of 2 elements") } Uuid.fromLongs(array[0], array[1]) diff --git a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/NameBasedGenerator.kt b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/NameBasedGenerator.kt similarity index 75% rename from kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/NameBasedGenerator.kt rename to kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/NameBasedGenerator.kt index 5a80b14..a506765 100644 --- a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/NameBasedGenerator.kt +++ b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/NameBasedGenerator.kt @@ -2,9 +2,9 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid -import kotlinx.uuid.internal.* +import app.softwork.uuid.internal.* import kotlin.experimental.* import kotlin.uuid.Uuid @@ -15,13 +15,13 @@ import kotlin.uuid.Uuid * * See [RFC4122 sec 4.3](https://tools.ietf.org/html/rfc4122#section-4.3) */ -public fun Uuid.Companion.generateUUID(namespace: Uuid, name: String): Uuid { +public fun Uuid.Companion.generateUuid(namespace: Uuid, name: String): Uuid { val hash = sha1 { update(namespace.toByteArray()) update(name.encodeToByteArray()) } - return generateUUIDByHash(hash, version = 5) + return generateUuidByHash(hash, version = 5) } /** @@ -31,15 +31,15 @@ public fun Uuid.Companion.generateUUID(namespace: Uuid, name: String): Uuid { * * See [RFC4122 sec 4.3](https://tools.ietf.org/html/rfc4122#section-4.3) * - * It is recommended to use generateUUID(namespace, name) instead while this function + * It is recommended to use generateUuid(namespace, name) instead while this function * is more for java.util.UUID parity. */ -public fun Uuid.Companion.generateUUID(bytes: ByteArray): Uuid { +public fun Uuid.Companion.generateUuid(bytes: ByteArray): Uuid { val hash = sha1 { update(bytes) } - return generateUUIDByHash(hash, version = 5) + return generateUuidByHash(hash, version = 5) } private inline fun sha1(builder: SHA1.() -> Unit): ByteArray { @@ -48,7 +48,7 @@ private inline fun sha1(builder: SHA1.() -> Unit): ByteArray { return sha1.final() } -private fun generateUUIDByHash(hashBytes: ByteArray, version: Int): Uuid { +private fun generateUuidByHash(hashBytes: ByteArray, version: Int): Uuid { hashBytes[6] = (hashBytes[6] and 0x0f or (version shl 4).toByte()) hashBytes[8] = hashBytes[8] and 0x3f or 0x80.toByte() diff --git a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/RandomGenerator.kt b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/RandomGenerator.kt similarity index 85% rename from kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/RandomGenerator.kt rename to kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/RandomGenerator.kt index 027490c..3f90b19 100644 --- a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/RandomGenerator.kt +++ b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/RandomGenerator.kt @@ -2,13 +2,13 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid import kotlin.random.* import kotlin.uuid.Uuid /** - * Generates a random UUID v4 using the specified [random] source. + * Generates a random Uuid v4 using the specified [random] source. */ public fun Uuid.Companion.random(random: Random): Uuid { val randomBytes = random.nextBytes(16) @@ -29,6 +29,6 @@ private fun uuidFromRandomBytes(randomBytes: ByteArray): Uuid { } /** - * Generates a random UUID v4 using this [Random] instance. + * Generates a random Uuid v4 using this [Random] instance. */ public fun Random.nextUuid(): Uuid = Uuid.random(this) diff --git a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/UUID.kt b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/Uuid.kt similarity index 80% rename from kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/UUID.kt rename to kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/Uuid.kt index 26a9768..f2a74ec 100644 --- a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/UUID.kt +++ b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/Uuid.kt @@ -2,7 +2,7 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid import kotlin.uuid.Uuid @@ -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 @@ -45,13 +45,13 @@ public val Uuid.timeStamp: Long /** * 13-bits non-negative number representing a sequence number - * or a random number depending on UUID [version] and [variant]. + * or a random number depending on Uuid [version] and [variant]. */ 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 get() = toLongs { timeStampAndVersionRaw, _ -> @@ -59,7 +59,7 @@ public val Uuid.versionNumber: Int } /** - * 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 -> @@ -67,12 +67,12 @@ public val Uuid.variant: Int } /** - * 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 /** - * Node UUID part, a 48-bit non-negative number. + * Node Uuid part, a 48-bit non-negative number. * Depending on [version] and [variant] it could be one of the following: * - a MAC address * - a random number @@ -82,10 +82,10 @@ public val Uuid.node: Long get() = toLongs { _, clockSequenceVariantAndNodeRaw -> clockSequenceVariantAndNodeRaw and 0xffffffffffffL } /** - * Check the [spec] string to conform to UUID - * @return `true` if the [spec] string is a UUID string + * Check the [spec] string to conform to Uuid + * @return `true` if the [spec] string is a Uuid string */ -public fun Uuid.Companion.isValidUUIDString(spec: String): Boolean = try { +public fun Uuid.Companion.isValidUuidString(spec: String): Boolean = try { parse(spec) true } catch (_: IllegalArgumentException) { @@ -93,14 +93,14 @@ public fun Uuid.Companion.isValidUUIDString(spec: String): Boolean = try { } /** - * Convert this String to a [Uuid], or throws a [IllegalArgumentException] if [this] is a malformed UUID. + * Convert this String to a [Uuid], or throws a [IllegalArgumentException] if [this] is a malformed Uuid. */ public fun String.toUuid(): Uuid = Uuid.parse(this) /** - * Convert this String to a [Uuid], or returns null if [this] is a malformed UUID. + * Convert this String to a [Uuid], or returns null if [this] is a malformed Uuid. */ -public fun String.toUUIDOrNull(): Uuid? = try { +public fun String.toUuidOrNull(): Uuid? = try { Uuid.parse(this) } catch (_: IllegalArgumentException) { null diff --git a/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/Uuidv7.kt b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/Uuidv7.kt new file mode 100644 index 0000000..644cf5e --- /dev/null +++ b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/Uuidv7.kt @@ -0,0 +1,36 @@ +package app.softwork.uuid + +import kotlin.random.Random +import kotlin.uuid.Uuid + +private const val UNIX_48_TIMESTAMP = 0x1FFF_FFFF_FFFF_FL + +/** + * An Uuidv7 implementation according to the + * [draft](https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis#section-5.7). + * + * [timeStamp] must be an 48 bit unix timestamp. + */ +public fun Uuidv7(timeStamp: Long, random: Random): Uuid { + require(timeStamp <= UNIX_48_TIMESTAMP) { + "timeStamp $timeStamp must be <= 48 bits, was $timeStamp." + } + val value = random.nextBytes(Uuid.SIZE_BYTES) + + value[0] = ((timeStamp shr 40) and 0xFF).toByte() + value[1] = ((timeStamp shr 32) and 0xFF).toByte() + value[2] = ((timeStamp shr 24) and 0xFF).toByte() + value[3] = ((timeStamp shr 16) and 0xFF).toByte() + value[4] = ((timeStamp shr 8) and 0xFF).toByte() + value[5] = (timeStamp and 0xFF).toByte() + + value[6] = (value[6].toInt() and 0x0F or 0x70).toByte() + value[8] = (value[8].toInt() and 0x3F or 0x80).toByte() + + return Uuid.fromByteArray(value) +} + +/** + * The Uuidv7 48 bit big-endian unsigned number of Unix epoch timestamp in milliseconds + */ +public val Uuid.unixTimeStamp: Long get() = toLongs { mostSignificantBits, _ -> mostSignificantBits ushr 16 } diff --git a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/internal/SHA1.kt b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/internal/SHA1.kt similarity index 99% rename from kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/internal/SHA1.kt rename to kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/internal/SHA1.kt index 2b738b4..26095bc 100644 --- a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/internal/SHA1.kt +++ b/kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/internal/SHA1.kt @@ -2,7 +2,7 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid.internal +package app.softwork.uuid.internal /** * This is a simple Kotlin SHA-1 implementation based on wiki's description. diff --git a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/UUID7.kt b/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/UUID7.kt deleted file mode 100644 index d83a4bd..0000000 --- a/kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/UUID7.kt +++ /dev/null @@ -1,42 +0,0 @@ -package kotlinx.uuid - -import kotlin.random.Random -import kotlin.uuid.Uuid - -private const val UNIX_48_TIMESTAMP = 0x1FFF_FFFF_FFFF_FL - -/** - * An UUIDv7 implementation according to the - * [draft](https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis#section-5.7). - * - * [timeStamp] must be an 48 bit unix timestamp. - */ -public fun UUIDv7(timeStamp: Long, random: Random): Uuid { - require(timeStamp <= UNIX_48_TIMESTAMP) { - "timeStamp $timeStamp must be <= 48 bits, was $timeStamp." - } - val ( - helperTimeStampAndVersionRaw, - helperClockSequenceVariantAndNodeRaw - ) = random.nextUuid().toLongs { mostSignificantBits, leastSignificantBits -> - mostSignificantBits to leastSignificantBits - } - val leftTimeStamp = timeStamp shl 16 - // set version to 0b0111 - val leftTimeStampAndVersion = leftTimeStamp or 28672 - val randA = helperTimeStampAndVersionRaw.let { timeStampAndVersionRaw -> - (timeStampAndVersionRaw ushr 32) or - (timeStampAndVersionRaw and 0xffff0000L shl 16) or - (timeStampAndVersionRaw and 0x0fffL shl 48) - } and 4095 - val timeStampAndVersionRaw = leftTimeStampAndVersion or randA - // set variant to 0b10 - val clockSequenceVariantAndNodeRaw = (2L shl 62) or (helperClockSequenceVariantAndNodeRaw ushr 2) - - return Uuid.fromLongs(timeStampAndVersionRaw, clockSequenceVariantAndNodeRaw) -} - -/** - * The UUIDv7 48 bit big-endian unsigned number of Unix epoch timestamp in milliseconds - */ -public val Uuid.unixTimeStamp: Long get() = toLongs { mostSignificantBits, _ -> mostSignificantBits ushr 16 } diff --git a/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/BinarySerializationTest.kt b/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/BinarySerializationTest.kt similarity index 98% rename from kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/BinarySerializationTest.kt rename to kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/BinarySerializationTest.kt index 2d38b76..3422ea7 100644 --- a/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/BinarySerializationTest.kt +++ b/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/BinarySerializationTest.kt @@ -2,7 +2,7 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid import kotlinx.serialization.* import kotlinx.serialization.cbor.* diff --git a/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/GenerationTest.kt b/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/GenerationTest.kt similarity index 87% rename from kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/GenerationTest.kt rename to kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/GenerationTest.kt index 5f53d2e..6d127c5 100644 --- a/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/GenerationTest.kt +++ b/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/GenerationTest.kt @@ -2,7 +2,7 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid import kotlin.random.* import kotlin.test.* @@ -29,15 +29,15 @@ class GenerationTest { @Test fun testGenerateFromName() { - val baseUUID = Uuid.parse(SOME_UUID_STRING) - val generated = Uuid.generateUUID(baseUUID, "test") + val baseUuid = Uuid.parse(SOME_UUID_STRING) + val generated = Uuid.generateUuid(baseUuid, "test") assertEquals(5, generated.versionNumber) assertEquals("9dc3df60-4ed1-5ea9-9e66-5c2030d5827b", generated.toString()) } @Test fun testGenerateFromBytes() { - val generated = Uuid.generateUUID(SOME_UUID_STRING.explodeToBytes().toByteArray()) + val generated = Uuid.generateUuid(SOME_UUID_STRING.explodeToBytes().toByteArray()) assertEquals(5, generated.versionNumber) assertEquals("29e5befd-ca93-58bf-9ef0-30f7da112935", generated.toString()) } diff --git a/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/SHA1Test.kt b/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/SHA1Test.kt similarity index 97% rename from kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/SHA1Test.kt rename to kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/SHA1Test.kt index e79e26e..5ec7768 100644 --- a/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/SHA1Test.kt +++ b/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/SHA1Test.kt @@ -2,9 +2,9 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid -import kotlinx.uuid.internal.* +import app.softwork.uuid.internal.* import kotlin.test.* class SHA1Test { diff --git a/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/UUIDTest.kt b/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/UuidTest.kt similarity index 93% rename from kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/UUIDTest.kt rename to kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/UuidTest.kt index 50c4275..2bd49cd 100644 --- a/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/UUIDTest.kt +++ b/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/UuidTest.kt @@ -2,7 +2,7 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid import kotlin.random.Random import kotlin.test.* @@ -12,7 +12,7 @@ private const val UUID_STRING_ALL_FF: String = "ffffffff-ffff-ffff-ffff-ffffffff private const val UUID_STRING: String = "1b3e4567-e99b-13d3-a476-446657420000" internal const val SOME_UUID_STRING: String = "1b3e4567-e99b-13d3-a476-446657420000" -class UUIDTest { +class UuidTest { @Test fun testZero() { assertEquals(0, Uuid.NIL.variant) @@ -34,7 +34,7 @@ class UUIDTest { assertEquals("446657420000", uuid.node.toString(16)) assertEquals(uuid, UUID_STRING.toUuid()) - assertEquals(uuid, UUID_STRING.toUUIDOrNull()) + assertEquals(uuid, UUID_STRING.toUuidOrNull()) } @Test @@ -152,12 +152,12 @@ class UUIDTest { @Test fun testIsValidString() { - assertTrue(Uuid.isValidUUIDString(SOME_UUID_STRING)) + assertTrue(Uuid.isValidUuidString(SOME_UUID_STRING)) - assertFalse(Uuid.isValidUUIDString(SOME_UUID_STRING.drop(1))) - assertFalse(Uuid.isValidUUIDString(SOME_UUID_STRING.dropLast(1))) - assertFalse(Uuid.isValidUUIDString(SOME_UUID_STRING.replace('b', 'X'))) - assertNull(SOME_UUID_STRING.drop(1).toUUIDOrNull()) + assertFalse(Uuid.isValidUuidString(SOME_UUID_STRING.drop(1))) + assertFalse(Uuid.isValidUuidString(SOME_UUID_STRING.dropLast(1))) + assertFalse(Uuid.isValidUuidString(SOME_UUID_STRING.replace('b', 'X'))) + assertNull(SOME_UUID_STRING.drop(1).toUuidOrNull()) } @Test diff --git a/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/Uuidv7Test.kt b/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/Uuidv7Test.kt new file mode 100644 index 0000000..4cac0e0 --- /dev/null +++ b/kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/Uuidv7Test.kt @@ -0,0 +1,16 @@ +package app.softwork.uuid + +import kotlin.random.Random +import kotlin.test.* + +class Uuidv7Test { + @Test + fun test() { + val uuid = Uuidv7(1645557742000, random = Random(4242)) + + assertEquals(1645557742000, uuid.unixTimeStamp) + assertEquals("017f22e2-79b0-7a1e-ad73-08cd2f61d78a", uuid.toString()) + assertEquals(7, uuid.versionNumber) + assertEquals(5, uuid.variant) + } +} diff --git a/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/UUIDv7Test.kt b/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/UUIDv7Test.kt deleted file mode 100644 index b815a2b..0000000 --- a/kotlinx-uuid-core/src/commonTest/kotlin/kotlinx/uuid/UUIDv7Test.kt +++ /dev/null @@ -1,18 +0,0 @@ -package kotlinx.uuid - -import kotlin.random.Random -import kotlin.test.* - -class UUIDv7Test { - @Test - fun test() { - val one = UUIDv7(0x17F22E279B0, random = Random(4242)) - val two = UUIDv7(1645557742000, random = Random(4242)) - assertEquals(one, two) - - assertEquals(1645557742000, one.unixTimeStamp) - assertEquals(1645557742000, two.unixTimeStamp) - assertEquals("017f22e2-79b0-7b35-ab5c-c2334bd875e2", one.toString()) - assertEquals(7, one.versionNumber) - } -} diff --git a/kotlinx-uuid-core/src/jvmMain/kotlin/app/softwork/uuid/Converting.kt b/kotlinx-uuid-core/src/jvmMain/kotlin/app/softwork/uuid/Converting.kt new file mode 100644 index 0000000..d6f772a --- /dev/null +++ b/kotlinx-uuid-core/src/jvmMain/kotlin/app/softwork/uuid/Converting.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package app.softwork.uuid + +import java.util.* +import kotlin.uuid.Uuid + +/** + * Converts this [UUID] value to a [Uuid] value + * by using the default [java.util.UUID.toString] representation. + */ +public fun UUID.toKotlinUuid(): Uuid = Uuid.parse(toString()) + +/** + * Converts this [Uuid] value to a [Uuid] value + * by using the default [kotlin.uuid.Uuid.toString] representation. + */ +public fun Uuid.toJavaUUID(): UUID = UUID.fromString(toString()) diff --git a/kotlinx-uuid-core/src/jvmMain/kotlin/kotlinx/uuid/Converting.kt b/kotlinx-uuid-core/src/jvmMain/kotlin/kotlinx/uuid/Converting.kt deleted file mode 100644 index 9d18af4..0000000 --- a/kotlinx-uuid-core/src/jvmMain/kotlin/kotlinx/uuid/Converting.kt +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.uuid -import kotlin.uuid.Uuid - -/** - * Converts this [java.util.UUID][java.util.UUID] value to a [kotlin.uuid.Uuid][Uuid] value - * by using the default [toString] representation. - */ -public fun java.util.UUID.toKotlinUuid(): Uuid = Uuid.parse(toString()) - -/** - * Converts this [kotlin.uuid.Uuid][Uuid] value to a [java.util.UUID][java.util.UUID] value - * by using the default [toString] representation. - */ -public fun Uuid.toJavaUUID(): java.util.UUID = java.util.UUID.fromString(toString()) diff --git a/kotlinx-uuid-core/src/jvmTest/kotlin/kotlinx/uuid/JavaConvertingTest.kt b/kotlinx-uuid-core/src/jvmTest/kotlin/app/softwork/uuid/JavaConvertingTest.kt similarity index 96% rename from kotlinx-uuid-core/src/jvmTest/kotlin/kotlinx/uuid/JavaConvertingTest.kt rename to kotlinx-uuid-core/src/jvmTest/kotlin/app/softwork/uuid/JavaConvertingTest.kt index c51da11..2ae0db2 100644 --- a/kotlinx-uuid-core/src/jvmTest/kotlin/kotlinx/uuid/JavaConvertingTest.kt +++ b/kotlinx-uuid-core/src/jvmTest/kotlin/app/softwork/uuid/JavaConvertingTest.kt @@ -2,7 +2,7 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid import kotlin.test.* import kotlin.uuid.Uuid diff --git a/kotlinx-uuid-core/src/jvmTest/kotlin/kotlinx/uuid/JavaSpecificTests.kt b/kotlinx-uuid-core/src/jvmTest/kotlin/app/softwork/uuid/JavaSpecificTests.kt similarity index 94% rename from kotlinx-uuid-core/src/jvmTest/kotlin/kotlinx/uuid/JavaSpecificTests.kt rename to kotlinx-uuid-core/src/jvmTest/kotlin/app/softwork/uuid/JavaSpecificTests.kt index cba2f8b..06b249d 100644 --- a/kotlinx-uuid-core/src/jvmTest/kotlin/kotlinx/uuid/JavaSpecificTests.kt +++ b/kotlinx-uuid-core/src/jvmTest/kotlin/app/softwork/uuid/JavaSpecificTests.kt @@ -2,7 +2,7 @@ * Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.uuid +package app.softwork.uuid import java.security.* import kotlin.random.* diff --git a/kotlinx-uuid-datetime/README.md b/kotlinx-uuid-datetime/README.md index 6100275..09a7884 100644 --- a/kotlinx-uuid-datetime/README.md +++ b/kotlinx-uuid-datetime/README.md @@ -8,10 +8,10 @@ dependencies { } ``` -UUIDv7 with the current timestamp using `Clock.System` and default SecureRandom can be created using: +UUIDv7 with the current timestamp using `Clock.System` can be created using: ```kotlin -val uuid = UUIDv7() +val uuid = UUIDv7(random = Random) ``` When processing existing UUIDv7s, the timestamp bits can be interpreted as an Instant with millisecond precision using: diff --git a/kotlinx-uuid-datetime/api/kotlinx-uuid-datetime.api b/kotlinx-uuid-datetime/api/kotlinx-uuid-datetime.api index 44bd723..fcd5567 100644 --- a/kotlinx-uuid-datetime/api/kotlinx-uuid-datetime.api +++ b/kotlinx-uuid-datetime/api/kotlinx-uuid-datetime.api @@ -1,6 +1,6 @@ -public final class kotlinx/uuid/datetime/DslKt { - public static final fun UUIDv7 (Lkotlinx/datetime/Instant;Lkotlin/random/Random;)Lkotlin/uuid/Uuid; - public static synthetic fun UUIDv7$default (Lkotlinx/datetime/Instant;Lkotlin/random/Random;ILjava/lang/Object;)Lkotlin/uuid/Uuid; +public final class app/softwork/uuid/datetime/DslKt { + public static final fun Uuidv7 (Lkotlinx/datetime/Instant;Lkotlin/random/Random;)Lkotlin/uuid/Uuid; + public static synthetic fun Uuidv7$default (Lkotlinx/datetime/Instant;Lkotlin/random/Random;ILjava/lang/Object;)Lkotlin/uuid/Uuid; public static final fun getInstant (Lkotlin/uuid/Uuid;)Lkotlinx/datetime/Instant; } diff --git a/kotlinx-uuid-datetime/src/commonMain/kotlin/app/softwork/uuid/datetime/Dsl.kt b/kotlinx-uuid-datetime/src/commonMain/kotlin/app/softwork/uuid/datetime/Dsl.kt new file mode 100644 index 0000000..f4e4cdd --- /dev/null +++ b/kotlinx-uuid-datetime/src/commonMain/kotlin/app/softwork/uuid/datetime/Dsl.kt @@ -0,0 +1,16 @@ +package app.softwork.uuid.datetime + +import app.softwork.uuid.Uuidv7 +import app.softwork.uuid.unixTimeStamp +import kotlinx.datetime.Clock +import kotlinx.datetime.Instant +import kotlin.random.Random +import kotlin.uuid.Uuid + +public fun Uuidv7(timeStamp: Instant = Clock.System.now(), random: Random): Uuid = + Uuidv7(timeStamp = timeStamp.toEpochMilliseconds(), random = random) + +/** + * The Uuidv7 48 bit big-endian unsigned number of Unix epoch timestamp in milliseconds + */ +public val Uuid.instant: Instant get() = Instant.fromEpochMilliseconds(unixTimeStamp) diff --git a/kotlinx-uuid-datetime/src/commonMain/kotlin/kotlinx/uuid/datetime/Dsl.kt b/kotlinx-uuid-datetime/src/commonMain/kotlin/kotlinx/uuid/datetime/Dsl.kt deleted file mode 100644 index 6ee5c2e..0000000 --- a/kotlinx-uuid-datetime/src/commonMain/kotlin/kotlinx/uuid/datetime/Dsl.kt +++ /dev/null @@ -1,16 +0,0 @@ -package kotlinx.uuid.datetime - -import kotlinx.datetime.Clock -import kotlinx.datetime.Instant -import kotlinx.uuid.UUIDv7 -import kotlinx.uuid.unixTimeStamp -import kotlin.random.Random -import kotlin.uuid.Uuid - -public fun UUIDv7(timeStamp: Instant = Clock.System.now(), random: Random): Uuid = - UUIDv7(timeStamp = timeStamp.toEpochMilliseconds(), random = random) - -/** - * The UUIDv7 48 bit big-endian unsigned number of Unix epoch timestamp in milliseconds - */ -public val Uuid.instant: Instant get() = Instant.fromEpochMilliseconds(unixTimeStamp) diff --git a/kotlinx-uuid-datetime/src/commonTest/kotlin/kotlinx/uuid/datetime/InstantTest.kt b/kotlinx-uuid-datetime/src/commonTest/kotlin/app/softwork/uuid/datetime/InstantTest.kt similarity index 64% rename from kotlinx-uuid-datetime/src/commonTest/kotlin/kotlinx/uuid/datetime/InstantTest.kt rename to kotlinx-uuid-datetime/src/commonTest/kotlin/app/softwork/uuid/datetime/InstantTest.kt index d1ecae2..8970cf4 100644 --- a/kotlinx-uuid-datetime/src/commonTest/kotlin/kotlinx/uuid/datetime/InstantTest.kt +++ b/kotlinx-uuid-datetime/src/commonTest/kotlin/app/softwork/uuid/datetime/InstantTest.kt @@ -1,7 +1,7 @@ -package kotlinx.uuid.datetime +package app.softwork.uuid.datetime +import app.softwork.uuid.versionNumber import kotlinx.datetime.Instant -import kotlinx.uuid.versionNumber import kotlin.random.Random import kotlin.test.Test import kotlin.test.assertEquals @@ -11,9 +11,9 @@ class InstantTest { @Test fun testConversionInstant() { val timestamp = Instant.parse("2020-02-20T10:21:42Z") - val uuid = UUIDv7(timeStamp = timestamp, random = Random(4242)) + val uuid = Uuidv7(timeStamp = timestamp, random = Random(4242)) assertEquals(timestamp, uuid.instant) - assertEquals("0170621e-0ef0-7b35-ab5c-c2334bd875e2", uuid.toString()) + assertEquals("0170621e-0ef0-7a1e-ad73-08cd2f61d78a", uuid.toString()) assertEquals(7, uuid.versionNumber) } } diff --git a/kotlinx-uuid-sqldelight/README.md b/kotlinx-uuid-sqldelight/README.md index 96d3556..78c9c09 100644 --- a/kotlinx-uuid-sqldelight/README.md +++ b/kotlinx-uuid-sqldelight/README.md @@ -1,8 +1,8 @@ # Module kotlinx-uuid-sqldelight -SQLDelight uses column adapters for custom types, like this UUID. -`kotlinx-uuid-sqldelight` provides two adapters, a `UUIDStringAdapter` for a `String` and a `ByteArrayAdapter` for -a `ByteArray` representation respectively. +SQLDelight uses column adapters for custom types, like [Uuid](kotlin.uuid.Uuid). +`kotlinx-uuid-sqldelight` provides two adapters, a [UuidStringAdapter](app.softwork.uuid.sqldelight.UuidStringAdapter) for a [String] and a [UuidByteArrayAdapter](app.softwork.uuid.sqldelight.UuidByteArrayAdapter)` for +a [ByteArray] representation respectively. ```kotlin dependencies { diff --git a/kotlinx-uuid-sqldelight/api/kotlinx-uuid-sqldelight.api b/kotlinx-uuid-sqldelight/api/kotlinx-uuid-sqldelight.api index e798bf8..c585ce1 100644 --- a/kotlinx-uuid-sqldelight/api/kotlinx-uuid-sqldelight.api +++ b/kotlinx-uuid-sqldelight/api/kotlinx-uuid-sqldelight.api @@ -1,13 +1,13 @@ -public final class kotlinx/uuid/sqldelight/UuidByteArrayAdapter : app/cash/sqldelight/ColumnAdapter { - public static final field INSTANCE Lkotlinx/uuid/sqldelight/UuidByteArrayAdapter; +public final class app/softwork/uuid/sqldelight/UuidByteArrayAdapter : app/cash/sqldelight/ColumnAdapter { + public static final field INSTANCE Lapp/softwork/uuid/sqldelight/UuidByteArrayAdapter; public synthetic fun decode (Ljava/lang/Object;)Ljava/lang/Object; public fun decode ([B)Lkotlin/uuid/Uuid; public synthetic fun encode (Ljava/lang/Object;)Ljava/lang/Object; public fun encode (Lkotlin/uuid/Uuid;)[B } -public final class kotlinx/uuid/sqldelight/UuidStringAdapter : app/cash/sqldelight/ColumnAdapter { - public static final field INSTANCE Lkotlinx/uuid/sqldelight/UuidStringAdapter; +public final class app/softwork/uuid/sqldelight/UuidStringAdapter : app/cash/sqldelight/ColumnAdapter { + public static final field INSTANCE Lapp/softwork/uuid/sqldelight/UuidStringAdapter; public synthetic fun decode (Ljava/lang/Object;)Ljava/lang/Object; public fun decode (Ljava/lang/String;)Lkotlin/uuid/Uuid; public synthetic fun encode (Ljava/lang/Object;)Ljava/lang/Object; diff --git a/kotlinx-uuid-sqldelight/src/commonMain/kotlin/kotlinx/uuid/sqldelight/UuidByteArrayAdapter.kt b/kotlinx-uuid-sqldelight/src/commonMain/kotlin/app/softwork/uuid/sqldelight/UuidByteArrayAdapter.kt similarity index 88% rename from kotlinx-uuid-sqldelight/src/commonMain/kotlin/kotlinx/uuid/sqldelight/UuidByteArrayAdapter.kt rename to kotlinx-uuid-sqldelight/src/commonMain/kotlin/app/softwork/uuid/sqldelight/UuidByteArrayAdapter.kt index 0322125..00a0eea 100644 --- a/kotlinx-uuid-sqldelight/src/commonMain/kotlin/kotlinx/uuid/sqldelight/UuidByteArrayAdapter.kt +++ b/kotlinx-uuid-sqldelight/src/commonMain/kotlin/app/softwork/uuid/sqldelight/UuidByteArrayAdapter.kt @@ -1,4 +1,4 @@ -package kotlinx.uuid.sqldelight +package app.softwork.uuid.sqldelight import app.cash.sqldelight.* import kotlin.uuid.Uuid diff --git a/kotlinx-uuid-sqldelight/src/commonMain/kotlin/kotlinx/uuid/sqldelight/UuidStringAdapter.kt b/kotlinx-uuid-sqldelight/src/commonMain/kotlin/app/softwork/uuid/sqldelight/UuidStringAdapter.kt similarity index 87% rename from kotlinx-uuid-sqldelight/src/commonMain/kotlin/kotlinx/uuid/sqldelight/UuidStringAdapter.kt rename to kotlinx-uuid-sqldelight/src/commonMain/kotlin/app/softwork/uuid/sqldelight/UuidStringAdapter.kt index 79f30be..cb23d0d 100644 --- a/kotlinx-uuid-sqldelight/src/commonMain/kotlin/kotlinx/uuid/sqldelight/UuidStringAdapter.kt +++ b/kotlinx-uuid-sqldelight/src/commonMain/kotlin/app/softwork/uuid/sqldelight/UuidStringAdapter.kt @@ -1,4 +1,4 @@ -package kotlinx.uuid.sqldelight +package app.softwork.uuid.sqldelight import app.cash.sqldelight.* import kotlin.uuid.Uuid