Skip to content

Commit

Permalink
Merge pull request #951 from simple-robot/support-kt-UUID
Browse files Browse the repository at this point in the history
增加 UUID 对 `kotlin.uuid.Uuid` 的兼容API
  • Loading branch information
ForteScarlet authored Oct 10, 2024
2 parents 56f5856 + 7f859de commit 1e79a42
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 6 deletions.
3 changes: 3 additions & 0 deletions simbot-commons/simbot-common-core/api/simbot-common-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ public final class love/forte/simbot/common/id/Identifies {
public static final fun toIntOrNull (Llove/forte/simbot/common/id/ID;)Ljava/lang/Integer;
public static final fun toIntOrNull (Llove/forte/simbot/common/id/ID;Lkotlin/jvm/functions/Function1;)Ljava/lang/Integer;
public static synthetic fun toIntOrNull$default (Llove/forte/simbot/common/id/ID;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/Integer;
public static final fun toJava (Llove/forte/simbot/common/id/UUID;)Ljava/util/UUID;
public static final fun toKotlin (Llove/forte/simbot/common/id/UUID;)Lkotlin/uuid/Uuid;
public static final fun toLong (Llove/forte/simbot/common/id/ID;)J
public static final fun toLong (Llove/forte/simbot/common/id/ID;Lkotlin/jvm/functions/Function1;)J
public static synthetic fun toLong$default (Llove/forte/simbot/common/id/ID;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)J
Expand Down Expand Up @@ -365,6 +367,7 @@ public final class love/forte/simbot/common/id/Identifies {
public static final fun uuid (Ljava/util/Random;)Llove/forte/simbot/common/id/UUID;
public static final fun uuid (Ljava/util/UUID;)Llove/forte/simbot/common/id/UUID;
public static final fun uuid (Lkotlin/random/Random;)Llove/forte/simbot/common/id/UUID;
public static final fun uuid (Lkotlin/uuid/Uuid;)Llove/forte/simbot/common/id/UUID;
public static synthetic fun uuid$default (Lkotlin/random/Random;ILjava/lang/Object;)Llove/forte/simbot/common/id/UUID;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic
import kotlin.random.Random
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid


/**
Expand Down Expand Up @@ -1140,3 +1142,27 @@ public inline fun ID.toLongID(notNumerical: ID.() -> LongID = { literal.toLong()
@JvmName("toULongID")
public inline fun ID.toULongID(notNumerical: ID.() -> ULongID = { literal.toULong().ID }): ULongID =
this as? ULongID ?: (this as? NumericalID)?.toULong()?.ID ?: notNumerical()


// UUIDs

/**
* 通过一个 [kotlin.uuid.Uuid] 构建 [UUID].
*
* @since 4.6.1
*/
@ExperimentalUuidApi
@get:JvmName("uuid")
public val Uuid.ID: UUID
get() = toLongs { mostSignificantBits, leastSignificantBits ->
UUID.from(mostSignificantBits, leastSignificantBits)
}

/**
* 将一个 [UUID] 转化为 [kotlin.uuid.Uuid] .
*
* @since 4.6.1
*/
@ExperimentalUuidApi
public fun UUID.toKotlin(): Uuid =
Uuid.fromLongs(mostSignificantBits, leastSignificantBits)
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2024. ForteScarlet.
*
* Project https://github.com/simple-robot/simpler-robot
* Email [email protected]
*
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

package love.forte.simbot.common.id

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid


/**
*
* @author ForteScarlet
*/
class UUIDTests {

@Test
fun uuidParseTest() {
val uid = UUID.from(444821983651646037L, -5295967363889204301L)
assertEquals("062c533c-c3d7-4a55-b680-f1bde55273b3", uid.literal)
}

@OptIn(ExperimentalUuidApi::class)
@Test
fun ktUuidToUUIDTest() {
val ktUuid = Uuid.random()
val uuid = ktUuid.ID

ktUuid.toLongs { mostSignificantBits, leastSignificantBits ->
assertEquals(mostSignificantBits, uuid.mostSignificantBits)
assertEquals(leastSignificantBits, uuid.leastSignificantBits)
}

assertEquals(ktUuid.toString(), uuid.literal)

val ktUuid2 = uuid.toKotlin()
assertEquals(ktUuid, ktUuid2)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public fun stringIDOf(value: String): StringID = value.ID
@ID4J
public fun stringIDOf(value: CharSequence): StringID = value.ID

// UUIDs

/**
*
* @see UUID.random
Expand All @@ -114,17 +116,28 @@ public fun uuidOf(random: Random? = null): UUID =
if (random == null) UUID.random() else UUID.random(random)

/**
* 将 [java.util.UUID] 转化为 [UUID]。
*
* @see java.util.UUID.simbotUUID
*/
@JvmName("uuid")
@ID4J
public fun uuidOf(javaUuid: java.util.UUID): UUID = javaUuid.simbotUUID

/**
* 使用 [java.util.Random] 得到一个随机的 [UUID]。
* @see java.util.UUID.simbotUUID
*/
@JvmName("uuid")
@ID4J
public fun uuidOf(javaRandom: java.util.Random?): UUID =
uuidOf(javaRandom?.asKotlinRandom())

/**
* 将 [UUID] 转化为 [java.util.UUID]。
*
* @since 4.6.1
*/
@ID4J
public fun UUID.toJava(): java.util.UUID =
java.util.UUID(mostSignificantBits, leastSignificantBits)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Project https://github.com/simple-robot/simpler-robot
* Email [email protected]
*
* This file is part of the Simple Robot Library.
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -44,8 +44,7 @@ public inline val JavaUUID.simbotUUID: UUID
* 通过 [java.util.Random] 构建随机的 [UUID]。
*
*/
public fun randomUUID(random: java.util.Random): UUID {
val data = ByteArray(16)
random.nextBytes(data)
return UUID.fromData(data)
}
@OptIn(ID4J::class)
@Deprecated("Use 'uuidOf(random)' instead", ReplaceWith("uuidOf(random)"))
public fun randomUUID(random: java.util.Random): UUID =
uuidOf(random)

0 comments on commit 1e79a42

Please sign in to comment.