public final class GUID extends Object implements Serializable, Comparable<GUID>
It serves as an alternative to the classic JDK's UUID.
It also serves as a self-contained generator, independent of the rest of the library. This can result in fewer classes being loaded.
This generator was designed to be an alternative to UuidCreator with
three primary objectives in mind: clean interface, simple implementation, and
high performance. It was inspired by popular libraries for Javascript and
Python.
The name GUID was chosen to avoid confusion with the classic JDK's UUID class. This naming choice was made so that when you see the word GUID in the source code, you can be sure it's not the JDK's built-in UUID.
Instances of this class are immutable and static methods of this class are thread safe.
| Modifier and Type | Field and Description |
|---|---|
static int |
GUID_BYTES
Number of bytes of a GUID.
|
static int |
GUID_CHARS
Number of characters of a GUID.
|
static byte |
LOCAL_DOMAIN_GROUP
The group domain, interpreted as POSIX GID domain on POSIX systems.
|
static byte |
LOCAL_DOMAIN_ORG
The organization domain, site-defined.
|
static byte |
LOCAL_DOMAIN_PERSON
The principal domain, interpreted as POSIX UID domain on POSIX systems.
|
static GUID |
MAX
A special GUID that has all 128 bits set to ONE.
|
static GUID |
NAMESPACE_DNS
Name space to be used when the name string is a fully-qualified domain name.
|
static GUID |
NAMESPACE_OID
Name space to be used when the name string is an ISO OID.
|
static GUID |
NAMESPACE_URL
Name space to be used when the name string is a URL.
|
static GUID |
NAMESPACE_X500
Name space to be used when the name string is an X.500 DN (DER or text).
|
static GUID |
NIL
A special GUID that has all 128 bits set to ZERO.
|
| Constructor and Description |
|---|
GUID(byte[] bytes)
Creates a new GUID.
|
GUID(GUID guid)
Creates a new GUID.
|
GUID(long mostSignificantBits,
long leastSignificantBits)
Creates a new GUID.
|
GUID(String string)
Creates a new GUID.
|
GUID(UUID uuid)
Creates a new GUID.
|
| Modifier and Type | Method and Description |
|---|---|
int |
compareTo(GUID other)
Compares two GUIDs as unsigned 128-bit integers.
|
boolean |
equals(Object other)
Checks if some other GUID is equal to this one.
|
int |
hashCode()
Returns a hash code value for the GUID.
|
byte[] |
toBytes()
Converts the GUID into a byte array.
|
String |
toString()
Converts the GUID into a canonical string.
|
UUID |
toUUID()
Converts the GUID into a JDK's UUID.
|
static GUID |
v1()
Returns a gregorian time-based unique identifier (UUIDv1).
|
static GUID |
v2(byte localDomain,
int localIdentifier)
Returns a DCE Security unique identifier (UUIDv2).
|
static GUID |
v3(GUID namespace,
String name)
Returns a name-based unique identifier that uses MD5 hashing (UUIDv3).
|
static GUID |
v4()
Returns a random-based unique identifier (UUIDv4).
|
static GUID |
v5(GUID namespace,
String name)
Returns a name-based unique identifier that uses SHA-1 hashing (UUIDv5).
|
static GUID |
v6()
Returns a reordered gregorian time-based unique identifier (UUIDv6).
|
static GUID |
v7()
Returns a Unix epoch time-based unique identifier (UUIDv7).
|
static boolean |
valid(String string)
Checks if the GUID string is valid.
|
int |
version()
Returns the version number of this GUID.
|
public static final GUID NIL
public static final GUID MAX
public static final GUID NAMESPACE_DNS
public static final GUID NAMESPACE_URL
public static final GUID NAMESPACE_OID
public static final GUID NAMESPACE_X500
public static final byte LOCAL_DOMAIN_PERSON
public static final byte LOCAL_DOMAIN_GROUP
public static final byte LOCAL_DOMAIN_ORG
public static final int GUID_CHARS
public static final int GUID_BYTES
public GUID(GUID guid)
Useful to make copies of GUIDs.
guid - a GUIDIllegalArgumentException - if the input is nullpublic GUID(UUID uuid)
Useful to make copies of JDK's UUIDs.
uuid - a JDK's UUIDIllegalArgumentException - if the input is nullpublic GUID(long mostSignificantBits,
long leastSignificantBits)
mostSignificantBits - the first 8 bytes as a long valueleastSignificantBits - the last 8 bytes as a long valuepublic GUID(byte[] bytes)
bytes - an array of 16 bytesIllegalArgumentException - if bytes are null or its length is not 16public GUID(String string)
string - a canonical stringIllegalArgumentException - if the input string is invalidpublic static GUID v1()
The clock sequence and node bits are reset to a pseudo-random value for each new UUIDv1 generated.
Usage:
GUID guid = GUID.v1();
public static GUID v2(byte localDomain, int localIdentifier)
Usage:
GUID guid = GUID.v2(Uuid.LOCAL_DOMAIN_PERSON, 1234567890);
localDomain - a custom local domain bytelocalIdentifier - a local identifierpublic static GUID v3(GUID namespace, String name)
Usage:
GUID guid = GUID.v3(Uuid.NAMESPACE_DNS, "www.example.com");
namespace - a GUID (optional)name - a stringNullPointerException - if the name is nullpublic static GUID v4()
It is an extremely fast and non-blocking alternative to
UUID.randomUUID().
It employs ThreadLocalRandom which works very well, although not
cryptographically strong.
Usage:
GUID guid = GUID.v4();
public static GUID v5(GUID namespace, String name)
Usage:
GUID guid = GUID.v5(Uuid.NAMESPACE_DNS, "www.example.com");
namespace - a GUID (optional)name - a stringNullPointerException - if the name is nullpublic static GUID v6()
The clock sequence and node bits are reset to a pseudo-random value for each new UUIDv6 generated.
Usage:
GUID guid = GUID.v6();
public static GUID v7()
Usage:
GUID guid = GUID.v7();
public static boolean valid(String string)
string - a GUID stringpublic byte[] toBytes()
public String toString()
public UUID toUUID()
It simply copies all 128 bits into a new JDK's UUID.
You can think of the GUID class as a JDK's UUID wrapper. This method unwraps a JDK's UUID instance so that you can store it in the built-in format or access its classic interface.
Usage:
UUID uuid = GUID.v4().toUUID();
public int version()
public int hashCode()
public boolean equals(Object other)
public int compareTo(GUID other)
The first of two GUIDs is greater than the second if the most significant byte in which they differ is greater for the first GUID.
If the second GUID is null, then it is treated as a NIL
GUID, which has all its bits set to ZERO, instead of throwing a
NullPointerException.
This method differs from JDK's UUID.compareTo(UUID) as this method
compares two GUIDs as unsigned 128-bit integers.
It can be useful because JDK's UUID.compareTo(UUID) can lead to
unexpected behavior due to its signed 64-bit comparison. Another
reason is that JDK's UUID.compareTo(UUID) throws
NullPointerException if it receives a null UUID.
compareTo in interface Comparable<GUID>other - a second GUID to be compared withthis is less than, equal to, or greater than
thatCopyright © 2024. All rights reserved.