1 32 package net.sf.retrotranslator.runtime.java.util; 33 34 import java.io.Serializable ; 35 import java.security.*; 36 37 40 public class UUID_ implements Serializable , Comparable <UUID_> { 41 42 private static final long serialVersionUID = 4759128571957838954L; 43 private static final SecureRandom randomGenerator = new SecureRandom(); 44 45 private long mostSignificantBits; 46 private long leastSignificantBits; 47 48 public UUID_(long mostSigBits, long leastSigBits) { 49 mostSignificantBits = mostSigBits; 50 leastSignificantBits = leastSigBits; 51 } 52 53 public static UUID_ randomUUID() { 54 byte[] bytes = new byte[16]; 55 randomGenerator.nextBytes(bytes); 56 return newInstance(bytes, 4); 57 } 58 59 public static UUID_ nameUUIDFromBytes(byte[] name) { 60 try { 61 return newInstance(MessageDigest.getInstance("MD5").digest(name), 3); 62 } catch (NoSuchAlgorithmException e) { 63 throw new InternalError (e.getMessage()); 64 } 65 } 66 67 public static UUID_ fromString(String name) { 68 String [] values = name.split("-"); 69 if (values.length != 5) throw new IllegalArgumentException (name); 70 try { 71 return new UUID_(Long.parseLong(values[0], 16) << 32 | Long.parseLong(values[1], 16) << 16 | 72 Long.parseLong(values[2], 16), Long.parseLong(values[3], 16) << 48 | Long.parseLong(values[4], 16)); 73 } catch (NumberFormatException e) { 74 throw new IllegalStateException (name); 75 } 76 } 77 78 public long getMostSignificantBits() { 79 return mostSignificantBits; 80 } 81 82 public long getLeastSignificantBits() { 83 return leastSignificantBits; 84 } 85 86 public int version() { 87 return (int) (mostSignificantBits >>> 12) & 0x0F; 88 } 89 90 public int variant() { 91 if (leastSignificantBits >>> 63 == 0) return 0; 92 if (leastSignificantBits >>> 62 == 2) return 2; 93 return (int) (leastSignificantBits >>> 61); 94 } 95 96 public long timestamp() { 97 assertVersion1(); 98 return (mostSignificantBits & 0x00000FFFL) << 48 | 99 (mostSignificantBits & 0xFFFF0000L) << 16 | 100 mostSignificantBits >>> 32; 101 } 102 103 public int clockSequence() { 104 assertVersion1(); 105 return (int) (leastSignificantBits >>> 48) & 0x3FFF; 106 } 107 108 public long node() { 109 assertVersion1(); 110 return leastSignificantBits & 0x0000FFFFFFFFFFFFL; 111 } 112 113 public String toString() { 114 return hex(mostSignificantBits >> 32, 8) + 115 '-' + hex(mostSignificantBits >> 16, 4) + 116 '-' + hex(mostSignificantBits, 4) + 117 '-' + hex(leastSignificantBits >> 48, 4) + 118 '-' + hex(leastSignificantBits, 12); 119 } 120 121 public int hashCode() { 122 return (int) (mostSignificantBits >> 32 ^ mostSignificantBits 123 ^ leastSignificantBits >> 32 ^ leastSignificantBits); 124 } 125 126 public boolean equals(Object obj) { 127 if (obj instanceof UUID_) { 128 UUID_ val = (UUID_) obj; 129 return mostSignificantBits == val.mostSignificantBits && 130 leastSignificantBits == val.leastSignificantBits; 131 } 132 return false; 133 } 134 135 public int compareTo(UUID_ val) { 136 if (mostSignificantBits > val.mostSignificantBits) return 1; 137 if (mostSignificantBits < val.mostSignificantBits) return -1; 138 if (leastSignificantBits > val.leastSignificantBits) return 1; 139 if (leastSignificantBits < val.leastSignificantBits) return -1; 140 return 0; 141 } 142 143 private static UUID_ newInstance(byte[] bytes, int version) { 144 return new UUID_(getLong(bytes, 0) & ~0xF000L | version << 12, 145 getLong(bytes, 8) & ~0xC000000000000000L | 0x8000000000000000L); 146 } 147 148 private static long getLong(byte[] bytes, int offset) { 149 long result = 0; 150 for (int i = 0; i < 8; i++) { 151 result = result << 8 | bytes[i + offset] & 0xFF; 152 } 153 return result; 154 } 155 156 private static String hex(long value, int length) { 157 String s = Long.toHexString(1L << (length << 2) | value); 158 return s.substring(s.length() - length); 159 } 160 161 private void assertVersion1() { 162 if (version() != 1) throw new UnsupportedOperationException ("Not a version 1 UUID"); 163 } 164 165 } 166 | Popular Tags |