UUIDTest example original

From GGCWiki
Jump to: navigation, search
import java.util.ArrayList;
import java.util.UUID;

public class UUIDTest {

	static ArrayList<UUID> tokens;

	// main() -- the "poor man's" tester!
	public static void main(String[] args) throws Exception {
		tokens = new ArrayList<UUID>();
		for (int i = 0; i < 1000; i++) {
			tokens.add(getNewDurableTokenId());
		}
	}

	public static UUID getNewDurableTokenId() throws Exception {
		UUID uuid = UUID.randomUUID();
		while (getUserIdFromTokenId(uuid) != null) {
			getNewDurableTokenId();
		}
		return uuid;
	}

	public static String getUserIdFromTokenId(UUID _uuid) {
		if (tokens.contains(_uuid))
			// return user's id based on token supplied
			// this is simulated for use in class, this would really be a
			// database lookup of some kind
			return _uuid.toString().substring(0, 4).toUpperCase();
		else
			// return null if there is no id for the token supplied
			return null;
	}
}

Better getNewDurableTokenId() Solution

Personal tools