From 63bee56534e032db6303a2440efce6b177ae7def Mon Sep 17 00:00:00 2001 From: Fabio Lima Date: Mon, 17 Feb 2020 01:49:00 -0300 Subject: [PATCH] Copied more code from uuid-creator --- .../com/github/f4b6a3/ulid/UlidCreator.java | 2 - .../com/github/f4b6a3/ulid/util/UlidUtil.java | 42 +++++++++++++++---- .../f4b6a3/{ulid/factory => }/TestSuite.java | 3 +- .../factory/LexicalOrderGuidCreatorTest.java | 3 +- .../DefaultTimestampStrategyTest.java | 9 +++- 5 files changed, 46 insertions(+), 13 deletions(-) rename src/test/java/com/github/f4b6a3/{ulid/factory => }/TestSuite.java (87%) diff --git a/src/main/java/com/github/f4b6a3/ulid/UlidCreator.java b/src/main/java/com/github/f4b6a3/ulid/UlidCreator.java index 628e55d..3d71c18 100644 --- a/src/main/java/com/github/f4b6a3/ulid/UlidCreator.java +++ b/src/main/java/com/github/f4b6a3/ulid/UlidCreator.java @@ -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); diff --git a/src/main/java/com/github/f4b6a3/ulid/util/UlidUtil.java b/src/main/java/com/github/f4b6a3/ulid/util/UlidUtil.java index ebf3ee0..56ffc8c 100644 --- a/src/main/java/com/github/f4b6a3/ulid/util/UlidUtil.java +++ b/src/main/java/com/github/f4b6a3/ulid/util/UlidUtil.java @@ -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); } /** diff --git a/src/test/java/com/github/f4b6a3/ulid/factory/TestSuite.java b/src/test/java/com/github/f4b6a3/TestSuite.java similarity index 87% rename from src/test/java/com/github/f4b6a3/ulid/factory/TestSuite.java rename to src/test/java/com/github/f4b6a3/TestSuite.java index 5925970..d67701f 100644 --- a/src/test/java/com/github/f4b6a3/ulid/factory/TestSuite.java +++ b/src/test/java/com/github/f4b6a3/TestSuite.java @@ -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; diff --git a/src/test/java/com/github/f4b6a3/ulid/factory/LexicalOrderGuidCreatorTest.java b/src/test/java/com/github/f4b6a3/ulid/factory/LexicalOrderGuidCreatorTest.java index 9b9f8af..cd13e6f 100644 --- a/src/test/java/com/github/f4b6a3/ulid/factory/LexicalOrderGuidCreatorTest.java +++ b/src/test/java/com/github/f4b6a3/ulid/factory/LexicalOrderGuidCreatorTest.java @@ -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; diff --git a/src/test/java/com/github/f4b6a3/ulid/timestamp/DefaultTimestampStrategyTest.java b/src/test/java/com/github/f4b6a3/ulid/timestamp/DefaultTimestampStrategyTest.java index b3d4902..5b8d251 100644 --- a/src/test/java/com/github/f4b6a3/ulid/timestamp/DefaultTimestampStrategyTest.java +++ b/src/test/java/com/github/f4b6a3/ulid/timestamp/DefaultTimestampStrategyTest.java @@ -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); + } }