Copied more code from uuid-creator

This commit is contained in:
Fabio Lima 2020-02-17 01:49:00 -03:00
parent 3fbe5c5c1b
commit 63bee56534
5 changed files with 46 additions and 13 deletions

View File

@ -37,11 +37,9 @@ import com.github.f4b6a3.ulid.util.UlidUtil;
*/
public class UlidCreator {
private UlidCreator() {
}
// TODO: not working yet
public static String getUlid() {
UUID guid = getLexicalOrderGuid();
return UlidUtil.fromUuidToUlid(guid);

View File

@ -46,10 +46,8 @@ public class UlidUtil {
* @return a ULID
*/
public static String fromUuidToUlid(UUID uuid) {
// TODO: not working yet
// byte[] bytes = UuidUtil.fromUuidToBytes(uuid);
// return fromBytesToUlid(bytes);
return null;
byte[] bytes = fromUuidToBytes(uuid);
return fromBytesToUlid(bytes);
}
/**
@ -62,10 +60,38 @@ public class UlidUtil {
* @return a UUID if valid
*/
public static UUID fromUlidToUuid(String ulid) {
// TODO: not working yet
// byte[] bytes = fromUlidToBytes(ulid);
// return UuidUtil.fromBytesToUuid(bytes);
return null;
byte[] bytes = fromUlidToBytes(ulid);
return fromBytesToUuid(bytes);
}
/**
* Get the array of bytes from a UUID.
*
* @param uuid
* a UUID
* @return an array of bytes
*/
public static byte[] fromUuidToBytes(UUID uuid) {
long msb = uuid.getMostSignificantBits();
long lsb = uuid.getLeastSignificantBits();
byte[] msbBytes = ByteUtil.toBytes(msb);
byte[] lsbBytes = ByteUtil.toBytes(lsb);
return ByteUtil.concat(msbBytes, lsbBytes);
}
/**
* Get a UUID from an array of bytes;
*
* @param bytes
* an array of bytes
* @return a UUID
*/
public static UUID fromBytesToUuid(byte[] bytes) {
byte[] msbBytes = ByteUtil.copy(bytes, 0, 8);
byte[] lsbBytes = ByteUtil.copy(bytes, 8, 16);
long msb = ByteUtil.toNumber(msbBytes);
long lsb = ByteUtil.toNumber(lsbBytes);
return new UUID(msb, lsb);
}
/**

View File

@ -1,9 +1,10 @@
package com.github.f4b6a3.ulid.factory;
package com.github.f4b6a3;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import com.github.f4b6a3.ulid.UlidCreatorTest;
import com.github.f4b6a3.ulid.factory.LexicalOrderGuidCreatorTest;
import com.github.f4b6a3.ulid.random.NaiveRandomTest;
import com.github.f4b6a3.ulid.timestamp.DefaultTimestampStrategyTest;
import com.github.f4b6a3.ulid.util.Base32UtilTest;

View File

@ -6,6 +6,7 @@ import java.util.UUID;
import org.junit.Test;
import com.github.f4b6a3.ulid.exception.UlidCreatorException;
import com.github.f4b6a3.ulid.factory.LexicalOrderGuidCreator.LexicalOrderGuidException;
import com.github.f4b6a3.ulid.random.Xorshift128PlusRandom;
import com.github.f4b6a3.ulid.timestamp.FixedTimestampStretegy;
@ -108,7 +109,7 @@ public class LexicalOrderGuidCreatorTest {
assertEquals(String.format("The MSB should be iqual to %s.", expected), expected, randomMsb);
}
@Test(expected = UlidCreatorException.class)
@Test(expected = LexicalOrderGuidException.class)
public void testShouldThrowOverflowException() {
long low = MAX_LOW - DEFAULT_LOOP;

View File

@ -1,5 +1,12 @@
package com.github.f4b6a3.ulid.timestamp;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class DefaultTimestampStrategyTest {
// TODO
@Test
public void testVoid() {
assertTrue("void test", true);
}
}