Improved UUID manager
This commit is contained in:
parent
746748494a
commit
3c3788f930
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue