Fix increment

Fix UlidSpecCreator.increment()
This commit is contained in:
Fabio Lima 2020-07-06 04:31:17 -03:00
parent 1deb79b52e
commit 3929965fe9
4 changed files with 26 additions and 22 deletions

View File

@ -28,7 +28,7 @@ Add these lines to your `pom.xml`.
<dependency> <dependency>
<groupId>com.github.f4b6a3</groupId> <groupId>com.github.f4b6a3</groupId>
<artifactId>ulid-creator</artifactId> <artifactId>ulid-creator</artifactId>
<version>2.0.0</version> <version>2.0.1</version>
</dependency> </dependency>
``` ```
See more options in [maven.org](https://search.maven.org/artifact/com.github.f4b6a3/ulid-creator). See more options in [maven.org](https://search.maven.org/artifact/com.github.f4b6a3/ulid-creator).

View File

@ -47,8 +47,8 @@ public class UlidSpecCreator {
protected long random1 = 0; protected long random1 = 0;
protected long random2 = 0; protected long random2 = 0;
protected long randomMax2;
protected long randomMax1; protected long randomMax1;
protected long randomMax2;
protected static final long HALF_RANDOM_COMPONENT = 0x000000ffffffffffL; protected static final long HALF_RANDOM_COMPONENT = 0x000000ffffffffffL;
protected static final long INCREMENT_MAX = 0x0000010000000000L; protected static final long INCREMENT_MAX = 0x0000010000000000L;
@ -63,7 +63,6 @@ public class UlidSpecCreator {
public UlidSpecCreator() { public UlidSpecCreator() {
this.timestampStrategy = new DefaultTimestampStrategy(); this.timestampStrategy = new DefaultTimestampStrategy();
this.randomStrategy = new DefaultRandomStrategy(); this.randomStrategy = new DefaultRandomStrategy();
this.reset();
} }
/** /**
@ -181,6 +180,7 @@ public class UlidSpecCreator {
this.randomStrategy.nextBytes(bytes); this.randomStrategy.nextBytes(bytes);
this.random1 = UlidUtil.toNumber(bytes, 0, 5); this.random1 = UlidUtil.toNumber(bytes, 0, 5);
this.random2 = UlidUtil.toNumber(bytes, 5, 10); this.random2 = UlidUtil.toNumber(bytes, 5, 10);
// Save the random values // Save the random values
this.randomMax1 = this.random1 | INCREMENT_MAX; this.randomMax1 = this.random1 | INCREMENT_MAX;
this.randomMax2 = this.random2 | INCREMENT_MAX; this.randomMax2 = this.random2 | INCREMENT_MAX;
@ -194,11 +194,13 @@ public class UlidSpecCreator {
* *
* @throws UlidCreatorException if an overrun happens. * @throws UlidCreatorException if an overrun happens.
*/ */
protected synchronized void increment() { protected synchronized void increment() {
if ((++this.random2 > this.randomMax2) && (++this.random1 > this.randomMax1)) { if (++this.random2 >= this.randomMax2) {
this.reset(); this.random2 = this.random2 & HALF_RANDOM_COMPONENT;
throw new UlidCreatorException(OVERRUN_MESSAGE); if ((++this.random1 >= this.randomMax1)) {
this.reset();
throw new UlidCreatorException(OVERRUN_MESSAGE);
}
} }
} }

View File

@ -4,12 +4,8 @@ import com.github.f4b6a3.ulid.creator.UlidSpecCreator;
class UlidSpecCreatorMock extends 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) { public UlidSpecCreatorMock(long random1, long random2, long randomMax1, long randomMax2, long previousTimestamp) {
super();
this.random1 = random1; this.random1 = random1;
this.random2 = random2; this.random2 = random2;

View File

@ -37,7 +37,7 @@ public class UlidSpecCreatorTest {
@Test @Test
public void testRandomMostSignificantBits() { public void testRandomMostSignificantBits() {
UlidSpecCreatorMock creator = new UlidSpecCreatorMock(TIMESTAMP); UlidSpecCreator creator = new UlidSpecCreator();
creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP));
UUID uuid = creator.create(); UUID uuid = creator.create();
@ -61,7 +61,7 @@ public class UlidSpecCreatorTest {
@Test @Test
public void testRandomLeastSignificantBits() { public void testRandomLeastSignificantBits() {
UlidSpecCreatorMock creator = new UlidSpecCreatorMock(TIMESTAMP); UlidSpecCreator creator = new UlidSpecCreator();
creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP));
UUID uuid = creator.create(); UUID uuid = creator.create();
@ -84,10 +84,11 @@ public class UlidSpecCreatorTest {
@Test @Test
public void testIncrementOfRandomLeastSignificantBits() { public void testIncrementOfRandomLeastSignificantBits() {
UlidSpecCreatorMock creator = new UlidSpecCreatorMock(TIMESTAMP); UlidSpecCreator creator = new UlidSpecCreator();
creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP));
long random2 = creator.getRandom2(); creator.create();
long random2 = creator.random2;
UUID uuid = new UUID(0, 0); UUID uuid = new UUID(0, 0);
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
@ -95,7 +96,7 @@ public class UlidSpecCreatorTest {
} }
long expected2 = random2 + DEFAULT_LOOP_MAX; long expected2 = random2 + DEFAULT_LOOP_MAX;
long rand2 = creator.getRandom2(); long rand2 = creator.random2;
assertEquals("Wrong low random after loop.", expected2, rand2); assertEquals("Wrong low random after loop.", expected2, rand2);
rand2 = creator.extractRandom2(uuid); rand2 = creator.extractRandom2(uuid);
@ -105,10 +106,11 @@ public class UlidSpecCreatorTest {
@Test @Test
public void testIncrementOfRandomMostSignificantBits() { public void testIncrementOfRandomMostSignificantBits() {
UlidSpecCreatorMock creator = new UlidSpecCreatorMock(TIMESTAMP); UlidSpecCreator creator = new UlidSpecCreator();
creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP));
long random1 = creator.getRandom1(); creator.create();
long random1 = creator.random1;
UUID uuid = new UUID(0, 0); UUID uuid = new UUID(0, 0);
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
@ -116,7 +118,7 @@ public class UlidSpecCreatorTest {
} }
long expected1 = random1; long expected1 = random1;
long rand1 = creator.getRandom1(); long rand1 = creator.random1;
assertEquals("Wrong high random after loop.", expected1, rand1); assertEquals("Wrong high random after loop.", expected1, rand1);
rand1 = creator.extractRandom1(uuid); rand1 = creator.extractRandom1(uuid);
@ -135,6 +137,8 @@ public class UlidSpecCreatorTest {
random1 = max1; random1 = max1;
random2 = max2 - DEFAULT_LOOP_MAX; random2 = max2 - DEFAULT_LOOP_MAX;
random2--; // Adjust
UlidSpecCreatorMock creator = new UlidSpecCreatorMock(random1, random2, max1, max2, TIMESTAMP); UlidSpecCreatorMock creator = new UlidSpecCreatorMock(random1, random2, max1, max2, TIMESTAMP);
creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP));
@ -173,6 +177,8 @@ public class UlidSpecCreatorTest {
random1 = max1; random1 = max1;
random2 = max2 - DEFAULT_LOOP_MAX; random2 = max2 - DEFAULT_LOOP_MAX;
random2--; // Adjust
UlidSpecCreatorMock creator = new UlidSpecCreatorMock(random1, random2, max1, max2, TIMESTAMP); UlidSpecCreatorMock creator = new UlidSpecCreatorMock(random1, random2, max1, max2, TIMESTAMP);
creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP)); creator.withTimestampStrategy(new FixedTimestampStretegy(TIMESTAMP));
@ -186,7 +192,7 @@ public class UlidSpecCreatorTest {
assertEquals("Incorrect high random after loop.", expected1, rand1); assertEquals("Incorrect high random after loop.", expected1, rand1);
long rand2 = creator.extractRandom2(uuid); 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); assertEquals("Incorrect low random after loop.", expected2, rand2);
long hi1 = random1 & UlidSpecCreatorMock.HALF_RANDOM_COMPONENT; long hi1 = random1 & UlidSpecCreatorMock.HALF_RANDOM_COMPONENT;