Skip to content

Instantly share code, notes, and snippets.

@justinoboyle
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinoboyle/51eac6e054b44eafdf8e to your computer and use it in GitHub Desktop.
Save justinoboyle/51eac6e054b44eafdf8e to your computer and use it in GitHub Desktop.
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