forked from TrueCloudLab/frostfs-sdk-java
40 lines
1.3 KiB
Java
40 lines
1.3 KiB
Java
package info.frostfs.sdk;
|
|
|
|
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.util.UUID;
|
|
|
|
import static info.frostfs.sdk.constants.AppConst.UUID_BYTE_ARRAY_LENGTH;
|
|
import static info.frostfs.sdk.constants.ErrorConst.INPUT_PARAM_IS_MISSING_TEMPLATE;
|
|
import static info.frostfs.sdk.constants.ErrorConst.WRONG_UUID_SIZE_TEMPLATE;
|
|
import static java.util.Objects.isNull;
|
|
|
|
public class UuidExtension {
|
|
private UuidExtension() {
|
|
}
|
|
|
|
public static UUID asUuid(byte[] bytes) {
|
|
if (isNull(bytes) || bytes.length != UUID_BYTE_ARRAY_LENGTH) {
|
|
throw new ValidationFrostFSException(String.format(WRONG_UUID_SIZE_TEMPLATE, UUID_BYTE_ARRAY_LENGTH));
|
|
}
|
|
|
|
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 ValidationFrostFSException(
|
|
String.format(INPUT_PARAM_IS_MISSING_TEMPLATE, UUID.class.getName())
|
|
);
|
|
}
|
|
|
|
ByteBuffer bb = ByteBuffer.allocate(UUID_BYTE_ARRAY_LENGTH);
|
|
bb.putLong(uuid.getMostSignificantBits());
|
|
bb.putLong(uuid.getLeastSignificantBits());
|
|
return bb.array();
|
|
}
|
|
}
|