1 21 22 package org.apache.derby.impl.services.uuid; 23 24 import org.apache.derby.iapi.services.io.FormatIdUtil; 25 import org.apache.derby.iapi.services.io.StoredFormatIds; 26 import org.apache.derby.iapi.services.io.Formatable; 27 28 import org.apache.derby.catalog.UUID; 29 30 import java.io.ObjectOutput ; 31 import java.io.ObjectInput ; 32 import java.io.IOException ; 33 34 import java.io.StringReader ; 35 36 37 public class BasicUUID implements UUID, Formatable 38 { 39 42 43 private long majorId; private long timemillis; 45 private int sequence; 46 47 50 51 54 public BasicUUID(long majorId, long timemillis, int sequence) 55 { 56 this.majorId = majorId; 57 this.timemillis = timemillis; 58 this.sequence = sequence; 59 } 60 61 67 public BasicUUID(String uuidstring) 68 { 69 StringReader sr = new StringReader (uuidstring); 70 sequence = (int) readMSB(sr); 71 72 long ltimemillis = readMSB(sr) << 32; 73 ltimemillis += readMSB(sr) << 16; 74 ltimemillis += readMSB(sr); 75 timemillis = ltimemillis; 76 majorId = readMSB(sr); 77 } 78 79 85 public BasicUUID(byte[] b) 86 { 87 int lsequence = 0; 88 for (int ix = 0; ix < 4; ix++) 89 { 90 lsequence = lsequence << 8; 91 lsequence = lsequence | (0xff & b[ix]); 92 } 93 94 long ltimemillis = 0; 95 for (int ix = 4; ix < 10; ix++) 96 { 97 ltimemillis = ltimemillis << 8; 98 ltimemillis = ltimemillis | (0xff & b[ix]); 99 } 100 101 long linetaddr = 0; 102 for (int ix = 10; ix < 16; ix++) 103 { 104 linetaddr = linetaddr << 8; 105 linetaddr = linetaddr | (0xff & b[ix]); 106 } 107 108 sequence = lsequence; 109 timemillis = ltimemillis; 110 majorId = linetaddr; 111 } 112 113 116 117 public BasicUUID() { super(); } 119 120 124 public void writeExternal(ObjectOutput out) throws IOException 125 { 126 out.writeLong(majorId); 128 out.writeLong(timemillis); 129 out.writeInt(sequence); 130 } 131 132 136 public void readExternal(ObjectInput in) throws IOException 137 { 138 majorId = in.readLong(); 139 timemillis = in.readLong(); 140 sequence = in.readInt(); 141 } 142 143 146 public int getTypeFormatId() { 147 return StoredFormatIds.BASIC_UUID; 148 } 149 150 private static void writeMSB(char[] data, int offset, long value, int nbytes) 151 { 152 for (int i = nbytes - 1; i >= 0; i--) 153 { 154 long b = (value & (255L << (8 * i))) >>> (8 * i); 155 156 int c = (int) ((b & 0xf0) >> 4); 157 data[offset++] = (char) (c < 10 ? c + '0' : (c - 10) + 'a'); 158 c = (int) (b & 0x0f); 159 data[offset++] = (char) (c < 10 ? c + '0' : (c - 10) + 'a'); 160 } 161 } 162 163 168 private static long readMSB(StringReader sr) 169 { 170 long value = 0; 171 172 try 173 { 174 int c; 175 while ((c = sr.read()) != -1) 176 { 177 if (c == '-') 178 break; 179 value <<= 4; 180 181 int nibble; 182 if (c <= '9') 183 nibble = c - '0'; 184 else if (c <= 'F') 185 nibble = c - 'A' + 10; 186 else 187 nibble = c - 'a' + 10; 188 value += nibble; 189 } 190 } 191 catch (Exception e) 192 { 193 } 194 195 return value; 196 } 197 198 201 202 206 public boolean equals(Object otherObject) 207 { 208 if (!(otherObject instanceof BasicUUID)) 209 return false; 210 211 BasicUUID other = (BasicUUID) otherObject; 212 213 return (this.sequence == other.sequence) 214 && (this.timemillis == other.timemillis) 215 && (this.majorId == other.majorId); 216 } 217 218 222 public int hashCode() 223 { 224 long hc = majorId ^ timemillis; 225 226 return sequence ^ ((int) (hc >> 4)); 227 } 228 229 236 public String toString() {return stringWorkhorse( '-' );} 237 238 242 public String toANSIidentifier() {return "U" + stringWorkhorse( 'X' );} 243 244 252 public String stringWorkhorse( char separator ) 253 { 254 char[] data = new char[36]; 255 256 writeMSB(data, 0, (long) sequence, 4); 257 258 int offset = 8; 259 if (separator != 0) data[offset++] = separator; 260 261 long ltimemillis = timemillis; 262 writeMSB(data, offset, (ltimemillis & 0x0000ffff00000000L) >>> 32, 2); 263 offset += 4; 264 if (separator != 0) data[offset++] = separator; 265 writeMSB(data, offset, (ltimemillis & 0x00000000ffff0000L) >>> 16, 2); 266 offset += 4; 267 if (separator != 0) data[offset++] = separator; 268 writeMSB(data, offset, (ltimemillis & 0x000000000000ffffL), 2); 269 offset += 4; 270 if (separator != 0) data[offset++] = separator; 271 writeMSB(data, offset, majorId, 6); 272 offset += 12; 273 274 return new String (data, 0, offset); 275 } 276 277 284 public byte[] toByteArray() 285 { 286 byte[] result = new byte[16]; 287 288 int lsequence = sequence; 289 result[0] = (byte)(lsequence >>> 24); 290 result[1] = (byte)(lsequence >>> 16); 291 result[2] = (byte)(lsequence >>> 8); 292 result[3] = (byte)lsequence; 293 294 long ltimemillis = timemillis; 295 result[4] = (byte)(ltimemillis >>> 40); 296 result[5] = (byte)(ltimemillis >>> 32); 297 result[6] = (byte)(ltimemillis >>> 24); 298 result[7] = (byte)(ltimemillis >>> 16); 299 result[8] = (byte)(ltimemillis >>> 8); 300 result[9] = (byte)ltimemillis; 301 302 long linetaddr = majorId; 303 result[10] = (byte)(linetaddr >>> 40); 304 result[11] = (byte)(linetaddr >>> 32); 305 result[12] = (byte)(linetaddr >>> 24); 306 result[13] = (byte)(linetaddr >>> 16); 307 result[14] = (byte)(linetaddr >>> 8); 308 result[15] = (byte)linetaddr; 309 310 return result; 311 } 312 313 318 public UUID cloneMe() 319 { 320 return new BasicUUID(majorId, timemillis, sequence); 321 } 322 323 public String toHexString() {return stringWorkhorse( (char) 0 );} 324 } 325 326 | Popular Tags |