001: package pygmy.core;
002:
003: import java.security.SecureRandom;
004: import java.util.StringTokenizer;
005:
006: public class UUID implements Comparable {
007:
008: private static SecureRandom randomGenerator = new SecureRandom();
009:
010: private long high;
011: private long low;
012:
013: public UUID(long high, long low) {
014: this .high = high;
015: this .low = low;
016: }
017:
018: public static UUID createUUID() {
019: byte[] uuid = new byte[16];
020: randomGenerator.nextBytes(uuid);
021: uuid[6] &= 0xf;
022: uuid[6] |= 0x40;
023: uuid[8] &= 0x3f;
024: uuid[8] |= 0x80;
025: uuid[10] |= 0x80;
026: return new UUID(copyToLong(uuid, 0), copyToLong(uuid, 8));
027: }
028:
029: public int compareTo(Object o) {
030: UUID uuid = (UUID) o;
031:
032: if (this .high < uuid.high) {
033: return -1;
034: } else if (this .high == uuid.high) {
035: if (this .low < uuid.low) {
036: return -1;
037: } else if (this .low == uuid.low) {
038: return 0;
039: } else {
040: return 1;
041: }
042: } else {
043: return 1;
044: }
045: }
046:
047: private static long copyToLong(byte[] uuid, int start) {
048: long num = 0L;
049: for (int i = start; i < (start + 8); i++)
050: num = num << 8 | (long) (uuid[i] & 0xff);
051: return num;
052: }
053:
054: public int hashCode() {
055: return (int) (high >> 32 ^ high ^ low >> 32 ^ low);
056: }
057:
058: public boolean equals(Object obj) {
059: if (obj == this )
060: return true;
061:
062: if (obj instanceof UUID) {
063: UUID uuid = (UUID) obj;
064:
065: return (uuid.high == this .high) && (uuid.low == this .low);
066: }
067:
068: return false;
069: }
070:
071: private static String digits(long num, int i) {
072: long shifted = 1L << i * 4;
073: return Long.toHexString(shifted | num & shifted - 1L)
074: .substring(1);
075: }
076:
077: public String toString() {
078: return digits(high >> 32, 8) + "-" + digits(high >> 16, 4)
079: + "-" + digits(high, 4) + "-" + digits(low >> 48, 4)
080: + "-" + digits(low, 12);
081: }
082:
083: public static UUID parse(String uuidStr) {
084: StringTokenizer token = new StringTokenizer(uuidStr, "-");
085: long upper = Long.parseLong(token.nextToken(), 16) << 32;
086: upper = upper | (Long.parseLong(token.nextToken(), 16) << 16);
087: upper = upper | (Long.parseLong(token.nextToken(), 16));
088:
089: long lower = Long.parseLong(token.nextToken(), 16) << 48;
090: lower = lower | Long.parseLong(token.nextToken(), 16);
091:
092: return new UUID(upper, lower);
093: }
094:
095: public static void main(String[] args) {
096: UUID uuid = UUID.createUUID();
097: System.out.println("UUID: (" + uuid + ")");
098: System.out.println("UUID: (" + UUID.parse(uuid.toString())
099: + ")");
100:
101: UUID uuid2 = UUID.createUUID();
102: System.out.println("UUID: (" + uuid2 + ")");
103: System.out.println("UUID: (" + UUID.parse(uuid2.toString())
104: + ")");
105:
106: UUID uuid3 = UUID.createUUID();
107: System.out.println("UUID: (" + uuid3 + ")");
108: System.out.println("UUID: (" + UUID.parse(uuid3.toString())
109: + ")");
110: }
111: }
|