Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit a319a31

Browse files
committed
use java.util.UUID
1 parent 638577e commit a319a31

1 file changed

Lines changed: 7 additions & 51 deletions

File tree

  • google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1

google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/KeyRecipe.java

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.ArrayList;
3232
import java.util.Base64;
3333
import java.util.List;
34+
import java.util.UUID;
3435
import java.util.concurrent.ThreadLocalRandom;
3536
import java.util.function.BiFunction;
3637
import java.util.stream.Collectors;
@@ -559,65 +560,20 @@ private static void validateUuid(String uuid) {
559560

560561
private static long[] parseUuid(String uuid) {
561562
String originalUuid = uuid;
562-
563-
// Handle optional braces
564563
if (uuid.startsWith("{")) {
565564
if (!uuid.endsWith("}")) {
566565
throw new IllegalArgumentException("Invalid UUID string: " + originalUuid);
567566
}
568567
uuid = uuid.substring(1, uuid.length() - 1);
569-
}
570-
571-
// Minimum 36 characters required (standard UUID format: 8-4-4-4-12)
572-
if (uuid.length() < 36) {
573-
throw new IllegalArgumentException("Invalid UUID string: " + originalUuid);
574-
}
575-
576-
// Check for leading hyphen
577-
if (uuid.startsWith("-")) {
568+
} else if (uuid.endsWith("}")) {
578569
throw new IllegalArgumentException("Invalid UUID string: " + originalUuid);
579570
}
580-
581-
// Parse 32 hex digits (ignoring hyphens in between)
582-
long high = 0;
583-
long low = 0;
584-
int hexCount = 0;
585-
586-
for (int i = 0; i < uuid.length(); i++) {
587-
char c = uuid.charAt(i);
588-
if (c == '-') {
589-
continue; // Skip hyphens
590-
}
591-
int digit = hexDigit(c);
592-
if (digit < 0) {
593-
throw new IllegalArgumentException("Invalid UUID string: " + originalUuid);
594-
}
595-
if (hexCount < 16) {
596-
high = (high << 4) | digit;
597-
} else {
598-
low = (low << 4) | digit;
599-
}
600-
hexCount++;
601-
}
602-
603-
if (hexCount != 32) {
604-
throw new IllegalArgumentException("Invalid UUID string: " + originalUuid);
605-
}
606-
607-
// After parsing, verify there are no trailing characters
608-
// (uuid must be exactly consumed)
609-
if (uuid.length() > 36) {
610-
throw new IllegalArgumentException("Invalid UUID string: " + originalUuid);
571+
try {
572+
UUID parsed = UUID.fromString(uuid);
573+
return new long[] {parsed.getMostSignificantBits(), parsed.getLeastSignificantBits()};
574+
} catch (IllegalArgumentException e) {
575+
throw new IllegalArgumentException("Invalid UUID string: " + originalUuid, e);
611576
}
612-
613-
return new long[] {high, low};
614-
}
615-
616-
private static int hexDigit(char c) {
617-
if (c >= '0' && c <= '9') return c - '0';
618-
if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
619-
if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
620-
return -1;
621577
}
622578

623579
private TargetRange encodeKeyInternal(

0 commit comments

Comments
 (0)