Skip to content

Commit

Permalink
Fixed UUID to BigInteger (and back). Had to treat the UUID strings as…
Browse files Browse the repository at this point in the history
… proper hex. BigInteger conversions almost complete.
  • Loading branch information
jdereg committed Feb 17, 2024
1 parent 8929589 commit 94982e4
Show file tree
Hide file tree
Showing 4 changed files with 1,867 additions and 1,763 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,34 @@ static BigInteger toBigInteger(Object from, Converter converter) {

static UUID bigIntegerToUUID(Object from, Converter converter) {
BigInteger bigInteger = (BigInteger) from;
BigInteger mask = BigInteger.valueOf(Long.MAX_VALUE);
long mostSignificantBits = bigInteger.shiftRight(64).and(mask).longValue();
long leastSignificantBits = bigInteger.and(mask).longValue();
return new UUID(mostSignificantBits, leastSignificantBits);
if (bigInteger.signum() < 0) {
throw new IllegalArgumentException("Cannot convert a negative number [" + bigInteger + "] to a UUID");
}
StringBuilder hex = new StringBuilder(bigInteger.toString(16));

// Pad the string to 32 characters with leading zeros (if necessary)
while (hex.length() < 32) {
hex.insert(0, "0");
}

// Split into two 64-bit parts
String highBitsHex = hex.substring(0, 16);
String lowBitsHex = hex.substring(16, 32);

// Combine and format into standard UUID format
String uuidString = highBitsHex.substring(0, 8) + "-" +
highBitsHex.substring(8, 12) + "-" +
highBitsHex.substring(12, 16) + "-" +
lowBitsHex.substring(0, 4) + "-" +
lowBitsHex.substring(4, 16);

// Create UUID from string
return UUID.fromString(uuidString);
}

static UUID bigDecimalToUUID(Object from, Converter converter) {
BigInteger bigInt = ((BigDecimal) from).toBigInteger();
long mostSigBits = bigInt.shiftRight(64).longValue();
long leastSigBits = bigInt.and(new BigInteger("FFFFFFFFFFFFFFFF", 16)).longValue();
return new UUID(mostSigBits, leastSigBits);
return bigIntegerToUUID(bigInt, converter);
}

/**
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/com/cedarsoftware/util/convert/UUIDConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.UUID;

/**
* @author John DeRegnaucourt ([email protected])
Expand All @@ -27,19 +26,12 @@ private UUIDConversions() {
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
UUID uuid = (UUID) from;
BigInteger mostSignificant = BigInteger.valueOf(uuid.getMostSignificantBits());
BigInteger leastSignificant = BigInteger.valueOf(uuid.getLeastSignificantBits());
// Shift the most significant bits to the left and add the least significant bits
return new BigDecimal(mostSignificant.shiftLeft(64).add(leastSignificant));
return new BigDecimal(toBigInteger(from, converter));
}

static BigInteger toBigInteger(Object from, Converter converter) {
UUID uuid = (UUID) from;
BigInteger mostSignificant = BigInteger.valueOf(uuid.getMostSignificantBits());
BigInteger leastSignificant = BigInteger.valueOf(uuid.getLeastSignificantBits());
// Shift the most significant bits to the left and add the least significant bits
return mostSignificant.shiftLeft(64).add(leastSignificant);
String hex = from.toString().replace("-", "");
return new BigInteger(hex, 16);
}
}

Loading

0 comments on commit 94982e4

Please sign in to comment.