Class UUID
- All Implemented Interfaces:
Serializable,Comparable<UUID>
public final class UUID extends Object implements Serializable, Comparable<UUID>
There are multiple, variant layouts of UUIDs, but this class is based upon variant 2 of RFC 4122, the Leach-Salz variant. This class can be used to model alternate variants, but most of the methods will be unsupported in those cases; see each method for details.
- Since:
- 1.5
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description UUID(long mostSigBits, long leastSigBits)Constructs an instance with the specified bits. -
Method Summary
Modifier and Type Method Description intclockSequence()The clock sequence value of the version 1, variant 2 UUID as per RFC 4122.intcompareTo(UUID uuid)Compares this UUID to the specified UUID.booleanequals(Object object)Compares this UUID to another object for equality.static UUIDfromString(String uuid)Parses a UUID string with the format defined bytoString().longgetLeastSignificantBits()The 64 least significant bits of the UUID.longgetMostSignificantBits()The 64 most significant bits of the UUID.inthashCode()Returns a hash value for this UUID that is consistent with theequals(Object)method.static UUIDnameUUIDFromBytes(byte[] name)Generates a variant 2, version 3 (name-based, MD5-hashed) UUID as per RFC 4122.longnode()The node value of the version 1, variant 2 UUID as per RFC 4122.static UUIDrandomUUID()Generates a variant 2, version 4 (randomly generated number) UUID as per RFC 4122.longtimestamp()The timestamp value of the version 1, variant 2 UUID as per RFC 4122.StringtoString()Returns a string representation of this UUID in the following format, as per RFC 4122.intvariant()The variant of the UUID as per RFC 4122.intversion()The version of the variant 2 UUID as per RFC 4122.
-
Constructor Details
-
UUID
public UUID(long mostSigBits, long leastSigBits)Constructs an instance with the specified bits.
- Parameters:
mostSigBits- The 64 most significant bits of the UUID.leastSigBits- The 64 least significant bits of the UUID.
-
-
Method Details
-
randomUUID
Generates a variant 2, version 4 (randomly generated number) UUID as per RFC 4122.
- Returns:
- an UUID instance.
-
nameUUIDFromBytes
Generates a variant 2, version 3 (name-based, MD5-hashed) UUID as per RFC 4122.
- Parameters:
name- the name used as byte array to create an UUID.- Returns:
- an UUID instance.
-
fromString
Parses a UUID string with the format defined by
toString().- Parameters:
uuid- the UUID string to parse.- Returns:
- an UUID instance.
- Throws:
NullPointerException- ifuuidisnull.IllegalArgumentException- ifuuidis not formatted correctly.
-
getLeastSignificantBits
public long getLeastSignificantBits()The 64 least significant bits of the UUID.
- Returns:
- the 64 least significant bits.
-
getMostSignificantBits
public long getMostSignificantBits()The 64 most significant bits of the UUID.
- Returns:
- the 64 most significant bits.
-
version
public int version()The version of the variant 2 UUID as per RFC 4122. If the variant is not 2, then the version will be 0.
- 1 - Time-based UUID
- 2 - DCE Security UUID
- 3 - Name-based with MD5 hashing UUID (
nameUUIDFromBytes(byte[])) - 4 - Randomly generated UUID (
randomUUID()) - 5 - Name-based with SHA-1 hashing UUID
- Returns:
- an
intvalue.
-
variant
public int variant()The variant of the UUID as per RFC 4122.
- 0 - Reserved for NCS compatibility
- 2 - RFC 4122/Leach-Salz
- 6 - Reserved for Microsoft Corporation compatibility
- 7 - Reserved for future use
- Returns:
- an
intvalue.
-
timestamp
public long timestamp()The timestamp value of the version 1, variant 2 UUID as per RFC 4122.
- Returns:
- a
longvalue. - Throws:
UnsupportedOperationException- ifversion()is not 1.
-
clockSequence
public int clockSequence()The clock sequence value of the version 1, variant 2 UUID as per RFC 4122.
- Returns:
- a
longvalue. - Throws:
UnsupportedOperationException- ifversion()is not 1.
-
node
public long node()The node value of the version 1, variant 2 UUID as per RFC 4122.
- Returns:
- a
longvalue. - Throws:
UnsupportedOperationException- ifversion()is not 1.
-
compareTo
Compares this UUID to the specified UUID. The natural ordering of UUIDs is based upon the value of the bits from most significant to least significant.
- Specified by:
compareToin interfaceComparable<UUID>- Parameters:
uuid- the UUID to compare to.- Returns:
- a value of -1, 0 or 1 if this UUID is less than, equal to or
greater than
uuid.
-
equals
Compares this UUID to another object for equality. If
objectis notnull, is a UUID instance, and all bits are equal, thentrueis returned.- Overrides:
equalsin classObject- Parameters:
object- theObjectto compare to.- Returns:
trueif this UUID is equal toobjectorfalseif not.- See Also:
Object.hashCode()
-
hashCode
public int hashCode()Returns a hash value for this UUID that is consistent with the
equals(Object)method.- Overrides:
hashCodein classObject- Returns:
- an
intvalue. - See Also:
Object.equals(java.lang.Object)
-
toString
Returns a string representation of this UUID in the following format, as per RFC 4122.
UUID = time-low "-" time-mid "-" time-high-and-version "-" clock-seq-and-reserved clock-seq-low "-" node time-low = 4hexOctet time-mid = 2hexOctet time-high-and-version = 2hexOctet clock-seq-and-reserved = hexOctet clock-seq-low = hexOctet node = 6hexOctet hexOctet = hexDigit hexDigit hexDigit = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" / "a" / "b" / "c" / "d" / "e" / "f" / "A" / "B" / "C" / "D" / "E" / "F"
-