Skip to content

Commit

Permalink
Revert "UUIDUtils."
Browse files Browse the repository at this point in the history
This reverts commit b997a0c.
  • Loading branch information
albertus82 committed May 2, 2024
1 parent 7cd3319 commit e14e8e6
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 77 deletions.
61 changes: 1 addition & 60 deletions src/main/java/io/github/albertus82/storage/util/UUIDUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.albertus82.storage.util;

import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.util.Base64;
Expand All @@ -16,10 +15,6 @@
*/
public class UUIDUtils {

private static final BigInteger B = BigInteger.ONE.shiftLeft(64);
private static final BigInteger L = BigInteger.valueOf(Long.MAX_VALUE);
private static final BigInteger X = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);

private static final Encoder encoder = Base64.getUrlEncoder().withoutPadding();
private static final Decoder decoder = Base64.getUrlDecoder();

Expand All @@ -34,7 +29,7 @@ private UUIDUtils() {}
*
* @return A new UUID object constructed using the decoded bytes.
*
* @throws IllegalArgumentException if the argument is not in valid
* @throws IllegalArgumentException if {@code uuidBase64Url} is not in valid
* <strong>base64url</strong> scheme as defined in RFC 4648.
*
* @see #toBase64Url(UUID)
Expand Down Expand Up @@ -63,58 +58,4 @@ public static String toBase64Url(final UUID uuid) {
return encoder.encodeToString(ByteBuffer.allocate(Long.BYTES * 2).putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits()).array());
}

/**
* Converts the specified number into a new UUID object.
*
* @param num the numeric representation of the UUID, e.g.
* {@code 281831033846701114428451845565925261418}
*
* @return A new UUID object based on the integer value.
*
* @throws IllegalArgumentException if the argument cannot be converted to UUID.
*
* @see <a href=
* "https://gist.github.com/drmalex07/9008c611ffde6cb2ef3a2db8668bc251">drmalex07/convertUuidToBigInteger.java
* - GitHub</a>
*/
public static UUID fromBigInteger(final BigInteger num) {
if (BigInteger.ZERO.compareTo(num) > 0 || X.compareTo(num) < 0) {
throw new IllegalArgumentException("0x" + num.toString(16));
}
final BigInteger[] parts = num.divideAndRemainder(B);
BigInteger hi = parts[0];
if (L.compareTo(hi) < 0) {
hi = hi.subtract(B);
}
BigInteger lo = parts[1];
if (L.compareTo(lo) < 0) {
lo = lo.subtract(B);
}
return new UUID(hi.longValueExact(), lo.longValueExact());
}

/**
* Convert the specified {@link UUID} into a {@link BigInteger}.
*
* @param uuid the UUID object to convert
*
* @return A numeric representation of the specified UUID, e.g.
* {@code 281831033846701114428451845565925261418}
*
* @see <a href=
* "https://gist.github.com/drmalex07/9008c611ffde6cb2ef3a2db8668bc251">drmalex07/convertUuidToBigInteger.java
* - GitHub</a>
*/
public static BigInteger toBigInteger(final UUID uuid) {
BigInteger hi = BigInteger.valueOf(uuid.getMostSignificantBits());
if (hi.signum() < 0) {
hi = hi.add(B);
}
BigInteger lo = BigInteger.valueOf(uuid.getLeastSignificantBits());
if (lo.signum() < 0) {
lo = lo.add(B);
}
return lo.add(hi.multiply(B));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.albertus82.storage.jdbc;

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

import org.junit.jupiter.api.Assertions;
Expand All @@ -12,26 +11,11 @@ class UUIDUtilsTest {

@Test
void test() {
BigInteger max = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
Assertions.assertDoesNotThrow(() -> UUIDUtils.fromBigInteger(BigInteger.ZERO));
Assertions.assertDoesNotThrow(() -> UUIDUtils.fromBigInteger(max));
Assertions.assertThrows(IllegalArgumentException.class, () -> UUIDUtils.fromBigInteger(max.add(BigInteger.ONE)));
Assertions.assertThrows(IllegalArgumentException.class, () -> UUIDUtils.fromBigInteger(BigInteger.ZERO.subtract(BigInteger.ONE)));
test(new UUID(0, 0));
for (short i = 0; i < Short.MAX_VALUE; i++) {
test(UUID.randomUUID());
}
test(new UUID(0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL));
}

private static void test(final UUID a) {
final UUID a = UUID.randomUUID();
final String s = UUIDUtils.toBase64Url(a);
Assertions.assertEquals(22, s.length());
final UUID b = UUIDUtils.fromBase64Url(s);
Assertions.assertEquals(a, b);
final BigInteger i = UUIDUtils.toBigInteger(b);
final UUID c = UUIDUtils.fromBigInteger(i);
Assertions.assertEquals(a, c);
}

}

0 comments on commit e14e8e6

Please sign in to comment.