java.util

Class UUID

Implemented Interfaces:
Comparable<T>, Serializable

public final class UUID
extends Object
implements Serializable, Comparable<T>

This class represents a 128-bit UUID value. There are several types of UUID, and while this class can be used to store them, only the Leach-Salz (variant 2) UUID specified in RFC-4122 will give meaningful results from the method calls. See: http://tools.ietf.org/html/4122 for the details The format of a Leach-Salz (variant 2) time-based (version 1) UUID is as follows: time_low - upper 32 bits of the most significant 64 bits, this is the least-significant part of the timestamp. time_mid - bits 16-31 of the most significant 64 bits, this is the middle portion of the timestamp. version - bits 8-15 of the most significant 64 bits. time_hi - bits 0-7 of the most significant 64 bits, the most significant portion of the timestamp. clock_and_reserved - bits 48-63 of the least significant 64 bits. a variable number of bits hold the variant (see the spec) node identifier - bits 0-47 of the least signficant 64 bits. These fields are valid only for version 1, in the remaining versions, only the version and variant fields are set, all others are used for data.
Since:
1.5
See Also:
Serialized Form

Constructor Summary

UUID(long mostSigBits, long leastSigBits)
Constructs a new UUID.

Method Summary

int
clockSequence()
Returns the clock-sequence value of this UUID.
int
compareTo(UUID o)
Compare this UUID to another.
boolean
equals(Object obj)
Compare a (UUID) object to this one
static UUID
fromString(String name)
Creates a UUID object from a Sting representation.
long
getLeastSignificantBits()
Returns the least significant 64 bits of the UUID as a long.
long
getMostSignificantBits()
Returns the most significant 64 bits of the UUID as a long.
int
hashCode()
Returns a hash of this UUID.
static UUID
nameUUIDFromBytes(byte[] name)
Creates a UUID version 3 object (name based with MD5 hashing) from a series of bytes representing a name.
long
node()
Returns the 48-bit node value in a long.
static UUID
randomUUID()
Generate a Leach-Salz (Variant 2) randomly generated (version 4) UUID.
long
timestamp()
Returns the 60-bit timestamp value of the UUID in a long.
String
toString()
Returns a String representation of the UUID.
int
variant()
Returns the variant of the UUID This may be: 0 = Reserved for NCS backwards-compatibility 2 = Leach-Salz (supports the other methods in this class) 6 = Reserved for Microsoft backwards-compatibility 7 = (reserved for future use)
int
version()
Returns the version # of the UUID.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

UUID

public UUID(long mostSigBits,
            long leastSigBits)
Constructs a new UUID.
Since:
1.5

Method Details

clockSequence

public int clockSequence()
Returns the clock-sequence value of this UUID. This field only exists in a time-based (version 1) UUID.
Throws:
UnsupportedOperationException - if the UUID type is not 1.

compareTo

public int compareTo(UUID o)
Compare this UUID to another. The comparison is performed as between two 128-bit integers.
Returns:
-1 if this <32val, 0 if they are equal, 1 if this > val.

equals

public boolean equals(Object obj)
Compare a (UUID) object to this one
Overrides:
equals in interface Object

fromString

public static UUID fromString(String name)
Creates a UUID object from a Sting representation. For the format of the string,
Returns:
a new UUID object.
See Also:
toString()

getLeastSignificantBits

public long getLeastSignificantBits()
Returns the least significant 64 bits of the UUID as a long.

getMostSignificantBits

public long getMostSignificantBits()
Returns the most significant 64 bits of the UUID as a long.

hashCode

public int hashCode()
Returns a hash of this UUID.
Overrides:
hashCode in interface Object

nameUUIDFromBytes

public static UUID nameUUIDFromBytes(byte[] name)
Creates a UUID version 3 object (name based with MD5 hashing) from a series of bytes representing a name.

node

public long node()
Returns the 48-bit node value in a long. This field only exists in a time-based (version 1) UUID.
Throws:
UnsupportedOperationException - if the UUID type is not 1.

randomUUID

public static UUID randomUUID()
Generate a Leach-Salz (Variant 2) randomly generated (version 4) UUID.

timestamp

public long timestamp()
Returns the 60-bit timestamp value of the UUID in a long. This field only exists in a time-based (version 1) UUID.
Throws:
UnsupportedOperationException - if the UUID type is not 1.

toString

public String toString()
Returns a String representation of the UUID. The format of the standard string representation (given in RFC4122) is: time-low "-" time-mid "-" time-high-and-version "-" clock-seq-and-reserved clock-seq-low "-" node Where each field is represented as a hex string.
Overrides:
toString in interface Object
Returns:
the String representation.

variant

public int variant()
Returns the variant of the UUID This may be: 0 = Reserved for NCS backwards-compatibility 2 = Leach-Salz (supports the other methods in this class) 6 = Reserved for Microsoft backwards-compatibility 7 = (reserved for future use)

version

public int version()
Returns the version # of the UUID. Valid version numbers for a variant 2 UUID are: 1 = Time based UUID 2 = DCE security UUID 3 = Name-based UUID using MD5 hashing 4 = Randomly generated UUID 5 = Name-based UUID using SHA-1 hashing
Returns:
the version number

UUID.java -- Class that represents a UUID object. Copyright (C) 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.