Java Reference
In-Depth Information
22.8.
UUID
The
UUID
class provides immutable objects that represent universally
unique identifiers. These are 128-bit values that are generated in such a
way that they are guaranteed to be unique. There are four different ver-
sions of
UUID
values, generally known as types 1, 2, 3, and 4. You can
ask a
UUID
object for its version by using the
version
method.
The usual way to create a type 4 (random)
UUID
is to use the static factory
method
randomUUID
. A type 3 (name-based)
UUID
can be obtained from the
static factory method
nameUUIDFromBytes
, which takes an array of bytes
representing the name.
You can directly construct an arbitrary form
UUID
by supplying two
long
values that represent the upper and lower halves. Of course, there are
no guarantees about uniqueness for such a constructed
UUID
it is up to
you to supply appropriate values. The two halves of a
UUID
object can be
retrieved as
long
values from the
getMostSignificantBits
and
getLeastSig-
nificantBits
methods.
The layout of a
UUID
is determined by which
variant
is used to implement
it. There are four variants: 0, 2, 6, and 7. The
UUID
class always generates
variant 2
UUID
valuesknown as the
Leach-Salz
variant. The details of
these variants are not important for this discussion. You can ask a
UUID
object for its variant by using the
variant
method. Two
UUID
objects are
equal
only if they have the same variant and 128-bit value.
The
toString
method returns a string representation of a
UUID
that can be
passed into the static
fromString
factory method to get a
UUID
object.
The remaining methods are applicable only to type 1 (time-based)
UUID
values, and throw
UnsupportedOperationException
if invoked on other
types:
public long
timestamp()
Returns the 60-bit timestamp associated with this
UUID
.