UUID is an immutable representation of a 128-bit universally unique
identifier (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.
| Method from java.util.UUID Detail: |
public int clockSequence() {
if (version != 1) {
throw new UnsupportedOperationException();
}
return clockSequence;
}
The clock sequence value of the version 1, variant 2 UUID as per RFC 4122.
|
public int compareTo(UUID uuid) {
if (uuid == this) {
return 0;
}
if (this.mostSigBits != uuid.mostSigBits) {
return this.mostSigBits < uuid.mostSigBits ? -1 : 1;
}
assert this.mostSigBits == uuid.mostSigBits;
if (this.leastSigBits != uuid.leastSigBits) {
return this.leastSigBits < uuid.leastSigBits ? -1 : 1;
}
assert this.leastSigBits == uuid.leastSigBits;
return 0;
}
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.
|
public boolean equals(Object object) {
if (object == null) {
return false;
}
if (this == object) {
return true;
}
if (!(object instanceof UUID)) {
return false;
}
UUID that = (UUID) object;
return (this.leastSigBits == that.leastSigBits)
&& (this.mostSigBits == that.mostSigBits);
}
Compares this UUID to another object for equality. If {@code object}
is not {@code null}, is a UUID instance, and all bits are equal, then
{@code true} is returned.
|
public static UUID fromString(String uuid) {
if (uuid == null) {
throw new NullPointerException();
}
int[] position = new int[5];
int lastPosition = 1;
int startPosition = 0;
int i = 0;
for (; i < position.length && lastPosition > 0; i++) {
position[i] = uuid.indexOf("-", startPosition); //$NON-NLS-1$
lastPosition = position[i];
startPosition = position[i] + 1;
}
// should have and only can have four "-" in UUID
if (i != position.length || lastPosition != -1) {
throw new IllegalArgumentException(Messages.getString("luni.47") + uuid); //$NON-NLS-1$
}
long m1 = Long.parseLong(uuid.substring(0, position[0]), 16);
long m2 = Long.parseLong(uuid.substring(position[0] + 1, position[1]),
16);
long m3 = Long.parseLong(uuid.substring(position[1] + 1, position[2]),
16);
long lsb1 = Long.parseLong(
uuid.substring(position[2] + 1, position[3]), 16);
long lsb2 = Long.parseLong(uuid.substring(position[3] + 1), 16);
long msb = (m1 < < 32) | (m2 < < 16) | m3;
long lsb = (lsb1 < < 48) | lsb2;
return new UUID(msb, lsb);
}
|
public long getLeastSignificantBits() {
return leastSigBits;
}
|
public long getMostSignificantBits() {
return mostSigBits;
}
|
public int hashCode() {
return hash;
}
|
public static UUID nameUUIDFromBytes(byte[] name) {
if (name == null) {
throw new NullPointerException();
}
byte[] hash;
try {
MessageDigest md = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
hash = md.digest(name);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
long msb = (hash[0] & 0xFFL) < < 56;
msb |= (hash[1] & 0xFFL) < < 48;
msb |= (hash[2] & 0xFFL) < < 40;
msb |= (hash[3] & 0xFFL) < < 32;
msb |= (hash[4] & 0xFFL) < < 24;
msb |= (hash[5] & 0xFFL) < < 16;
msb |= (hash[6] & 0x0FL) < < 8;
msb |= (0x3L < < 12); // set the version to 3
msb |= (hash[7] & 0xFFL);
long lsb = (hash[8] & 0x3FL) < < 56;
lsb |= (0x2L < < 62); // set the variant to bits 01
lsb |= (hash[9] & 0xFFL) < < 48;
lsb |= (hash[10] & 0xFFL) < < 40;
lsb |= (hash[11] & 0xFFL) < < 32;
lsb |= (hash[12] & 0xFFL) < < 24;
lsb |= (hash[13] & 0xFFL) < < 16;
lsb |= (hash[14] & 0xFFL) < < 8;
lsb |= (hash[15] & 0xFFL);
return new UUID(msb, lsb);
}
Generates a variant 2, version 3 (name-based, MD5-hashed) UUID as per RFC 4122.
|
public long node() {
if (version != 1) {
throw new UnsupportedOperationException();
}
return node;
}
The node value of the version 1, variant 2 UUID as per RFC 4122.
|
public static UUID randomUUID() {
byte[] data;
// lock on the class to protect lazy init
synchronized (UUID.class) {
if (rng == null) {
rng = new SecureRandom();
}
}
rng.nextBytes(data = new byte[16]);
long msb = (data[0] & 0xFFL) < < 56;
msb |= (data[1] & 0xFFL) < < 48;
msb |= (data[2] & 0xFFL) < < 40;
msb |= (data[3] & 0xFFL) < < 32;
msb |= (data[4] & 0xFFL) < < 24;
msb |= (data[5] & 0xFFL) < < 16;
msb |= (data[6] & 0x0FL) < < 8;
msb |= (0x4L < < 12); // set the version to 4
msb |= (data[7] & 0xFFL);
long lsb = (data[8] & 0x3FL) < < 56;
lsb |= (0x2L < < 62); // set the variant to bits 01
lsb |= (data[9] & 0xFFL) < < 48;
lsb |= (data[10] & 0xFFL) < < 40;
lsb |= (data[11] & 0xFFL) < < 32;
lsb |= (data[12] & 0xFFL) < < 24;
lsb |= (data[13] & 0xFFL) < < 16;
lsb |= (data[14] & 0xFFL) < < 8;
lsb |= (data[15] & 0xFFL);
return new UUID(msb, lsb);
}
Generates a variant 2, version 4 (randomly generated number) UUID as per
RFC 4122.
|
public long timestamp() {
if (version != 1) {
throw new UnsupportedOperationException();
}
return timestamp;
}
The timestamp value of the version 1, variant 2 UUID as per RFC 4122.
|
public String toString() {
StringBuilder builder = new StringBuilder(36);
String msbStr = Long.toHexString(mostSigBits);
if (msbStr.length() < 16) {
int diff = 16 - msbStr.length();
for (int i = 0; i < diff; i++) {
builder.append('0');
}
}
builder.append(msbStr);
builder.insert(8, '-');
builder.insert(13, '-');
builder.append('-');
String lsbStr = Long.toHexString(leastSigBits);
if (lsbStr.length() < 16) {
int diff = 16 - lsbStr.length();
for (int i = 0; i < diff; i++) {
builder.append('0');
}
}
builder.append(lsbStr);
builder.insert(23, '-');
return builder.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"
|
public int variant() {
return variant;
}
|
public int version() {
return version;
}
The version of the variant 2 UUID as per RFC 4122. If the variant
is not 2, then the version will be 0.
|