Skip to content

Commit

Permalink
Added UuidUtils add method. (#43)
Browse files Browse the repository at this point in the history
* Added UuidUtils.add method.
  • Loading branch information
onukristo authored Nov 29, 2023
1 parent 2bc56d1 commit e5fcab4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.12.1] - 2023-10-29

### Added

* UuidUtils.add method.

## [1.12.0] - 2023-10-03

### Changed
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.12.0
version=1.12.1
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public static byte[] toBytes(UUID uuid) {
return bytes;
}

public static UUID add(UUID uuid, long constant) {
if (uuid == null) {
throw new NullPointerException("Can not add anything to null.");
}

// Can overflow, but this is fine.
return new UUID(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits() + constant);
}

protected static long applyVersionBits(final long msb, int versionBits) {
return (msb & 0xffffffffffff0fffL) | versionBits;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.common.collect.Ordering;
Expand All @@ -11,6 +13,7 @@
import java.util.Arrays;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -45,6 +48,45 @@ void defaultPrefixCombUuidIsGrowingOverTime() {
assertEquals(n, Set.of(uuids).size());
}

@Test
void constantCanBeAddedToUuid() {
var uuid = UuidUtils.generatePrefixCombUuid();
var uuidWithConstant = UuidUtils.add(uuid, 11111);

assertNotEquals(uuidWithConstant, uuid);

assertThrows(NullPointerException.class, () -> UuidUtils.add(null, 0));
}

@Test
void addingConstantDoesNotChangeOrdering() {
TestClock clock = TestClock.createAndRegister();

int n = 100;

UUID[] uuids = new UUID[n];
for (int i = 0; i < n; i++) {
uuids[i] = UuidUtils.add(UuidUtils.generatePrefixCombUuid(), ThreadLocalRandom.current().nextInt());
clock.tick(Duration.ofMillis(2));
}

long previousTime = -1;
for (int i = 0; i < n; i++) {
long time = uuids[i].getMostSignificantBits() >>> (64 - 38);

if (previousTime != -1) {
assertTrue(previousTime < time);
}
previousTime = time;

System.out.println(uuids[i]);
assertEquals(4, uuids[i].version());
}

assertTrue(Ordering.natural().isOrdered(Arrays.asList(uuids)));
assertEquals(n, Set.of(uuids).size());
}

@Test
void convertingFromUuidAndBackToBytesEndWithTheSameResult() {
UUID expected = UUID.randomUUID();
Expand Down

0 comments on commit e5fcab4

Please sign in to comment.