public class IdGenerator
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected static int |
DEFAULT_ID_SIZE
The default ID size.
|
protected IResultCommand<java.lang.String,byte[]> |
encoder |
protected java.util.Set<java.lang.String> |
generatedIds
Set of generated IDs to guarantee collision-free IDs.
|
protected int |
idsize
The selected id size.
|
protected java.util.Random |
random
Random number generator.
|
Constructor and Description |
---|
IdGenerator()
Creates a new ID generator.
|
IdGenerator(boolean collisionfree)
Creates a new ID generator.
|
IdGenerator(int idsize)
Creates a new ID generator.
|
IdGenerator(int idsize,
boolean collisionfree,
java.util.Random random)
Creates a new ID generator.
|
IdGenerator(int idsize,
java.util.Random random)
Creates a new ID generator.
|
Modifier and Type | Method and Description |
---|---|
void |
addUsedId(java.lang.String id)
Adds a used ID, only useful for collision-free mode.
|
void |
base16Mode()
Switches the ID generator to BASE16 mode.
|
void |
base64Mode()
Switches the ID generator to BASE64 mode.
|
java.lang.String |
generateId()
Generates an ID.
|
static void |
main(java.lang.String[] args) |
void |
removeUsedId(java.lang.String id)
Removes a used ID, only useful in collision-free mode.
|
protected static final int DEFAULT_ID_SIZE
protected java.util.Random random
protected int idsize
protected IResultCommand<java.lang.String,byte[]> encoder
protected java.util.Set<java.lang.String> generatedIds
public IdGenerator()
public IdGenerator(int idsize)
idsize
- Size of ID entropy in bytes, actual ID is slightly larger due to Base64 encoding.public IdGenerator(boolean collisionfree)
collisionfree
- If true, this will guarantee collision-free IDs. However, requires maintaining
a set of used IDs, increasing memory consumption. This option is unnecessary for
most use cases due to the extremely low probability of collisions at reasonable
ID sizes.public IdGenerator(int idsize, java.util.Random random)
public IdGenerator(int idsize, boolean collisionfree, java.util.Random random)
idsize
- Size of ID entropy in bytes, actual ID is slightly larger due to Base64 encoding.collisionfree
- If true, this will guarantee collision-free IDs. However, requires maintaining
a set of used IDs, increasing memory consumption. This option is unnecessary for
most use cases due to the extremely low probability of collisions at reasonable
ID sizes.public java.lang.String generateId()
public void addUsedId(java.lang.String id)
id
- The id.public void removeUsedId(java.lang.String id)
id
- The ID.public void base64Mode()
public void base16Mode()
public static void main(java.lang.String[] args)