Class GUID

  • All Implemented Interfaces:
    Serializable, Comparable<GUID>

    public final class GUID
    extends Object
    implements Serializable, Comparable<GUID>
    A class that represents and generates GUIDs/UUIDs.

    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.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field 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 Summary

      Constructors 
      Constructor Description
      GUID​(byte[] bytes)
      Creates a new GUID.
      GUID​(long mostSignificantBits, long leastSignificantBits)
      Creates a new GUID.
      GUID​(GUID guid)
      Creates a new GUID.
      GUID​(String string)
      Creates a new GUID.
      GUID​(UUID uuid)
      Creates a new GUID.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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 GUID v8​(GUID namespace, String name)
      Returns a name-based unique identifier that uses SHA-256 hashing (UUIDv8).
      static boolean valid​(String string)
      Checks if the GUID string is valid.
      int version()
      Returns the version number of this GUID.
    • Field Detail

      • NIL

        public static final GUID NIL
        A special GUID that has all 128 bits set to ZERO.
      • MAX

        public static final GUID MAX
        A special GUID that has all 128 bits set to ONE.
      • NAMESPACE_DNS

        public static final GUID NAMESPACE_DNS
        Name space to be used when the name string is a fully-qualified domain name.
      • NAMESPACE_URL

        public static final GUID NAMESPACE_URL
        Name space to be used when the name string is a URL.
      • NAMESPACE_OID

        public static final GUID NAMESPACE_OID
        Name space to be used when the name string is an ISO OID.
      • NAMESPACE_X500

        public static final GUID NAMESPACE_X500
        Name space to be used when the name string is an X.500 DN (DER or text).
      • LOCAL_DOMAIN_PERSON

        public static final byte LOCAL_DOMAIN_PERSON
        The principal domain, interpreted as POSIX UID domain on POSIX systems.
        See Also:
        Constant Field Values
      • LOCAL_DOMAIN_GROUP

        public static final byte LOCAL_DOMAIN_GROUP
        The group domain, interpreted as POSIX GID domain on POSIX systems.
        See Also:
        Constant Field Values
      • LOCAL_DOMAIN_ORG

        public static final byte LOCAL_DOMAIN_ORG
        The organization domain, site-defined.
        See Also:
        Constant Field Values
      • GUID_CHARS

        public static final int GUID_CHARS
        Number of characters of a GUID.
        See Also:
        Constant Field Values
      • GUID_BYTES

        public static final int GUID_BYTES
        Number of bytes of a GUID.
        See Also:
        Constant Field Values
    • Constructor Detail

      • GUID

        public GUID​(GUID guid)
        Creates a new GUID.

        Useful to make copies of GUIDs.

        Parameters:
        guid - a GUID
        Throws:
        IllegalArgumentException - if the input is null
      • GUID

        public GUID​(UUID uuid)
        Creates a new GUID.

        Useful to make copies of JDK's UUIDs.

        Parameters:
        uuid - a JDK's UUID
        Throws:
        IllegalArgumentException - if the input is null
      • GUID

        public GUID​(long mostSignificantBits,
                    long leastSignificantBits)
        Creates a new GUID.
        Parameters:
        mostSignificantBits - the first 8 bytes as a long value
        leastSignificantBits - the last 8 bytes as a long value
      • GUID

        public GUID​(byte[] bytes)
        Creates a new GUID.
        Parameters:
        bytes - an array of 16 bytes
        Throws:
        IllegalArgumentException - if bytes are null or its length is not 16
      • GUID

        public GUID​(String string)
        Creates a new GUID.
        Parameters:
        string - a canonical string
        Throws:
        IllegalArgumentException - if the input string is invalid
    • Method Detail

      • v1

        public static GUID v1()
        Returns a gregorian time-based unique identifier (UUIDv1).

        The clock sequence and node bits are reset to a pseudo-random value for each new UUIDv1 generated.

        Usage:

        
         GUID guid = GUID.v1();
         
        Returns:
        a GUID
      • v2

        public static GUID v2​(byte localDomain,
                              int localIdentifier)
        Returns a DCE Security unique identifier (UUIDv2).

        Usage:

        
         GUID guid = GUID.v2(Uuid.LOCAL_DOMAIN_PERSON, 1234567890);
         
        Parameters:
        localDomain - a custom local domain byte
        localIdentifier - a local identifier
        Returns:
        a GUID
      • v3

        public static GUID v3​(GUID namespace,
                              String name)
        Returns a name-based unique identifier that uses MD5 hashing (UUIDv3).

        Usage:

        
         GUID guid = GUID.v3(Uuid.NAMESPACE_DNS, "www.example.com");
         
        Parameters:
        namespace - a GUID
        name - a string
        Returns:
        a GUID
      • v4

        public static GUID v4()
        Returns a random-based unique identifier (UUIDv4).

        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();
         
        Returns:
        a GUID
      • v5

        public static GUID v5​(GUID namespace,
                              String name)
        Returns a name-based unique identifier that uses SHA-1 hashing (UUIDv5).

        Usage:

        
         GUID guid = GUID.v5(Uuid.NAMESPACE_DNS, "www.example.com");
         
        Parameters:
        namespace - a GUID
        name - a string
        Returns:
        a GUID
      • v6

        public static GUID v6()
        Returns a reordered gregorian time-based unique identifier (UUIDv6).

        The clock sequence and node bits are reset to a pseudo-random value for each new UUIDv6 generated.

        Usage:

        
         GUID guid = GUID.v6();
         
        Returns:
        a GUID
      • v7

        public static GUID v7()
        Returns a Unix epoch time-based unique identifier (UUIDv7).

        Usage:

        
         GUID guid = GUID.v7();
         
        Returns:
        a GUID
      • v8

        public static GUID v8​(GUID namespace,
                              String name)
        Returns a name-based unique identifier that uses SHA-256 hashing (UUIDv8).

        Usage:

        
         GUID guid = GUID.v8(Uuid.NAMESPACE_DNS, "www.example.com");
         
        Parameters:
        namespace - a GUID
        name - a string
        Returns:
        a GUID
      • valid

        public static boolean valid​(String string)
        Checks if the GUID string is valid.
        Parameters:
        string - a GUID string
        Returns:
        true if valid, false if invalid
      • toBytes

        public byte[] toBytes()
        Converts the GUID into a byte array.
        Returns:
        an byte array.
      • toString

        public String toString()
        Converts the GUID into a canonical string.
        Overrides:
        toString in class Object
      • toUUID

        public UUID toUUID()
        Converts the GUID into a JDK's UUID.

        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();
         
        Returns:
        a JDK's UUID.
      • version

        public int version()
        Returns the version number of this GUID.
        Returns:
        a version number
      • hashCode

        public int hashCode()
        Returns a hash code value for the GUID.
        Overrides:
        hashCode in class Object
      • equals

        public boolean equals​(Object other)
        Checks if some other GUID is equal to this one.
        Overrides:
        equals in class Object
      • compareTo

        public int compareTo​(GUID other)
        Compares two GUIDs as unsigned 128-bit integers.

        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.

        Specified by:
        compareTo in interface Comparable<GUID>
        Parameters:
        other - a second GUID to be compared with
        Returns:
        -1, 0 or 1 as this is less than, equal to, or greater than that