package info.frostfs.sdk; import java.nio.ByteBuffer; import java.util.UUID; import static java.util.Objects.isNull; public class UuidExtension { private static final int UUID_BYTE_ARRAY_LENGTH = 16; private static final String ERROR_WRONG_UUID_SIZE = "Uuid byte array length must be " + UUID_BYTE_ARRAY_LENGTH; private static final String ERROR_UUID_MISSING = "Uuid is not present"; private UuidExtension() { } public static UUID asUuid(byte[] bytes) { if (isNull(bytes) || bytes.length != UUID_BYTE_ARRAY_LENGTH) { throw new IllegalArgumentException(ERROR_WRONG_UUID_SIZE); } ByteBuffer bb = ByteBuffer.wrap(bytes); long firstLong = bb.getLong(); long secondLong = bb.getLong(); return new UUID(firstLong, secondLong); } public static byte[] asBytes(UUID uuid) { if (isNull(uuid)) { throw new IllegalArgumentException(ERROR_UUID_MISSING); } ByteBuffer bb = ByteBuffer.allocate(UUID_BYTE_ARRAY_LENGTH); bb.putLong(uuid.getMostSignificantBits()); bb.putLong(uuid.getLeastSignificantBits()); return bb.array(); } }