BinaryToUuidMapper implements TypeMapper<Object, UUID> { @Override public UUID toJavaType(Column column, Class<?> aClass, Object binary) { if (binary == null) return null; final byte[] data; try { data = (byte[]) binary; } catch (final ClassCastException ex) { throw new SpeedmentTypeMapperException(format( "Expected database input to be a byte[] but was a %s.", binary.getClass() ), ex); } long msb = 0; long lsb = 0; if (data.length != 16) { throw new SpeedmentTypeMapperException( "Binary UUID is expected to be 16 bytes in length" ); } for (int i = 0; i < 8; i++) msb = (msb << 8) | (data[i] & 0xff); for (int i = 8; i < 16; i++) lsb = (lsb << 8) | (data[i] & 0xff); return new UUID(msb, lsb); }  @Override String getLabel(); @Override Type getJavaType(Column column); @Override UUID toJavaType(Column column, Class<?> aClass, Object binary); @Override byte[] toDatabaseType(UUID uuid);  }
@Test void toJavaTypeHex() throws Exception { final byte[] test = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, }; final String expected = bytesToHex(test); final String actual = uuidMapper.toJavaType(null, null, test).toString() .replace("-", "").toLowerCase(); assertEquals(expected, actual, "Compare as HEX: "); }
@Test void toJavaTypeUUID() throws Exception { final ByteBuffer test = ByteBuffer.wrap(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, }); final UUID expected = new UUID(test.getLong(), test.getLong()); final UUID actual = uuidMapper.toJavaType(null, null, test.array()); assertEquals(expected, actual, "Compare as UUID: "); }