Skip to content

Commit

Permalink
Go back to Uuid msb/lsb constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
benasher44 committed Dec 29, 2019
1 parent beaafd3 commit 6116f30
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ project adheres to [Semantic Versioning](https://semver.org/).
## [0.0.7] - TBD
### Changed
- Deprecate Uuid.parse() in favor of Uuid.fromString(), which returns a non-null Uuid or throws an error for an invalid string, in line with Java's UUID.fromString().
- `uuidOf(msb: Long, lsb: Long)` function (#52)
- Deprecated `Uuid(msb: Long, lsb: Long)` in favor of `uuidOf` (#64)
- `Uuid(msb: Long, lsb: Long)` is now a constructor in stead of a free function (#66)

## [0.0.6] - 2019-11-21
### Changed
Expand Down
53 changes: 18 additions & 35 deletions src/commonMain/kotlin/uuid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ internal const val UUID_STRING_LENGTH = 36
@Deprecated("Use `Uuid` instead.", ReplaceWith("Uuid"))
public typealias UUID = Uuid

// @SinceKotlin("1.x")
@Suppress("FunctionName")
@Deprecated("Use uuidOf instead", ReplaceWith("uuidOf()"))
public fun Uuid(msb: Long, lsb: Long): Uuid = uuidOf(msb, lsb)

/**
* A RFC4122 UUID
*
Expand All @@ -24,8 +19,14 @@ public fun Uuid(msb: Long, lsb: Long): Uuid = uuidOf(msb, lsb)
* @throws IllegalArgumentException, if uuid.count() is not 16
*/
public class Uuid(val uuid: ByteArray) {
@Deprecated("use uuid4 instead", ReplaceWith("uuid4()"))
constructor() : this(genUuid())

/**
* Construct new [Uuid] instance using the given data.
*
* @param msb The 64 most significant bits of the [Uuid].
* @param lsb The 64 least significant bits of the [Uuid].
*/
public constructor(msb: Long, lsb: Long) : this(fromBits(msb, lsb))

/** The most significant 64 bits of this UUID's 128 bit value. */
val mostSignificantBits: Long by lazy {
Expand Down Expand Up @@ -86,15 +87,16 @@ public class Uuid(val uuid: ByteArray) {

companion object {

/** Generates a random UUID */
private fun genUuid(): ByteArray {
val bytes = getRandomUuidBytes()
// Set the version bit
bytes[6] = ((bytes[6].toInt() and 0x0F) or 0x40).toByte()

// Set the 0 and 1 bits
bytes[8] = ((bytes[8].toInt() and 0b00111111) or 0b10000000).toByte()
return bytes
/** Creates the [ByteArray] from most and least significant bits */
private fun fromBits(msb: Long, lsb: Long) = ByteArray(UUID_BYTES).also { bytes ->
(7 downTo 0).fold(msb) { x, i ->
bytes[i] = (x and 0xff).toByte()
x shr 8
}
(15 downTo 8).fold(lsb) { x, i ->
bytes[i] = (x and 0xff).toByte()
x shr 8
}
}

// Ranges of non-hyphen characters in a UUID string
Expand Down Expand Up @@ -218,25 +220,6 @@ public class Uuid(val uuid: ByteArray) {
override fun hashCode(): Int = uuid.contentHashCode()
}

/**
* Construct new [Uuid] instance using the given data.
*
* @param msb The 64 most significant bits of the [Uuid].
* @param lsb The 64 least significant bits of the [Uuid].
*/
// @SinceKotlin("1.x")
public fun uuidOf(msb: Long, lsb: Long): Uuid =
Uuid(ByteArray(UUID_BYTES).also { bytes ->
(7 downTo 0).fold(msb) { x, i ->
bytes[i] = (x and 0xff).toByte()
x shr 8
}
(15 downTo 8).fold(lsb) { x, i ->
bytes[i] = (x and 0xff).toByte()
x shr 8
}
})

/**
* Set the [Uuid.version] on this big-endian [ByteArray]. The [Uuid.variant] is
* always set to the RFC 4122 one since this is the only variant supported by
Expand Down
10 changes: 5 additions & 5 deletions src/commonTest/kotlin/UuidTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ class UuidTest {
}

@Test
fun uuidOf_from_msb_and_lsb() {
assertEquals("00000000-0000-0000-0000-000000000000", uuidOf(0, 0).toString(), "min")
assertEquals("00000000-0000-0000-ffff-ffffffffffff", uuidOf(0, -1).toString(), "lsb")
assertEquals("ffffffff-ffff-ffff-0000-000000000000", uuidOf(-1, 0).toString(), "msb")
assertEquals("ffffffff-ffff-ffff-ffff-ffffffffffff", uuidOf(-1, -1).toString(), "max")
fun uuid_from_msb_and_lsb() {
assertEquals("00000000-0000-0000-0000-000000000000", Uuid(0, 0).toString(), "min")
assertEquals("00000000-0000-0000-ffff-ffffffffffff", Uuid(0, -1).toString(), "lsb")
assertEquals("ffffffff-ffff-ffff-0000-000000000000", Uuid(-1, 0).toString(), "msb")
assertEquals("ffffffff-ffff-ffff-ffff-ffffffffffff", Uuid(-1, -1).toString(), "max")
}

@Test
Expand Down

0 comments on commit 6116f30

Please sign in to comment.