Uuid

Represents a Universally Unique Identifier (UUID), also known as a Globally Unique Identifier (GUID).

A UUID is a 128-bit value used to uniquely identify items universally. They are particularly useful in environments lacking central registration authority or coordination mechanism for generating identifiers, making UUIDs highly suitable for distributed systems.

The standard textual representation of a UUID, also known as the "hex-and-dash" format, is: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where 'x' represents a hexadecimal digit, e.g., "550e8400-e29b-41d4-a716-446655440000". This format includes hyphens to separate different parts of the UUID, enhancing human readability.

This class provides utility functions for:

  • Generating UUIDs.

  • Creating UUIDs from given 128 bits.

  • Parsing UUIDs from and formatting them to their string representations.

  • Converting UUIDs to and from arrays of bytes.

  • Comparing UUIDs to establish ordering or equality.

Since Kotlin

2.0

Samples

import kotlin.uuid.*

fun main() { 
   //sampleStart 
   val uuid = Uuid.parse("550E8400-e29b-41d4-A716-446655440000") // case insensitive
println(uuid) // 550e8400-e29b-41d4-a716-446655440000 
   //sampleEnd
}
import kotlin.uuid.*

fun main() { 
   //sampleStart 
   val byteArray = byteArrayOf(
    0x55, 0x0e, 0x84.toByte(), 0x00, 0xe2.toByte(), 0x9b.toByte(), 0x41, 0xd4.toByte(),
    0xa7.toByte(), 0x16, 0x44, 0x66, 0x55, 0x44, 0x00, 0x00
)
val uuid = Uuid.fromByteArray(byteArray)
println(uuid) // 550e8400-e29b-41d4-a716-446655440000 
   //sampleEnd
}
import kotlin.uuid.*

fun main() { 
   //sampleStart 
   // Generates a random and unique uuid each time
val uuid1 = Uuid.random()
val uuid2 = Uuid.random()
val uuid3 = Uuid.random()

println(uuid1 == uuid2) // false
println(uuid1 == uuid3) // false
println(uuid2 == uuid3) // false 
   //sampleEnd
}

Types

Link copied to clipboard
object Companion
Since Kotlin 2.0

Functions

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Checks whether this uuid is equal to the specified other object.

Since Kotlin 2.0
Link copied to clipboard
open override fun hashCode(): Int
Since Kotlin 2.0
Link copied to clipboard

Returns a byte array representation of this uuid.

Since Kotlin 2.0
Link copied to clipboard

Returns the hexadecimal string representation of this uuid without hyphens.

Since Kotlin 2.0
Link copied to clipboard

Converts this kotlin.uuid.Uuid value to the corresponding java.util.UUID value.

Since Kotlin 2.0
Link copied to clipboard
inline fun <T> toLongs(action: (mostSignificantBits: Long, leastSignificantBits: Long) -> T): T

Executes the specified block of code, providing access to the uuid's bits in the form of two Long values.

Since Kotlin 2.0
Link copied to clipboard
open override fun toString(): String

Returns the standard string representation of this uuid.

Since Kotlin 2.0
Link copied to clipboard
inline fun <T> toULongs(action: (mostSignificantBits: ULong, leastSignificantBits: ULong) -> T): T

Executes a specified block of code, providing access to the uuid's bits in the form of two ULong values.

Since Kotlin 2.0