Last active
August 29, 2015 14:19
-
-
Save justinoboyle/51eac6e054b44eafdf8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.UUID; | |
public class Example { | |
/** | |
* A final List<String> of all previous UUIDs generated with | |
* generateUniqueID(), turned into a string with uuid.toString(); | |
*/ | |
private static final List<String> PREVIOUS = new ArrayList<String>(); | |
/** | |
* Generates a truly unique UUID. | |
* | |
* @param previous | |
* A List<String> of previous UUIDs, converted into a string with | |
* uuid.toString(); | |
* @return a UUID generated with UUID.randomUUID(); that is not included in | |
* the given List<String>. | |
*/ | |
public static UUID generateUniqueID(List<String> previous) { | |
UUID u = UUID.randomUUID(); | |
if (previous.contains(u.toString())) { | |
return generateUniqueID(previous); | |
} | |
return u; | |
} | |
/** | |
* Generates a truly unique UUID using the final List<String> PREVIOUS | |
* variable defined at the top of the class. | |
* | |
* @return A truly random UUID created with generateUniqueID(List<String> | |
* previous); | |
*/ | |
public static UUID generateUniqueID() { | |
UUID u = generateUniqueID(PREVIOUS); | |
PREVIOUS.add(u.toString()); | |
return u; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment