diff --git a/README.md b/README.md index c650745..17a1e1c 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Add these lines to your `pom.xml`. com.github.f4b6a3 ulid-creator - 2.0.0 + 2.0.1 ``` See more options in [maven.org](https://search.maven.org/artifact/com.github.f4b6a3/ulid-creator). diff --git a/src/main/java/com/github/f4b6a3/ulid/creator/UlidSpecCreator.java b/src/main/java/com/github/f4b6a3/ulid/creator/UlidSpecCreator.java index e61899f..70ff192 100644 --- a/src/main/java/com/github/f4b6a3/ulid/creator/UlidSpecCreator.java +++ b/src/main/java/com/github/f4b6a3/ulid/creator/UlidSpecCreator.java @@ -47,8 +47,8 @@ public class UlidSpecCreator { protected long random1 = 0; protected long random2 = 0; - protected long randomMax2; protected long randomMax1; + protected long randomMax2; protected static final long HALF_RANDOM_COMPONENT = 0x000000ffffffffffL; protected static final long INCREMENT_MAX = 0x0000010000000000L; @@ -63,7 +63,6 @@ public class UlidSpecCreator { public UlidSpecCreator() { this.timestampStrategy = new DefaultTimestampStrategy(); this.randomStrategy = new DefaultRandomStrategy(); - this.reset(); } /** @@ -181,6 +180,7 @@ public class UlidSpecCreator { this.randomStrategy.nextBytes(bytes); this.random1 = UlidUtil.toNumber(bytes, 0, 5); this.random2 = UlidUtil.toNumber(bytes, 5, 10); + // Save the random values this.randomMax1 = this.random1 | INCREMENT_MAX; this.randomMax2 = this.random2 | INCREMENT_MAX; @@ -194,11 +194,13 @@ public class UlidSpecCreator { * * @throws UlidCreatorException if an overrun happens. */ - protected synchronized void increment() { - if ((++this.random2 > this.randomMax2) && (++this.random1 > this.randomMax1)) { - this.reset(); - throw new UlidCreatorException(OVERRUN_MESSAGE); + if (++this.random2 >= this.randomMax2) { + this.random2 = this.random2 & HALF_RANDOM_COMPONENT; + if ((++this.random1 >= this.randomMax1)) { + this.reset(); + throw new UlidCreatorException(OVERRUN_MESSAGE); + } } } diff --git a/src/test/java/com/github/f4b6a3/ulid/creator/UlidSpecCreatorMock.java b/src/test/java/com/github/f4b6a3/ulid/creator/UlidSpecCreatorMock.java index 4cbbd41..7d890c2 100644 --- a/src/test/java/com/github/f4b6a3/ulid/creator/UlidSpecCreatorMock.java +++ b/src/test/java/com/github/f4b6a3/ulid/creator/UlidSpecCreatorMock.java @@ -4,13 +4,9 @@ import com.github.f4b6a3.ulid.creator.UlidSpecCreator; class UlidSpecCreatorMock extends UlidSpecCreator { - public UlidSpecCreatorMock(long previousTimestamp) { - super(); - this.previousTimestamp = previousTimestamp; - } - public UlidSpecCreatorMock(long random1, long random2, long randomMax1, long randomMax2, long previousTimestamp) { - + super(); + this.random1 = random1; this.random2 = random2; diff --git a/src/test/java/com/github/f4b6a3/ulid/creator/UlidSpecCreatorTest.java b/src/test/java/com/github/f4b6a3/ulid/creator/UlidSpecCreatorTest.java index d630bc6..d13c605 100644 --- a/src/test/java/com/github/f4b6a3/ulid/creator/UlidSpecCreatorTest.java +++ b/src/test/java/com/github/f4b6a3/ulid/creator/UlidSpecCreatorTest.java @@ -37,7 +37,7 @@ public class UlidSpecCreatorTest { @Test public void testRandomMostSignificantBits() { - UlidSpecCreatorMock creator = new UlidSpecCreatorMock(TIMESTAMP); + UlidSpecCreator creator = new UlidSpecCreator(); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); UUID uuid = creator.create(); @@ -61,7 +61,7 @@ public class UlidSpecCreatorTest { @Test public void testRandomLeastSignificantBits() { - UlidSpecCreatorMock creator = new UlidSpecCreatorMock(TIMESTAMP); + UlidSpecCreator creator = new UlidSpecCreator(); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); UUID uuid = creator.create(); @@ -84,10 +84,11 @@ public class UlidSpecCreatorTest { @Test public void testIncrementOfRandomLeastSignificantBits() { - UlidSpecCreatorMock creator = new UlidSpecCreatorMock(TIMESTAMP); + UlidSpecCreator creator = new UlidSpecCreator(); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); - long random2 = creator.getRandom2(); + creator.create(); + long random2 = creator.random2; UUID uuid = new UUID(0, 0); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { @@ -95,7 +96,7 @@ public class UlidSpecCreatorTest { } long expected2 = random2 + DEFAULT_LOOP_MAX; - long rand2 = creator.getRandom2(); + long rand2 = creator.random2; assertEquals("Wrong low random after loop.", expected2, rand2); rand2 = creator.extractRandom2(uuid); @@ -105,10 +106,11 @@ public class UlidSpecCreatorTest { @Test public void testIncrementOfRandomMostSignificantBits() { - UlidSpecCreatorMock creator = new UlidSpecCreatorMock(TIMESTAMP); + UlidSpecCreator creator = new UlidSpecCreator(); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); - long random1 = creator.getRandom1(); + creator.create(); + long random1 = creator.random1; UUID uuid = new UUID(0, 0); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { @@ -116,7 +118,7 @@ public class UlidSpecCreatorTest { } long expected1 = random1; - long rand1 = creator.getRandom1(); + long rand1 = creator.random1; assertEquals("Wrong high random after loop.", expected1, rand1); rand1 = creator.extractRandom1(uuid); @@ -135,6 +137,8 @@ public class UlidSpecCreatorTest { random1 = max1; random2 = max2 - DEFAULT_LOOP_MAX; + random2--; // Adjust + UlidSpecCreatorMock creator = new UlidSpecCreatorMock(random1, random2, max1, max2, TIMESTAMP); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); @@ -173,6 +177,8 @@ public class UlidSpecCreatorTest { random1 = max1; random2 = max2 - DEFAULT_LOOP_MAX; + random2--; // Adjust + UlidSpecCreatorMock creator = new UlidSpecCreatorMock(random1, random2, max1, max2, TIMESTAMP); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); @@ -186,7 +192,7 @@ public class UlidSpecCreatorTest { assertEquals("Incorrect high random after loop.", expected1, rand1); long rand2 = creator.extractRandom2(uuid); - long expected2 = (max2 & UlidSpecCreatorMock.HALF_RANDOM_COMPONENT); + long expected2 = (max2 & UlidSpecCreatorMock.HALF_RANDOM_COMPONENT) - 1; assertEquals("Incorrect low random after loop.", expected2, rand2); long hi1 = random1 & UlidSpecCreatorMock.HALF_RANDOM_COMPONENT;