com.tectonica.util.NewId Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tectonica-commons Show documentation
Show all versions of tectonica-commons Show documentation
Set of Java utility classes, all completely independent, to provide lightweight solutions for common situations
package com.tectonica.util;
import java.util.Random;
public class NewId
{
private static Random rand = new Random();
/**
* returns a so-called Time-UUID, i.e. a UUID that guarantees no conflicts at the following probabilities:
*
* - 99.9999973% (or 1 - 2.7e-8) if called 1,000,000 times in every millisecond
*
- value of (1 - 2.7e-14) if called once in every microsecond
*
- either way, UUIDs within different milliseconds have 0% probability of conflicting
*
* The resulting UUID when used in sort retains order of creation
*/
public static String generate()
{
String timePart = Long.toHexString(System.currentTimeMillis());
// all dates between 2004 and and 2527 are taking up 11 hex digits, so the following is unnecessary
// timePart = StrUtils.leftPad(timePart, 11, '0');
String randomPart = Long.toHexString(rand.nextLong());
return timePart + randomPart;
}
/**
* convenience method for adding prefix to the generated id
*/
public static String generate(String prefix)
{
return prefix + generate();
}
}