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.
import kotlin.uuid.*
fun main(args: Array<String>) {
//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(args: Array<String>) {
//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(args: Array<String>) {
//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
}
Functions
equals
Checks whether this uuid is equal to the specified other object.
fun equals(other: Any?): Boolean
hashCode
Returns a hash code value for the object. The general contract of hashCode
is:
fun hashCode(): Int
toByteArray
Returns a byte array representation of this uuid.
fun toByteArray(): ByteArray
toHexString
Returns the hexadecimal string representation of this uuid without hyphens.
fun toHexString(): String
toLongs
Executes the specified block of code, providing access to the uuid's bits in the form of two Long values.
fun <T> toLongs(
action: (mostSignificantBits: Long, leastSignificantBits: Long) -> T
): T
toString
Returns the standard string representation of this uuid.
fun toString(): String
Companion Object Properties
LEXICAL_ORDER
A Comparator that lexically orders uuids.
val LEXICAL_ORDER: Comparator<Uuid>
SIZE_BITS
The number of bits used to represent an instance of Uuid in a binary form.
const val SIZE_BITS: Int
SIZE_BYTES
The number of bytes used to represent an instance of Uuid in a binary form.
const val SIZE_BYTES: Int
Companion Object Functions
fromLongs
Creates a uuid from specified 128 bits split into two 64-bit Longs.
fun fromLongs(
mostSignificantBits: Long,
leastSignificantBits: Long
): Uuid
parse
Parses a uuid from the standard string representation as described in Uuid.toString.
fun parse(uuidString: String): Uuid
parseHex
Parses a uuid from the hexadecimal string representation as described in Uuid.toHexString.
fun parseHex(hexString: String): Uuid
Extension Functions
toJavaUuid
Converts this kotlin.uuid.Uuid value to the corresponding java.util.UUID value.