public String uuidRepr() { return NessUUID.toString(mostSigBits, leastSigBits); }
long mostSigBits = decode(str, dashPos, 0) & 0xffffffffL; mostSigBits <<= 16; mostSigBits |= (decode(str, dashPos, 1) & 0xffffL); mostSigBits <<= 16; mostSigBits |= (decode(str, dashPos, 2) & 0xffffL); long leastSigBits = (decode(str, dashPos, 3) & 0xffffL); leastSigBits <<= 48; leastSigBits |= (decode(str, dashPos, 4) & 0xffffffffffffL);
public static UUID fromString(String str) { java.util.UUID ans = NessUUID.fromString(str); return new UUID(ans.getMostSignificantBits(), ans.getLeastSignificantBits()); }
@VisibleForTesting static long decode(final String str, final int [] dashPos, final int field) { final int start = dashPos[field]+1; final int end = dashPos[field+1]; if (start >= end) { throw new IllegalArgumentException(String.format("In call to decode(), start (%d) >= end (%d)", start, end)); } // at most 16 nibbles are allowed (64 bits) else if (end - start > 16) { throw new NumberFormatException("long overflow"); } long curr = 0; for (int i = start; i < end; i++) { curr <<= 4; curr |= getNibbleFromChar(str.charAt(i)); } return curr; }
private static void digits(char[] dest, int offset, int digits, long val) { final long hi = 1L << (digits * 4); toUnsignedString(dest, offset, digits, hi | (val & (hi - 1)), 4); }
/** Roughly patterned (read: stolen) from java.util.UUID and java.lang.Long. */ public static String toString(long msb, long lsb) { final char[] uuidChars = new char[36]; digits(uuidChars, 0, 8, msb >> 32); uuidChars[8] = '-'; digits(uuidChars, 9, 4, msb >> 16); uuidChars[13] = '-'; digits(uuidChars, 14, 4, msb); uuidChars[18] = '-'; digits(uuidChars, 19, 4, lsb >> 48); uuidChars[23] = '-'; digits(uuidChars, 24, 12, lsb); try { return (String) STRING_FACTORY.invokeExact(uuidChars); } catch (Throwable e) { throw Throwables.propagate(e); } }
@Test public void testNumChars() { for (int i = 0; i < numChars.length; i++) { int res = NessUUID.getNibbleFromChar(numChars[i]); Assert.assertEquals(i, res); } }
public static String toString(UUID uuid) { return toString(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); }
public long timeNessUuidFromString(int reps) { long accum = 0; for (int i = 0; i < reps; i++) { accum += NessUUID.fromString(testStrings[i % N_UUIDS]).getMostSignificantBits(); } return accum; }
@Test public void testAlphaLowChars() { for (int i = 0; i < alphaLowChars.length; i++) { int res = NessUUID.getNibbleFromChar(alphaLowChars[i]); Assert.assertEquals(i + 10, res); } }
private void doDecodeTest(String value, long expected) { long result = NessUUID.decode(value, new int [] { value.indexOf('-'), value.lastIndexOf('-') }, 0); Assert.assertEquals(expected, result); } }
public long timeNessUuidToString(int reps) { long accum = 0; for (int i = 0; i < reps; i++) { accum += NessUUID.toString(testUuids[i % N_UUIDS]).charAt(0); } return accum; } }
private void testEx(String str) { try { NessUUID.fromString(hyphen1); } catch (IllegalArgumentException e) { return; } Assert.fail(str); }
@Test public void testAlphaHiChars() { for (int i = 0; i < alphaHiChars.length; i++) { int res = NessUUID.getNibbleFromChar(alphaHiChars[i]); Assert.assertEquals(i + 10, res); } }
public static String toString(UUID obj) { return NessUUID.toString(obj.getMostSignificantBits(), obj.getLeastSignificantBits()); } }
@Test public void testBasic() { Assert.assertEquals(UUID.fromString(uuid), NessUUID.fromString(uuid)); Assert.assertEquals(UUID.fromString(caseSensitivity), NessUUID.fromString(caseSensitivity)); Assert.assertEquals(UUID.fromString(overflow), NessUUID.fromString(overflow)); Assert.assertEquals(UUID.fromString(zero), NessUUID.fromString(zero)); }
@Test public void testBoundaryChars() { for (int i = 0; i < illegalChars.length; i++) { try { NessUUID.getNibbleFromChar(illegalChars[i]); Assert.fail(illegalChars[i] + " must not be accepted!"); } catch (IllegalArgumentException iae) { // ok } } } }
@Test public void test_toString() { UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L); String actual = NessUUID.toString(uuid); Assert.assertEquals("f81d4fae-7dec-11d0-a765-00a0c91e6bf6", actual); uuid = new UUID(0x0000000000001000L, 0x8000000000000000L); actual = NessUUID.toString(uuid); Assert.assertEquals("00000000-0000-1000-8000-000000000000", actual); }
@Test public void testLength() { final UUID uuid = NessUUID.fromString(badLength); Assert.assertEquals(0, uuid.getMostSignificantBits()); Assert.assertEquals(0, uuid.getLeastSignificantBits()); }
@Test public void test100ToString() { final long PRIME = 514229; for (long msb = 0; msb < 10; msb++) { for (long lsb = msb; lsb < msb + 10; lsb++) { UUID uuid = new UUID(msb * PRIME, lsb * PRIME * PRIME); Assert.assertEquals(uuid.toString(), NessUUID.toString(uuid)); } } }