Improved UUID manager

This commit is contained in:
Luca Frosini 2023-04-20 10:00:46 +02:00
parent 746748494a
commit 3c3788f930
2 changed files with 36 additions and 4 deletions

View File

@ -57,11 +57,16 @@ public final class UUIDManager {
}
public UUID generateValidRandomUUID() {
UUID generated = UUID.randomUUID();
while(RESERVED_UUID.contains(generated)) {
generated = UUID.randomUUID();
UUID uuid = UUID.randomUUID();
return validateUUID(uuid);
}
public UUID validateUUID(UUID uuid) {
UUID ret = uuid;
while(RESERVED_UUID.contains(ret)) {
ret = UUID.randomUUID();
}
return generated;
return ret;
}

View File

@ -34,4 +34,31 @@ public class UUIDManagerTest {
}
}
@Test
public void testGenerateUUID() {
UUIDManager uuidManager = UUIDManager.getInstance();
Set<UUID> allReservedUUID = uuidManager.getAllReservedUUID();
UUID uuid = uuidManager.generateValidRandomUUID();
Assert.assertTrue(!allReservedUUID.contains(uuid));
uuid = UUID.randomUUID();
uuid = uuidManager.validateUUID(uuid);
Assert.assertTrue(!allReservedUUID.contains(uuid));
for(UUID reservedUUID : allReservedUUID) {
uuid = uuidManager.validateUUID(reservedUUID);
Assert.assertTrue(reservedUUID.compareTo(uuid)!=0);
}
}
}