Skip to content

Commit

Permalink
Changed extension vals to actual vals (#79)
Browse files Browse the repository at this point in the history
This is an ABI breaking change since we removed the extension functions. Nothing changed from an API perspective. I used this chance and improved a few releated definitions.
  • Loading branch information
Fleshgrinder authored Feb 17, 2020
1 parent d4c1efe commit 7b4c548
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 58 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ kotlin.code.style=official
kotlin.incremental=true

GROUP=com.benasher44
VERSION=0.0.8-SNAPSHOT
VERSION=0.1.0-SNAPSHOT

POM_URL=https://github.com/benasher44/uuid/
POM_SCM_URL=https://github.com/benasher44/uuid/
Expand Down
14 changes: 6 additions & 8 deletions src/commonMain/kotlin/uuid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,18 @@ public expect class Uuid : Comparable<Uuid> {
*/
// @SinceKotlin("1.x")
public constructor(msb: Long, lsb: Long)

/** The most significant 64 bits of this UUID's 128 bit value. */
public val mostSignificantBits: Long

/** The least significant 64 bits of this UUID's 128 bit value. */
public val leastSignificantBits: Long
}

/** Gets the raw UUID bytes */
// @SinceKotlin("1.x")
public expect val Uuid.bytes: ByteArray

/** The most significant 64 bits of this UUID's 128 bit value. */
// @SinceKotlin("1.x")
public expect val Uuid.mostSignificantBits: Long

/** The least significant 64 bits of this UUID's 128 bit value. */
// @SinceKotlin("1.x")
public expect val Uuid.leastSignificantBits: Long

/**
* The variant of the [Uuid], determines the interpretation of the bits.
*
Expand Down
46 changes: 16 additions & 30 deletions src/jvmMain/kotlin/uuid.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.benasher44.uuid

import java.nio.ByteBuffer
import java.util.UUID

// This suppression is required to force Kotlin to accept the Java getters as
// val definitions:
//
// - https://youtrack.jetbrains.com/issue/KT-15620
// - https://youtrack.jetbrains.com/issue/KT-27413
@Suppress("NO_ACTUAL_CLASS_MEMBER_FOR_EXPECTED_CLASS")
public actual typealias Uuid = UUID

public actual fun uuidOf(bytes: ByteArray): Uuid {
require(bytes.count() == UUID_BYTES) {
"Invalid UUID bytes. Expected $UUID_BYTES bytes; found ${bytes.count()}"
}
val msb = (0 until 8).fold(0L) { m, i ->
(m shl 8) or (bytes[i].toLong() and 0xff)
require(bytes.size == UUID_BYTES) {
"Invalid UUID bytes. Expected $UUID_BYTES bytes; found ${bytes.size}"
}
val lsb = (8 until 16).fold(0L) { l, i ->
(l shl 8) or (bytes[i].toLong() and 0xff)
}
return UUID(msb, lsb)
return ByteBuffer.wrap(bytes).let { UUID(it.long, it.long) }
}

// Implementation Notes:
Expand Down Expand Up @@ -90,30 +91,15 @@ private fun String.segmentToLong(start: Int, end: Int): Long {
return result
}

public actual fun uuid4(): Uuid = UUID.randomUUID()
@Suppress("NOTHING_TO_INLINE")
public actual inline fun uuid4(): Uuid =
UUID.randomUUID()

public actual val UUID.bytes: ByteArray
get() = ByteArray(UUID_BYTES) { index ->
val bits: Long
val offsetIndex: Int
if (index < 8) {
bits = this.mostSignificantBits
offsetIndex = index
} else {
bits = this.leastSignificantBits
offsetIndex = index - 8
}
((bits ushr ((7 - offsetIndex) * 8)) and 0xff).toByte()
}

public actual val UUID.mostSignificantBits: Long
get() = mostSignificantBits

public actual val UUID.leastSignificantBits: Long
get() = mostSignificantBits
get() = ByteBuffer.allocate(16).putLong(mostSignificantBits).putLong(leastSignificantBits).array()

public actual val UUID.version: Int
public actual inline val UUID.version: Int
get() = version()

public actual val UUID.variant: Int
public actual inline val UUID.variant: Int
get() = variant()
37 changes: 18 additions & 19 deletions src/nonJvmMain/kotlin/uuid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public actual class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidO
@Suppress("DEPRECATION")
public actual constructor(msb: Long, lsb: Long) : this(fromBits(msb, lsb))

public actual val mostSignificantBits: Long
get() = uuidBytes.bits(0, 8)

public actual val leastSignificantBits: Long
get() = uuidBytes.bits(8, 16)

init {
require(uuidBytes.count() == UUID_BYTES) {
"Invalid UUID bytes. Expected $UUID_BYTES bytes; found ${uuidBytes.count()}"
Expand All @@ -22,6 +28,14 @@ public actual class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidO
}

companion object {
private fun ByteArray.bits(start: Int, end: Int): Long {
var b = 0L
var i = start
do {
b = (b shl 8) or (get(i).toLong() and 0xff)
} while (++i < end)
return b
}

/** Creates the [ByteArray] from most and least significant bits */
private fun fromBits(msb: Long, lsb: Long) = ByteArray(UUID_BYTES).also { bytes ->
Expand Down Expand Up @@ -78,15 +92,14 @@ public actual class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidO
/**
* @return true if other is a UUID and its uuid bytes are equal to this one
*/
override fun equals(other: Any?): Boolean {
if (other !is Uuid) return false
return other.uuidBytes.contentEquals(uuidBytes)
}
override fun equals(other: Any?): Boolean =
other is Uuid && uuidBytes.contentEquals(other.uuidBytes)

/**
* @return The hashCode of the uuid bytes
*/
override fun hashCode(): Int = uuidBytes.contentHashCode()
override fun hashCode(): Int =
uuidBytes.contentHashCode()

/**
* @return The result of comparing [uuidBytes] between this and [other]
Expand All @@ -103,20 +116,6 @@ public actual class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidO
public actual val Uuid.bytes: ByteArray
get() = uuidBytes

public actual val Uuid.mostSignificantBits: Long
get() {
return (0..7).fold(0L) { bits, i ->
bits shl 8 or (uuidBytes[i].toLong() and 0xff)
}
}

public actual val Uuid.leastSignificantBits: Long
get() {
return (8..15).fold(0L) { bits, i ->
bits shl 8 or (uuidBytes[i].toLong() and 0xff)
}
}

public actual val Uuid.variant: Int
get() = (leastSignificantBits.ushr((64 - (leastSignificantBits ushr 62)).toInt()) and (leastSignificantBits shr 63)).toInt()

Expand Down

0 comments on commit 7b4c548

Please sign in to comment.