Package dev.rafandoo.cup.utils
Class UniqueIdentifiers
java.lang.Object
dev.rafandoo.cup.utils.UniqueIdentifiers
The
UniqueIdentifiers class provides a set of static methods for generating
various types of unique identifiers, including UUIDs (Versions 1 to 7) and ULIDs.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic UUIDconvertUlidToUUID(com.github.f4b6a3.ulid.Ulid ulid) Converts a ULID to a UUID (RFC-4122 format).static UUIDconvertUlidToUUIDv4(com.github.f4b6a3.ulid.Ulid ulid) Converts a ULID to a Version 4 UUID.static com.github.f4b6a3.ulid.UlidgetHashUlid(long time, String name) Generates a hash-based ULID using the specified timestamp and name.static com.github.f4b6a3.ulid.UlidGenerates a monotonic ULID, which guarantees that ULIDs are ordered if generated within the same millisecond.static com.github.f4b6a3.ulid.UlidGenerates a fast ULID.static com.github.f4b6a3.ulid.UlidgetULID()Generates a ULID (Universally Unique Lexicographically Sortable Identifier).static com.github.f4b6a3.ulid.UlidgetUlidFromString(String ulid) Converts a string into a ULID.static InstantgetUlidInstant(com.github.f4b6a3.ulid.Ulid ulid) Extracts the timestamp from a ULID and returns it as anInstant.static UUIDgetUUIDFromString(String uuid) Converts a string into a UUID.static UUIDGenerates a time-based UUID (Version 1).static UUIDgetUUIDv2(int localIdentifier) Generates a DCE Security UUID (Version 2).static UUIDGenerates a name-based UUID (Version 3) using MD5 hashing.static UUIDGenerates a random-based UUID (Version 4).static UUIDGenerates a name-based UUID (Version 5) using SHA-1 hashing.static UUIDGenerates a time-ordered UUID (Version 6).static UUIDGenerates a Unix Epoch time-based UUID (Version 7).static booleanisValidUlid(String ulid) Checks if a ULID is valid.static booleanisValidUUID(String uuid) Checks if a UUID is valid.
-
Constructor Details
-
UniqueIdentifiers
public UniqueIdentifiers()
-
-
Method Details
-
getUUIDv1
Generates a time-based UUID (Version 1). This UUID includes a timestamp and a machine identifier.- Returns:
- a UUIDv1.
-
getUUIDv2
Generates a DCE Security UUID (Version 2). This UUID includes a timestamp, local domain, and local identifier.- Parameters:
localIdentifier- the local identifier to include in the UUID.- Returns:
- a UUIDv2.
-
getUUIDv3
Generates a name-based UUID (Version 3) using MD5 hashing. The generated UUID is based on the given name and the URL namespace.- Parameters:
name- the name to hash into a UUID.- Returns:
- a UUIDv3.
-
getUUIDv4
Generates a random-based UUID (Version 4). This UUID is purely random and does not contain any timestamp or machine identifier.- Returns:
- a UUIDv4.
-
getUUIDv5
Generates a name-based UUID (Version 5) using SHA-1 hashing. The generated UUID is based on the given name and the URL namespace.- Parameters:
name- the name to hash into a UUID.- Returns:
- a UUIDv5.
-
getUUIDv6
Generates a time-ordered UUID (Version 6). This UUID includes a timestamp, and its ordering is optimized for database indexing.- Returns:
- a UUIDv6.
-
getUUIDv7
Generates a Unix Epoch time-based UUID (Version 7). This UUID includes a timestamp with millisecond precision and random data.- Returns:
- a UUIDv7.
-
getULID
public static com.github.f4b6a3.ulid.Ulid getULID()Generates a ULID (Universally Unique Lexicographically Sortable Identifier). This identifier is based on the current time and random data.- Returns:
- a ULID.
-
getMonotonicUlid
public static com.github.f4b6a3.ulid.Ulid getMonotonicUlid()Generates a monotonic ULID, which guarantees that ULIDs are ordered if generated within the same millisecond.- Returns:
- a monotonic ULID.
-
getHashUlid
Generates a hash-based ULID using the specified timestamp and name.- Parameters:
time- the timestamp to include in the ULID.name- the name to hash into the ULID.- Returns:
- a hash-based ULID.
-
getQuickUlid
public static com.github.f4b6a3.ulid.Ulid getQuickUlid()Generates a fast ULID. This method is optimized for speed.- Returns:
- a fast ULID.
-
convertUlidToUUID
Converts a ULID to a UUID (RFC-4122 format).- Parameters:
ulid- the ULID to convert.- Returns:
- the equivalent UUID.
-
convertUlidToUUIDv4
Converts a ULID to a Version 4 UUID.- Parameters:
ulid- the ULID to convert.- Returns:
- the equivalent UUIDv4.
-
getUlidInstant
Extracts the timestamp from a ULID and returns it as anInstant.- Parameters:
ulid- the ULID to extract the timestamp from.- Returns:
- the timestamp as an
Instant.
-
isValidUlid
Checks if a ULID is valid.- Parameters:
ulid- the ULID to check.- Returns:
trueif the ULID is valid,falseotherwise.
-
isValidUUID
Checks if a UUID is valid.- Parameters:
uuid- the UUID to check.- Returns:
trueif the UUID is valid,falseotherwise.
-
getUUIDFromString
Converts a string into a UUID.- Parameters:
uuid- the string to convert.- Returns:
- the UUID.
-
getUlidFromString
Converts a string into a ULID.- Parameters:
ulid- the string to convert.- Returns:
- the ULID.
-