package org.softao.jassandra.utils;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.util.UUID;
/**
*
*/
public final class UUIDHelper {
/**
* @return a new instance of time based UUID.
*/
public static UUID createTimeBasedUUID() {
org.safehaus.uuid.UUID timeBasedUUID = org.safehaus.uuid.UUIDGenerator
.getInstance().generateTimeBasedUUID();
return UUID.fromString(timeBasedUUID.toString());
}
/**
* @param bytes
* @return the UUID derived from <code>bytes</code>.
*/
public static UUID toUUID(byte[] bytes) {
ByteBuffer bb = ByteBuffer.wrap(bytes);
LongBuffer lb = bb.asLongBuffer();
long high = lb.get();
long low = lb.get();
return new UUID(high, low);
}
/**
* @param uuid
* @return the bytes representation of UUID.
*/
public static byte[] toBytes(UUID uuid) {
ByteBuffer bb = ByteBuffer.allocate(16);
LongBuffer lb = bb.asLongBuffer();
lb.put(uuid.getMostSignificantBits());
lb.put(uuid.getLeastSignificantBits());
return bb.array();
}
private UUIDHelper() {
}
}
|