frostfs-sdk-java/models/src/main/java/info/frostfs/sdk/UuidExtension.java
Bruk Ori 441d3129dc
All checks were successful
DCO / DCO (pull_request) Successful in 28s
[#6] cover the models module with junit tests
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2024-08-21 11:12:33 +03:00

37 lines
1.2 KiB
Java

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();
}
}