Improved UUID management
This commit is contained in:
parent
42a650167b
commit
11b218fdec
|
@ -1,41 +0,0 @@
|
|||
package org.gcube.informationsystem.utils;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class ReservedUUID {
|
||||
|
||||
public static final Set<String> RESERVED_UUID_STRING;
|
||||
public static final Set<UUID> RESERVED_UUID;
|
||||
|
||||
static {
|
||||
RESERVED_UUID_STRING = new HashSet<>();
|
||||
RESERVED_UUID = new HashSet<>();
|
||||
|
||||
String[] uuidValidCharacters = {"1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
|
||||
|
||||
for(String string : uuidValidCharacters) {
|
||||
String uuidString = "00000000-0000-0000-0000-000000000000";
|
||||
uuidString.replaceAll("0", string);
|
||||
RESERVED_UUID_STRING.add(uuidString);
|
||||
RESERVED_UUID.add(UUID.fromString(uuidString));
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isReservedUUID(UUID uuid) {
|
||||
return RESERVED_UUID.contains(uuid);
|
||||
}
|
||||
|
||||
public static boolean isReservedUUID(String uuid) {
|
||||
return RESERVED_UUID_STRING.contains(uuid);
|
||||
}
|
||||
|
||||
public static Set<String> allUUIDAsString(){
|
||||
return new HashSet<>(RESERVED_UUID_STRING);
|
||||
}
|
||||
|
||||
public static Set<UUID> allUUID(){
|
||||
return new HashSet<>(RESERVED_UUID);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package org.gcube.informationsystem.utils;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public final class UUIDManager {
|
||||
|
||||
public static final Set<String> RESERVED_UUID_STRING;
|
||||
public static final Set<UUID> RESERVED_UUID;
|
||||
|
||||
private UUIDManager() {
|
||||
|
||||
}
|
||||
|
||||
static {
|
||||
RESERVED_UUID_STRING = new HashSet<>();
|
||||
RESERVED_UUID = new HashSet<>();
|
||||
|
||||
String[] uuidValidCharacters = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
|
||||
|
||||
for(String string : uuidValidCharacters) {
|
||||
String uuidString = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
|
||||
uuidString = uuidString.replaceAll("X", string);
|
||||
RESERVED_UUID_STRING.add(uuidString);
|
||||
RESERVED_UUID.add(UUID.fromString(uuidString));
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isReservedUUID(UUID uuid) {
|
||||
return RESERVED_UUID.contains(uuid);
|
||||
}
|
||||
|
||||
public static boolean isReservedUUID(String uuid) {
|
||||
return RESERVED_UUID_STRING.contains(uuid);
|
||||
}
|
||||
|
||||
public static Set<String> getAllReservedUUIDAsStrings(){
|
||||
return new TreeSet<>(RESERVED_UUID_STRING);
|
||||
}
|
||||
|
||||
public static Set<UUID> getAllReservedUUID(){
|
||||
return new TreeSet<>(RESERVED_UUID);
|
||||
}
|
||||
|
||||
public static UUID generateValidRandomUUID() {
|
||||
UUID generated = UUID.randomUUID();
|
||||
while(RESERVED_UUID.contains(generated)) {
|
||||
generated = UUID.randomUUID();
|
||||
}
|
||||
return generated;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package org.gcube.informationsystem.utils;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class UUIDManagerTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(UUIDManagerTest.class);
|
||||
|
||||
@Test
|
||||
public void testReservedUUID() {
|
||||
Set<UUID> allUUID = UUIDManager.getAllReservedUUID();
|
||||
logger.debug("Reserved UUIDs are {}", allUUID);
|
||||
|
||||
Set<String> allUUIDAsString = UUIDManager.getAllReservedUUIDAsStrings();
|
||||
Assert.assertTrue(allUUID.size()==allUUIDAsString.size());
|
||||
Assert.assertTrue(allUUID.size()==16);
|
||||
Assert.assertTrue(allUUIDAsString.size()==16);
|
||||
for(UUID uuid : allUUID) {
|
||||
Assert.assertTrue(UUIDManager.isReservedUUID(uuid));
|
||||
Assert.assertTrue(UUIDManager.isReservedUUID(uuid.toString()));
|
||||
}
|
||||
|
||||
for(String uuidString : allUUIDAsString) {
|
||||
Assert.assertTrue(UUIDManager.isReservedUUID(UUID.fromString(uuidString)));
|
||||
Assert.assertTrue(UUIDManager.isReservedUUID(uuidString));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue