-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #951 from simple-robot/support-kt-UUID
增加 UUID 对 `kotlin.uuid.Uuid` 的兼容API
- Loading branch information
Showing
5 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...commons/simbot-common-core/src/commonTest/kotlin/love/forte/simbot/common/id/UUIDTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) |