File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueUuid.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.value;
8
9 import java.sql.PreparedStatement;
10 import java.sql.SQLException;
11 import java.util.UUID;
12
13 import org.h2.constant.ErrorCode;
14 import org.h2.message.DbException;
15 import org.h2.util.Utils;
16 import org.h2.util.MathUtils;
17 import org.h2.util.StringUtils;
18
19 /**
20 * Implementation of the UUID data type.
21 */
22 public class ValueUuid extends Value {
23
24 /**
25 * The precision of this value in number of bytes.
26 */
27 private static final int PRECISION = 16;
28
29 /**
30 * The display size of the textual representation of a UUID.
31 * Example: cd38d882-7ada-4589-b5fb-7da0ca559d9a
32 */
33 private static final int DISPLAY_SIZE = 36;
34
35 private final long high, low;
36
37 private ValueUuid(long high, long low) {
38 this.high = high;
39 this.low = low;
40 }
41
42 public int hashCode() {
43 return (int) ((high >>> 32) ^ high ^ (low >>> 32) ^ low);
44 }
45
46 /**
47 * Create a new UUID using the pseudo random number generator.
48 *
49 * @return the new UUID
50 */
51 public static ValueUuid getNewRandom() {
52 long high = MathUtils.secureRandomLong();
53 long low = MathUtils.secureRandomLong();
54 // version 4 (random)
55 high = (high & (~0xf000L)) | 0x4000L;
56 // variant (Leach-Salz)
57 low = (low & 0x3fffffffffffffffL) | 0x8000000000000000L;
58 return new ValueUuid(high, low);
59 }
60
61 /**
62 * Get or create a UUID for the given 16 bytes.
63 *
64 * @param binary the byte array (must be at least 16 bytes long)
65 * @return the UUID
66 */
67 public static ValueUuid get(byte[] binary) {
68 if (binary.length < 16) {
69 return get(StringUtils.convertBytesToHex(binary));
70 }
71 long high = Utils.readLong(binary, 0);
72 long low = Utils.readLong(binary, 8);
73 return (ValueUuid) Value.cache(new ValueUuid(high, low));
74 }
75
76 /**
77 * Get or create a UUID for the given high and low order values.
78 *
79 * @param high the most significant bits
80 * @param low the least significant bits
81 * @return the UUID
82 */
83 public static ValueUuid get(long high, long low) {
84 return (ValueUuid) Value.cache(new ValueUuid(high, low));
85 }
86
87 /**
88 * Get or create a UUID for the given text representation.
89 *
90 * @param s the text representation of the UUID
91 * @return the UUID
92 */
93 public static ValueUuid get(String s) {
94 long low = 0, high = 0;
95 for (int i = 0, j = 0, length = s.length(); i < length; i++) {
96 char c = s.charAt(i);
97 if (c >= '0' && c <= '9') {
98 low = (low << 4) | (c - '0');
99 } else if (c >= 'a' && c <= 'f') {
100 low = (low << 4) | (c - 'a' + 0xa);
101 } else if (c == '-') {
102 continue;
103 } else if (c >= 'A' && c <= 'F') {
104 low = (low << 4) | (c - 'A' + 0xa);
105 } else if (c <= ' ') {
106 continue;
107 } else {
108 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, s);
109 }
110 if (j++ == 15) {
111 high = low;
112 low = 0;
113 }
114 }
115 return (ValueUuid) Value.cache(new ValueUuid(high, low));
116 }
117
118 public String getSQL() {
119 return StringUtils.quoteStringSQL(getString());
120 }
121
122 public int getType() {
123 return Value.UUID;
124 }
125
126 public long getPrecision() {
127 return PRECISION;
128 }
129
130 private static void appendHex(StringBuilder buff, long x, int bytes) {
131 for (int i = bytes * 8 - 4; i >= 0; i -= 8) {
132 buff.append(Integer.toHexString((int) (x >> i) & 0xf)).
133 append(Integer.toHexString((int) (x >> (i - 4)) & 0xf));
134 }
135 }
136
137 public String getString() {
138 StringBuilder buff = new StringBuilder(36);
139 appendHex(buff, high >> 32, 4);
140 buff.append('-');
141 appendHex(buff, high >> 16, 2);
142 buff.append('-');
143 appendHex(buff, high, 2);
144 buff.append('-');
145 appendHex(buff, low >> 48, 2);
146 buff.append('-');
147 appendHex(buff, low, 6);
148 return buff.toString();
149 }
150
151 protected int compareSecure(Value o, CompareMode mode) {
152 if (o == this) {
153 return 0;
154 }
155 ValueUuid v = (ValueUuid) o;
156 if (high == v.high) {
157 return MathUtils.compareLong(low, v.low);
158 }
159 return high > v.high ? 1 : -1;
160 }
161
162 public boolean equals(Object other) {
163 return other instanceof ValueUuid && compareSecure((Value) other, null) == 0;
164 }
165
166 public Object getObject() {
167 return new UUID(high, low);
168 }
169
170 public byte[] getBytes() {
171 byte[] buff = new byte[16];
172 for (int i = 0; i < 8; i++) {
173 buff[i] = (byte) ((high >> (8 * (7 - i))) & 255);
174 buff[8 + i] = (byte) ((low >> (8 * (7 - i))) & 255);
175 }
176 return buff;
177 }
178
179 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
180 prep.setBytes(parameterIndex, getBytes());
181 }
182
183 /**
184 * Get the most significant 64 bits of this UUID.
185 *
186 * @return the high order bits
187 */
188 public long getHigh() {
189 return high;
190 }
191
192 /**
193 * Get the least significant 64 bits of this UUID.
194 *
195 * @return the low order bits
196 */
197 public long getLow() {
198 return low;
199 }
200
201 public int getDisplaySize() {
202 return DISPLAY_SIZE;
203 }
204
205 }
206