public class Identifiers
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
BASE64_VALID_CHARS |
static java.lang.String |
ID_VALID_NONSTART_CHARS |
static java.lang.String |
ID_VALID_START_CHARS |
static java.lang.String |
JAVA_GENERATED_IDENTIFIER_START_CHARS |
static java.lang.String |
JAVA_GENERATED_IDENTIFIERNONSTART_CHARS |
static java.lang.String |
JAVA_GOOD_NONSTART_CHARS |
static java.lang.String |
JAVA_GOOD_START_CHARS |
Constructor and Description |
---|
Identifiers() |
Modifier and Type | Method and Description |
---|---|
static void |
appendBase64IdFromValueOfLength(long value,
int length,
java.lang.StringBuffer sb) |
static void |
appendBase64IdFromValueOfLength(long value,
int length,
java.lang.StringBuilder sb) |
static java.lang.String |
getBase64IdFromValue(long value) |
static java.lang.String |
getBase64IdFromValue(long value,
int length) |
static boolean |
isValidToken(java.lang.String token,
java.lang.String validStartChars,
java.lang.String validSubsequentChars) |
static java.lang.String |
makeIdFromHash(long d)
creates a short identifier comfortable in java and OS's, given an input hash code
|
static java.lang.String |
makeRandomBase64Id(int length) |
static java.lang.String |
makeRandomId(int l)
makes a random id string (letters and numbers) of the given length;
starts with letter (upper or lower) so can be used as java-id;
tests ensure random distribution, so random ID of length 5
is about 2^29 possibilities
|
static java.lang.String |
makeRandomJavaId(int l)
makes a random id string (letters and numbers) of the given length;
starts with letter (upper or lower) so can be used as java-id;
tests ensure random distribution, so random ID of length 5
is about 2^29 possibilities
|
static boolean |
randomBoolean() |
static byte[] |
randomBytes(byte[] buf)
returns the array passed in
|
static byte[] |
randomBytes(int length) |
static double |
randomDouble() |
static int |
randomInt() |
static int |
randomInt(int upbound)
returns in [0,upbound)
|
static long |
randomLong() |
public static final java.lang.String JAVA_GOOD_START_CHARS
public static final java.lang.String JAVA_GOOD_NONSTART_CHARS
public static final java.lang.String JAVA_GENERATED_IDENTIFIER_START_CHARS
public static final java.lang.String JAVA_GENERATED_IDENTIFIERNONSTART_CHARS
public static final java.lang.String BASE64_VALID_CHARS
public static final java.lang.String ID_VALID_START_CHARS
public static final java.lang.String ID_VALID_NONSTART_CHARS
public static java.lang.String makeRandomId(int l)
With ID of length 4 it is not unlikely (15% chance) to get duplicates in the first 2000 attempts. With ID of length 8 there is 1% chance to get duplicates in the first 1M attempts and 50% for the first 16M.
implementation is efficient, uses char array, and makes one call to random per 5 chars; makeRandomId(5) takes about 4 times as long as a simple Math.random call, or about 50 times more than a simple x++ instruction; in other words, it's appropriate for contexts where random id's are needed, but use efficiently (ie cache it per object), and prefer to use a counter where feasible
in general this is preferable to base64 as is more portable, can be used throughout javascript (as ID's which don't allow +) or as java identifiers (which don't allow numbers in the first char)
public static java.lang.String makeIdFromHash(long d)
result is always at least of length 1, shorter if the hash is smaller
public static java.lang.String makeRandomJavaId(int l)
implementation is efficient, uses char array, and makes one call to random per 5 chars; makeRandomId(5) takes about 4 times as long as a simple Math.random call, or about 50 times more than a simple x++ instruction; in other words, it's appropriate for contexts where random id's are needed, but use efficiently (ie cache it per object), and prefer to use a counter where feasible
public static double randomDouble()
public static long randomLong()
public static boolean randomBoolean()
public static int randomInt()
public static int randomInt(int upbound)
public static byte[] randomBytes(byte[] buf)
public static byte[] randomBytes(int length)
public static java.lang.String makeRandomBase64Id(int length)
public static java.lang.String getBase64IdFromValue(long value)
public static java.lang.String getBase64IdFromValue(long value, int length)
public static void appendBase64IdFromValueOfLength(long value, int length, java.lang.StringBuffer sb)
public static void appendBase64IdFromValueOfLength(long value, int length, java.lang.StringBuilder sb)
public static boolean isValidToken(java.lang.String token, java.lang.String validStartChars, java.lang.String validSubsequentChars)