Top Description Inners Fields Constructors Methods
io.github.thibaultmeyer.cuid

public final Class CUID

extends Object
implements Serializable, Comparable<CUID>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable, java.io.Serializable
Imports
io.github.thibaultmeyer.cuid.exception.CUIDGenerationException, java.io.Serializable, java.lang.management.ManagementFactory, java.math.BigInteger, java.nio.charset.StandardCharsets, java.security.MessageDigest, .NoSuchAlgorithmException, .SecureRandom, java.util.Objects

Collision-resistant ID optimized for horizontal scaling and performance.
Since
1.0.0
See Also
CUID official website

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
CUID.CUIDv1

CUID Version 1.

private static class
CUID.CUIDv2

CUID Version 2.

Field Summary

Modifier and TypeField and Description
private static final int
private static final long
private final String
value

CUID internal value holder.

Constructor Summary

AccessConstructor and Description
private
CUID(final String
A valid CUID value
value
)

Creates a new instance.

Method Summary

Modifier and TypeMethod and Description
public int
compareTo(final CUID cuid)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.

public boolean
equals(final Object
An object
o
)

Overrides java.lang.Object.equals.

Returns true if the argument is equal to current object, false otherwise.

public static CUID

Returns:

A CUID with the specified value
fromString
(final String
A string that specifies a CUID (Version 1 or 2)
cuidAsString
)

Creates a CUID from the string standard representation.

public int

Returns:

Generated hashcode
hashCode
()

Overrides java.lang.Object.hashCode.

Generates a hash code.

public static boolean

Returns:

true If the string is conforms, otherwise, false
isValid
(final String
A string that specifies a CUID (Version 1 or 2)
cuidAsString
)

Checks the CUID from the string standard representation.

public static CUID

Returns:

Newly generated CUID (Version 1)
randomCUID1
()

Generates a new random CUID (Version 1).

public static CUID

Returns:

Newly generated CUID (Version 2)
randomCUID2
()

Generates a new random CUID (Version 2).

public static CUID

Returns:

Newly generated CUID (Version 2)
randomCUID2
(final int
requested CUID length
length
)

Generates a new random CUID (Version 2).

private static int

Returns:

a non-negative value
safeAbs
(final int
the integer value
i
)

Always return non-negative value.

public String

Returns:

String containing the CUID
toString
()

Overrides java.lang.Object.toString.

Returns the string representation.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

NUMBER_BASEback to summary
private static final int NUMBER_BASE
serialVersionUIDback to summary
private static final long serialVersionUID
valueback to summary
private final String value

CUID internal value holder.

Constructor Detail

CUIDback to summary
private CUID(final String value)

Creates a new instance.

Parameters
value:String

A valid CUID value

Since
1.0.0

Method Detail

compareToback to summary
public int compareTo(final CUID cuid)

Implements java.lang.Comparable.compareTo.

Doc from java.lang.Comparable.compareTo.

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor must ensure signum(x.compareTo(y)) == -signum(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0.

Finally, the implementor must ensure that x.compareTo(y)==0 implies that signum(x.compareTo(z)) == signum(y.compareTo(z)), for all z.

Annotations
@Override
Since
1.0.0
equalsback to summary
public boolean equals(final Object o)

Overrides java.lang.Object.equals.

Returns true if the argument is equal to current object, false otherwise.

Parameters
o:Object

An object

Annotations
@Override
Since
1.0.0
fromStringback to summary
public static CUID fromString(final String cuidAsString)

Creates a CUID from the string standard representation.

Parameters
cuidAsString:String

A string that specifies a CUID (Version 1 or 2)

Returns:CUID

A CUID with the specified value

Exceptions
IllegalArgumentException:
If the string is not conform
Since
1.0.0
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Generates a hash code.

Returns:int

Generated hashcode

Annotations
@Override
Since
1.0.0
isValidback to summary
public static boolean isValid(final String cuidAsString)

Checks the CUID from the string standard representation.

Parameters
cuidAsString:String

A string that specifies a CUID (Version 1 or 2)

Returns:boolean

true If the string is conforms, otherwise, false

Since
1.0.0
randomCUID1back to summary
public static CUID randomCUID1()

Generates a new random CUID (Version 1).

Returns:CUID

Newly generated CUID (Version 1)

Since
2.0.0
randomCUID2back to summary
public static CUID randomCUID2()

Generates a new random CUID (Version 2).

Returns:CUID

Newly generated CUID (Version 2)

Since
2.0.0
randomCUID2back to summary
public static CUID randomCUID2(final int length)

Generates a new random CUID (Version 2).

Parameters
length:int

requested CUID length

Returns:CUID

Newly generated CUID (Version 2)

Since
2.0.1
safeAbsback to summary
private static int safeAbs(final int i)

Always return non-negative value.

Parameters
i:int

the integer value

Returns:int

a non-negative value

Since
2.0.3
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the string representation.

Returns:String

String containing the CUID

Annotations
@Override
Since
1.0.0
io.github.thibaultmeyer.cuid back to summary

private final Class CUID.Common

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final String
private static final SecureRandom
private static final byte[]
private static final int
private static int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static String

Returns:

The machine fingerprint
getMachineFingerprint
()

retrieves the machine fingerprint.

private static synchronized int

Returns:

A random integer
nextIntValue
()

Retrieves next random integer value.

private static String

Returns:

The padded string
padWithZero
(final String
The string to pad
str
,
final int
The size to keep
size
)

Pads string with leading zero.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

MACHINE_FINGERPRINTback to summary
private static final String MACHINE_FINGERPRINT
NUMBER_GENERATORback to summary
private static final SecureRandom NUMBER_GENERATOR
RANDOM_BUFFERback to summary
private static final byte[] RANDOM_BUFFER
RANDOM_BUFFER_SIZEback to summary
private static final int RANDOM_BUFFER_SIZE
randomBufferIndexback to summary
private static int randomBufferIndex

Constructor Detail

Commonback to summary
private Common()

Method Detail

getMachineFingerprintback to summary
private static String getMachineFingerprint()

retrieves the machine fingerprint.

Returns:String

The machine fingerprint

Since
1.0.0
nextIntValueback to summary
private static synchronized int nextIntValue()

Retrieves next random integer value.

Returns:int

A random integer

Since
1.0.0
padWithZeroback to summary
private static String padWithZero(final String str, final int size)

Pads string with leading zero.

Parameters
str:String

The string to pad

size:int

The size to keep

Returns:String

The padded string

Since
1.0.0
io.github.thibaultmeyer.cuid back to summary

private final Class CUID.CUIDv1

extends Object
Class Inheritance

CUID Version 1.
Since
1.0.0

Field Summary

Modifier and TypeField and Description
private static final int
private static int
private static final int
private static final int
private static final String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static String

Returns:

Newly generated block of data
getRandomBlock
()

Generates a random block of data.

private static synchronized int

Returns:

The counter next value
nextCounterValue
()

Retrieves the counter next value.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BLOCK_SIZEback to summary
private static final int BLOCK_SIZE
counterback to summary
private static int counter
DISCRETE_VALUEback to summary
private static final int DISCRETE_VALUE
LENGTH_STANDARDback to summary
private static final int LENGTH_STANDARD
START_CHARACTERback to summary
private static final String START_CHARACTER

Constructor Detail

CUIDv1back to summary
private CUIDv1()

Method Detail

getRandomBlockback to summary
private static String getRandomBlock()

Generates a random block of data.

Returns:String

Newly generated block of data

Since
1.0.0
nextCounterValueback to summary
private static synchronized int nextCounterValue()

Retrieves the counter next value.

Returns:int

The counter next value

Since
1.0.0
io.github.thibaultmeyer.cuid back to summary

private final Class CUID.CUIDv2

extends Object
Class Inheritance

CUID Version 2.
Since
2.0.0

Field Summary

Modifier and TypeField and Description
private static final char[]
private static int
private static final int
private static final int[]

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static String

Returns:

String containing hash
computeHash
(final String content, final int saltLength)

Computes hash.

private static String

Returns:

String containing entropy in base CUID#NUMBER_BASE
createEntropy
(final int
Length of the entropy string
length
)

Creates an entropy string.

private static synchronized int

Returns:

The counter next value
nextCounterValue
()

Retrieves the counter next value.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ALPHABET_ARRAYback to summary
private static final char[] ALPHABET_ARRAY
counterback to summary
private static int counter
LENGTH_STANDARDback to summary
private static final int LENGTH_STANDARD
PRIME_NUMBER_ARRAYback to summary
private static final int[] PRIME_NUMBER_ARRAY

Constructor Detail

CUIDv2back to summary
private CUIDv2()

Method Detail

computeHashback to summary
private static String computeHash(final String content, final int saltLength)

Computes hash.

Returns:String

String containing hash

createEntropyback to summary
private static String createEntropy(final int length)

Creates an entropy string.

Parameters
length:int

Length of the entropy string

Returns:String

String containing entropy in base CUID#NUMBER_BASE

nextCounterValueback to summary
private static synchronized int nextCounterValue()

Retrieves the counter next value.

Returns:int

The counter next value