Skip to content

Commit

Permalink
More deprecations (benasher44#69)
Browse files Browse the repository at this point in the history
This adds more deprecations to prepare for the 0.0.8, where I plan to use java.util.UUID on JVM.
  • Loading branch information
benasher44 authored Dec 29, 2019
1 parent cdfe9c7 commit d0488fc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ project adheres to [Semantic Versioning](https://semver.org/).
- `Uuid(msb: Long, lsb: Long)` is now a constructor in stead of a free function (#66)
- Removed empty `Uuid()` constructor (#66)
- Deprecate `Uuid(bytes)`, which will eventually become `internal` (#67)
- Deprecate `.uuid` in favor of `.bytes` (#69)

## [0.0.6] - 2019-11-21
### Changed
Expand Down
4 changes: 2 additions & 2 deletions src/cocoaTest/kotlin/CocoaUuidTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CocoaUuidTest {
@Test
fun `UUID.toString() matches NSUUID`() {
val uuidL = uuid4()
val nativeUuidString = uuidL.uuid.usePinned {
val nativeUuidString = uuidL.bytes.usePinned {
NSUUID(it.addressOf(0).reinterpret()).UUIDString
}.toLowerCase()
assertEquals(uuidL.toString(), nativeUuidString)
Expand All @@ -27,7 +27,7 @@ class CocoaUuidTest {
nativeBytes.usePinned {
nativeUuid.getUUIDBytes(it.addressOf(0).reinterpret())
}
assertTrue(uuidL.uuid.contentEquals(nativeBytes))
assertTrue(uuidL.bytes.contentEquals(nativeBytes))
}

@Test
Expand Down
33 changes: 33 additions & 0 deletions src/commonMain/kotlin/deprecations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@file:Suppress("RedundantVisibilityModifier")

import com.benasher44.uuid.Uuid
import com.benasher44.uuid.uuidFrom

@Deprecated("Use `Uuid` instead.", ReplaceWith("Uuid"))
public typealias UUID = Uuid

/**
* Parses a UUID from a String
*
* @param from The String, from which to deserialize the UUID
* @return a UUID, if the string is a valid UUID string
*/
@Deprecated(
message = "Use uuidFrom() instead. This will be removed in the next release",
replaceWith = ReplaceWith("uuidFrom(from)"),
level = DeprecationLevel.ERROR
)
public fun Uuid.Companion.parse(from: String): Uuid? {
return try {
uuidFrom(from)
} catch (_: Throwable) {
null
}
}

@Deprecated(
message = "Use uuidFrom() instead. This will be removed in the next release.",
replaceWith = ReplaceWith("Uuid.bytes")
)
public val Uuid.uuid: ByteArray
get() = uuidBytes
49 changes: 16 additions & 33 deletions src/commonMain/kotlin/uuid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package com.benasher44.uuid

import kotlin.DeprecationLevel.WARNING
import kotlin.native.concurrent.SharedImmutable

// Number of bytes in a UUID
Expand All @@ -30,17 +29,14 @@ internal val UUID_HYPHEN_INDICES = listOf(8, 13, 18, 23)
@SharedImmutable
internal val UUID_CHARS = ('0'..'9') + ('a'..'f')

@Deprecated("Use `Uuid` instead.", ReplaceWith("Uuid"))
public typealias UUID = Uuid

/**
* A RFC4122 UUID
*
* @property uuid The underlying UUID bytes
* @constructor Constructs a new UUID from the given ByteArray
* @throws IllegalArgumentException, if uuid.count() is not 16
*/
public class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidOf(uuid)")) constructor(val uuid: ByteArray) {
public class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidOf(uuid)")) constructor(internal val uuidBytes: ByteArray) {

/**
* Construct new [Uuid] instance using the given data.
Expand All @@ -51,17 +47,23 @@ public class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidOf(uuid)
@Suppress("DEPRECATION")
public constructor(msb: Long, lsb: Long) : this(fromBits(msb, lsb))

/**
* The UUID's raw bytes
*/
public val bytes: ByteArray
get() = uuidBytes

/** The most significant 64 bits of this UUID's 128 bit value. */
val mostSignificantBits: Long by lazy {
public val mostSignificantBits: Long by lazy {
(0..7).fold(0L) { bits, i ->
bits shl 8 or (uuid[i].toLong() and 0xff)
bits shl 8 or (uuidBytes[i].toLong() and 0xff)
}
}

/** The least significant 64 bits of this UUID's 128 bit value. */
val leastSignificantBits: Long by lazy {
public val leastSignificantBits: Long by lazy {
(8..15).fold(0L) { bits, i ->
bits shl 8 or (uuid[i].toLong() and 0xff)
bits shl 8 or (uuidBytes[i].toLong() and 0xff)
}
}

Expand Down Expand Up @@ -102,8 +104,8 @@ public class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidOf(uuid)
get() = ((mostSignificantBits shr 12) and 0x0f).toInt()

init {
require(uuid.count() == UUID_BYTES) {
"Invalid UUID bytes. Expected $UUID_BYTES bytes; found ${uuid.count()}"
require(uuidBytes.count() == UUID_BYTES) {
"Invalid UUID bytes. Expected $UUID_BYTES bytes; found ${uuidBytes.count()}"
}
this.freeze()
}
Expand All @@ -122,25 +124,6 @@ public class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidOf(uuid)
}
}

/**
* Parses a UUID from a String
*
* @param from The String, from which to deserialize the UUID
* @return a UUID, if the string is a valid UUID string
*/
@Deprecated(
message = "Use uuidFrom() instead.",
replaceWith = ReplaceWith("uuidFrom(from)"),
level = WARNING
)
fun parse(from: String): Uuid? {
return try {
uuidFrom(from)
} catch (_: Throwable) {
null
}
}

/** The ranges of sections of UUID bytes, to be separated by hyphens */
private val uuidByteRanges: List<IntRange> = listOf(
0 until 4,
Expand All @@ -159,7 +142,7 @@ public class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidOf(uuid)
var charIndex = 0
for (range in uuidByteRanges) {
for (i in range) {
val octetPair = uuid[i]
val octetPair = uuidBytes[i]
// convert the octet pair in this byte into 2 characters
val left = octetPair.toInt().shr(4) and 0b00001111
val right = octetPair.toInt() and 0b00001111
Expand All @@ -178,13 +161,13 @@ public class Uuid @Deprecated("Use `uuidOf` instead.", ReplaceWith("uuidOf(uuid)
*/
override fun equals(other: Any?): Boolean {
if (other !is Uuid) return false
return other.uuid.contentEquals(uuid)
return other.uuidBytes.contentEquals(uuidBytes)
}

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

/**
Expand Down

0 comments on commit d0488fc

Please sign in to comment.