Debian Codegraph @

src/main/org/h2/value/ValueUuid.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.value;
7
8 import java.util.UUID;
9
10 import org.h2.api.ErrorCode;
11 import org.h2.engine.CastDataProvider;
12 import org.h2.message.DbException;
13 import org.h2.util.Bits;
14 import org.h2.util.MathUtils;
15 import org.h2.util.StringUtils;
16
17 /**
18 * Implementation of the UUID data type.
19 */
20 public final class ValueUuid extends Value {
21
22 /**
23 * The precision of this value in number of bytes.
24 */
25 static final int PRECISION = 16;
26
27 /**
28 * The display size of the textual representation of a UUID.
29 * Example: cd38d882-7ada-4589-b5fb-7da0ca559d9a
30 */
31 static final int DISPLAY_SIZE = 36;
32
33 private final long high, low;
34
35 private ValueUuid(long high, long low) {
36 this.high = high;
37 this.low = low;
38 }
39
40 @Override
41 public int hashCode() {
42 return (int) ((high >>> 32) ^ high ^ (low >>> 32) ^ low);
43 }
44
45 /**
46 * Create a new UUID using the pseudo random number generator.
47 *
48 * @return the new UUID
49 */
50 public static ValueUuid getNewRandom() {
51 long high = MathUtils.secureRandomLong();
52 long low = MathUtils.secureRandomLong();
53 // version 4 (random)
54 high = (high & ~0xf000L) | 0x4000L;
55 // variant (Leach-Salz)
56 low = (low & 0x3fff_ffff_ffff_ffffL) | 0x8000_0000_0000_0000L;
57 return new ValueUuid(high, low);
58 }
59
60 /**
61 * Get or create a UUID for the given 16 bytes.
62 *
63 * @param binary the byte array
64 * @return the UUID
65 */
66 public static ValueUuid get(byte[] binary) {
67 int length = binary.length;
68 if (length != 16) {
69 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, "UUID requires 16 bytes, got " + length);
70 }
71 return get(Bits.readLong(binary, 0), Bits.readLong(binary, 8));
72 }
73
74 /**
75 * Get or create a UUID for the given high and low order values.
76 *
77 * @param high the most significant bits
78 * @param low the least significant bits
79 * @return the UUID
80 */
81 public static ValueUuid get(long high, long low) {
82 return (ValueUuid) Value.cache(new ValueUuid(high, low));
83 }
84
85 /**
86 * Get or create a UUID for the given Java UUID.
87 *
88 * @param uuid Java UUID
89 * @return the UUID
90 */
91 public static ValueUuid get(UUID uuid) {
92 return get(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
93 }
94
95 /**
96 * Get or create a UUID for the given text representation.
97 *
98 * @param s the text representation of the UUID
99 * @return the UUID
100 */
101 public static ValueUuid get(String s) {
102 long low = 0, high = 0;
103 int j = 0;
104 for (int i = 0, length = s.length(); i < length; i++) {
105 char c = s.charAt(i);
106 if (c >= '0' && c <= '9') {
107 low = (low << 4) | (c - '0');
108 } else if (c >= 'a' && c <= 'f') {
109 low = (low << 4) | (c - ('a' - 0xa));
110 } else if (c == '-') {
111 continue;
112 } else if (c >= 'A' && c <= 'F') {
113 low = (low << 4) | (c - ('A' - 0xa));
114 } else if (c <= ' ') {
115 continue;
116 } else {
117 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, s);
118 }
119 if (++j == 16) {
120 high = low;
121 low = 0;
122 }
123 }
124 if (j != 32) {
125 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, s);
126 }
127 return get(high, low);
128 }
129
130 @Override
131 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
132 return addString(builder.append("UUID '")).append('\'');
133 }
134
135 @Override
136 public TypeInfo getType() {
137 return TypeInfo.TYPE_UUID;
138 }
139
140 @Override
141 public int getMemory() {
142 return 32;
143 }
144
145 @Override
146 public int getValueType() {
147 return UUID;
148 }
149
150 @Override
151 public String getString() {
152 return addString(new StringBuilder(36)).toString();
153 }
154
155 @Override
156 public byte[] getBytes() {
157 return Bits.uuidToBytes(high, low);
158 }
159
160 private StringBuilder addString(StringBuilder builder) {
161 StringUtils.appendHex(builder, high >> 32, 4).append('-');
162 StringUtils.appendHex(builder, high >> 16, 2).append('-');
163 StringUtils.appendHex(builder, high, 2).append('-');
164 StringUtils.appendHex(builder, low >> 48, 2).append('-');
165 return StringUtils.appendHex(builder, low, 6);
166 }
167
168 @Override
169 public int compareTypeSafe(Value o, CompareMode mode, CastDataProvider provider) {
170 if (o == this) {
171 return 0;
172 }
173 ValueUuid v = (ValueUuid) o;
174 int cmp = Long.compareUnsigned(high, v.high);
175 return cmp != 0 ? cmp : Long.compareUnsigned(low, v.low);
176 }
177
178 @Override
179 public boolean equals(Object other) {
180 if (!(other instanceof ValueUuid)) {
181 return false;
182 }
183 ValueUuid v = (ValueUuid) other;
184 return high == v.high && low == v.low;
185 }
186
187 /**
188 * Returns the UUID.
189 *
190 * @return the UUID
191 */
192 public UUID getUuid() {
193 return new UUID(high, low);
194 }
195
196 /**
197 * Get the most significant 64 bits of this UUID.
198 *
199 * @return the high order bits
200 */
201 public long getHigh() {
202 return high;
203 }
204
205 /**
206 * Get the least significant 64 bits of this UUID.
207 *
208 * @return the low order bits
209 */
210 public long getLow() {
211 return low;
212 }
213
214 @Override
215 public long charLength() {
216 return DISPLAY_SIZE;
217 }
218
219 @Override
220 public long octetLength() {
221 return PRECISION;
222 }
223
224 }