1 5 package org.h2.value; 6 7 import java.sql.PreparedStatement ; 8 import java.sql.SQLException ; 9 10 import org.h2.util.ByteUtils; 11 import org.h2.util.RandomUtils; 12 import org.h2.util.StringUtils; 13 14 public class ValueUuid extends Value { 15 public static final int PRECISION = 36; 16 private long high, low; 17 18 private ValueUuid(long high, long low) { 19 this.high = high; 20 this.low = low; 21 } 22 23 public int hashCode() { 24 return (int)((high >>> 32) ^ high ^ (low >>> 32) ^ low); 25 } 26 27 public static ValueUuid getNewRandom() { 28 long high = RandomUtils.getSecureLong(); 29 long low = RandomUtils.getSecureLong(); 30 high = (high & (~0xf000L)) | 0x4000L; low = (low & 0x3fffffffffffffffL) | 0x8000000000000000L; return new ValueUuid(high, low); 33 } 34 35 public static ValueUuid get(byte[] binary) { 36 if(binary.length < 32) { 37 return get(ByteUtils.convertBytesToString(binary)); 38 } 39 long high = ByteUtils.readLong(binary, 0); 40 long low = ByteUtils.readLong(binary, 16); 41 return (ValueUuid) Value.cache(new ValueUuid(high, low)); 42 } 43 44 public static ValueUuid get(long high, long low) { 45 return (ValueUuid) Value.cache(new ValueUuid(high, low)); 46 } 47 48 public static ValueUuid get(String s) { 49 long high = 0, low = 0; 50 int i=0; 51 for(int j=0; i<s.length() && j<16; i++) { 52 char ch = s.charAt(i); 53 if(ch != '-') { 54 high = (high << 4) | Character.digit(ch, 16); 55 j++; 56 } 57 } 58 for(int j=0; i<s.length() && j<16; i++) { 59 char ch = s.charAt(i); 60 if(ch != '-') { 61 low = (low << 4) | Character.digit(ch, 16); 62 j++; 63 } 64 } 65 return (ValueUuid) Value.cache(new ValueUuid(high, low)); 66 } 67 68 public String getSQL() { 69 return StringUtils.quoteStringSQL(getString()); 70 } 71 72 public int getType() { 73 return Value.UUID; 74 } 75 76 public long getPrecision() { 77 return PRECISION; 78 } 79 80 public int getDisplaySize() { 81 return 0; 82 } 83 84 private void appendHex(StringBuffer buff, long x, int bytes) { 85 for (int i = bytes*8-4; i >= 0; i-=8) { 86 buff.append(Integer.toHexString((int)(x >> i) & 0xf)); 87 buff.append(Integer.toHexString((int)(x >> (i-4)) & 0xf)); 88 } 89 } 90 91 public String getString() { 92 StringBuffer buff = new StringBuffer (36); 93 appendHex(buff, high >> 32, 4); 94 buff.append('-'); 95 appendHex(buff, high >> 16, 2); 96 buff.append('-'); 97 appendHex(buff, high, 2); 98 buff.append('-'); 99 appendHex(buff, low >> 48, 2); 100 buff.append('-'); 101 appendHex(buff, low, 6); 102 return buff.toString(); 103 } 104 105 protected int compareSecure(Value o, CompareMode mode) { 106 ValueUuid v = (ValueUuid) o; 107 if (high == v.high) { 108 return (low == v.low) ? 0 : (low > v.low ? 1 : -1); 109 } else { 110 return high > v.high ? 1 : -1; 111 } 112 } 113 114 protected boolean isEqual(Value v) { 115 return v instanceof ValueUuid && compareSecure(v, null) == 0; 116 } 117 118 public Object getObject() { 119 return new long[]{high, low}; 121 } 122 123 public byte[] getBytes() { 124 byte[] buff = new byte[16]; 125 for(int i=0; i<8; i++) { 126 buff[i] = (byte)((high >> (8*(8-i))) & 255); 127 buff[8+i] = (byte)((low >> (8*(8-i))) & 255); 128 } 129 return buff; 130 } 131 132 public void set(PreparedStatement prep, int parameterIndex) throws SQLException { 133 prep.setBytes(parameterIndex, getBytes()); 134 } 135 136 public long getHigh() { 137 return high; 138 } 139 140 public long getLow() { 141 return low; 142 } 143 144 } 145 | Popular Tags |