Class UuidCreator


  • public final class UuidCreator
    extends Object
    Facade to all the UUID generators.
    • Method Detail

      • toBytes

        public static byte[] toBytes​(UUID uuid)
        Get the array of bytes from a UUID.
        Parameters:
        uuid - a UUID
        Returns:
        an array of bytes
      • fromBytes

        public static UUID fromBytes​(byte[] uuid)
        Returns a UUID from a byte array. It validates the input byte array.
        Parameters:
        uuid - a byte array
        Returns:
        a UUID
        Throws:
        InvalidUuidException - if invalid
      • toString

        public static String toString​(UUID uuid)
        Get a string from a UUID. It is much faster than UUID.toString() in JDK 8. In JDK9+ prefer UUID.toString().
        Parameters:
        uuid - a UUID
        Returns:
        a UUID string
      • fromString

        public static UUID fromString​(String string)
        Returns a UUID from a string. It accepts strings: - With URN prefix: "urn:uuid:"; - With curly braces: '{' and '}'; - With upper or lower case; - With or without hyphens. It is much faster than UUID.fromString(String) in JDK 8.
        Parameters:
        string - a UUID string
        Returns:
        a UUID
        Throws:
        InvalidUuidException - if invalid
      • getNil

        public static UUID getNil()
        Returns a Nil UUID. The Nil UUID is special UUID that has all 128 bits set to zero.
        Returns:
        a Nil UUID
      • getRandomBased

        public static UUID getRandomBased()
        Returns a random UUID. The random generator is SecureRandom.
         Details: 
         - Version number: 4
         
        Returns:
        a version 4 UUID
      • getTimeBased

        public static UUID getTimeBased()
        Returns a time-based UUID.
         Details: 
         - Version number: 1
         - Node identifier: random (default) or user defined
         
        A user defined node identifier can be provided by the system property 'uuidcreator.node' or the environment variable 'UUIDCREATOR_NODE'.
        Returns:
        a version 1 UUID
      • getTimeBasedWithMac

        public static UUID getTimeBasedWithMac()
        Returns a time-based UUID with hardware address as node identifier.
         Details: 
         - Version number: 1 
         - Node identifier: MAC
         
        Returns:
        a version 1 UUID
      • getTimeBasedWithHash

        public static UUID getTimeBasedWithHash()
        Returns a time-based UUID with system data hash as node identifier.
         Details: 
         - Version number: 1 
         - Node identifier: system data hash
         
        Returns:
        a version 1 UUID
      • getTimeBased

        public static UUID getTimeBased​(Instant instant,
                                        Integer clockseq,
                                        Long nodeid)
        Returns a time-based UUID.
         Details: 
         - Version number: 1
         - Node identifier: random
         
        Parameters:
        instant - an alternate instant
        clockseq - an alternate clock sequence (0 to 16,383)
        nodeid - an alternate node (0 to 2^48-1)
        Returns:
        a version 1 UUID
      • getTimeBasedWithMac

        public static UUID getTimeBasedWithMac​(Instant instant,
                                               Integer clockseq)
        Returns a time-based UUID with hardware address as node identifier.
         Details: 
         - Version number: 1
         - Node identifier: MAC
         
        Parameters:
        instant - an alternate instant
        clockseq - an alternate clock sequence (0 to 16,383)
        Returns:
        a version 1 UUID
      • getTimeBasedWithHash

        public static UUID getTimeBasedWithHash​(Instant instant,
                                                Integer clockseq)
        Returns a time-based UUID with system data hash as node identifier.
         Details: 
         - Version number: 1
         - Node identifier: system data hash
         
        Parameters:
        instant - an alternate instant
        clockseq - an alternate clock sequence (0 to 16,383)
        Returns:
        a version 1 UUID
      • getTimeOrdered

        public static UUID getTimeOrdered()
        Returns a time-ordered UUID.
         Details: 
         - Version number: 6
         - Node identifier: random (default) or user defined
         
        A user defined node identifier can be provided by the system property 'uuidcreator.node' or the environment variable 'UUIDCREATOR_NODE'.
        Returns:
        a version 6 UUID
      • getTimeOrderedWithMac

        public static UUID getTimeOrderedWithMac()
        Returns a time-ordered UUID with hardware address as node identifier.
         Details: 
         - Version number: 6
         - Node identifier: MAC
         
        Returns:
        a version 6 UUID
      • getTimeOrderedWithHash

        public static UUID getTimeOrderedWithHash()
        Returns a time-ordered UUID with system data hash as node identifier.
         Details: 
         - Version number: 6
         - Node identifier: system data hash
         
        Returns:
        a version 6 UUID
      • getTimeOrdered

        public static UUID getTimeOrdered​(Instant instant,
                                          Integer clockseq,
                                          Long nodeid)
        Returns a time-ordered UUID.
         Details: 
         - Version number: 6
         - Node identifier: random
         
        Parameters:
        instant - an alternate instant
        clockseq - an alternate clock sequence (0 to 16,383)
        nodeid - an alternate node (0 to 2^48-1)
        Returns:
        a version 6 UUID
      • getTimeOrderedWithMac

        public static UUID getTimeOrderedWithMac​(Instant instant,
                                                 Integer clockseq)
        Returns a time-ordered UUID with hardware address as node identifier.
         Details: 
         - Version number: 6
         - Node identifier: MAC
         
        Parameters:
        instant - an alternate instant
        clockseq - an alternate clock sequence (0 to 16,383)
        Returns:
        a version 6 UUID
      • getTimeOrderedWithHash

        public static UUID getTimeOrderedWithHash​(Instant instant,
                                                  Integer clockseq)
        Returns a time-ordered UUID with system data hash as node identifier.
         Details: 
         - Version number: 6
         - Node identifier: system data hash
         
        Parameters:
        instant - an alternate instant
        clockseq - an alternate clock sequence (0 to 16,383)
        Returns:
        a version 6 UUID
      • getNameBasedMd5

        public static UUID getNameBasedMd5​(String name)
        Returns a name-based UUID (MD5).
         Details: 
         - Version number: 3 
         - Hash Algorithm: MD5 
         - Name Space: NO
         
        The name string is encoded into a sequence of bytes using the UTF-8 charset. If you want another charset, use getNameBasedMd5(byte[]) instead.
        Parameters:
        name - a name string
        Returns:
        a version 3 UUID
      • getNameBasedMd5

        public static UUID getNameBasedMd5​(byte[] name)
        Returns a name-based UUID (MD5).
         Details: 
         - Version number: 3 
         - Hash Algorithm: MD5 
         - Name Space: NO
         
        Parameters:
        name - a byte array
        Returns:
        a version 3 UUID
      • getNameBasedMd5

        public static UUID getNameBasedMd5​(UUID namespace,
                                           String name)
        Returns a name-based UUID (MD5).
         Details: 
         - Version number: 3 
         - Hash Algorithm: MD5 
         - Name Space: YES (custom)
         
        The name string is encoded into a sequence of bytes using the UTF-8 charset. If you want another charset, use getNameBasedMd5(UUID, byte[]) instead.
        Parameters:
        namespace - a custom name space UUID
        name - a name string
        Returns:
        a version 3 UUID
      • getNameBasedMd5

        public static UUID getNameBasedMd5​(UUID namespace,
                                           byte[] name)
        Returns a name-based UUID (MD5).
         Details: 
         - Version number: 3 
         - Hash Algorithm: MD5 
         - Name Space: YES (custom)
         
        Parameters:
        namespace - a custom name space UUID
        name - a byte array
        Returns:
        a version 3 UUID
      • getNameBasedMd5

        public static UUID getNameBasedMd5​(String namespace,
                                           String name)
        Returns a name-based UUID (MD5).
         Details: 
         - Version number: 3 
         - Hash Algorithm: MD5 
         - Name Space: YES (custom)
         
        The name string is encoded into a sequence of bytes using the UTF-8 charset. If you want another charset, use getNameBasedMd5(String, byte[]) instead.
        Parameters:
        namespace - a custom name space UUID in string format
        name - a name string
        Returns:
        a version 3 UUID
        Throws:
        InvalidUuidException - if the namespace is invalid
      • getNameBasedMd5

        public static UUID getNameBasedMd5​(String namespace,
                                           byte[] name)
        Returns a name-based UUID (MD5).
         Details: 
         - Version number: 3 
         - Hash Algorithm: MD5 
         - Name Space: YES (custom)
         
        Parameters:
        namespace - a custom name space UUID in string format
        name - a byte array
        Returns:
        a version 3 UUID
        Throws:
        InvalidUuidException - if the namespace is invalid
      • getNameBasedMd5

        public static UUID getNameBasedMd5​(UuidNamespace namespace,
                                           String name)
        Returns a name-based UUID (MD5).
         Details: 
         - Version number: 3 
         - Hash Algorithm: MD5 
         - Name Space: YES (predefined)
         
        The name string is encoded into a sequence of bytes using the UTF-8 charset. If you want another charset, use getNameBasedMd5(UuidNamespace, byte[]) instead.
         Name spaces predefined by RFC-4122 (Appendix C):
         
         - NAMESPACE_DNS: Name string is a fully-qualified domain name;
         - NAMESPACE_URL: Name string is a URL;
         - NAMESPACE_ISO_OID: Name string is an ISO OID;
         - NAMESPACE_X500_DN: Name string is an X.500 DN (in DER or a text format).
         
        See: UuidNamespace.
        Parameters:
        namespace - a predefined name space enumeration
        name - a name string
        Returns:
        a version 3 UUID
      • getNameBasedMd5

        public static UUID getNameBasedMd5​(UuidNamespace namespace,
                                           byte[] name)
        Returns a name-based UUID (MD5).
         Details: 
         - Version number: 3 
         - Hash Algorithm: MD5 
         - Name Space: YES (predefined)
         
         Name spaces predefined by RFC-4122 (Appendix C):
         
         - NAMESPACE_DNS: Name string is a fully-qualified domain name;
         - NAMESPACE_URL: Name string is a URL;
         - NAMESPACE_ISO_OID: Name string is an ISO OID;
         - NAMESPACE_X500_DN: Name string is an X.500 DN (in DER or a text format).
         
        See: UuidNamespace.
        Parameters:
        namespace - a predefined name space enumeration
        name - a byte array
        Returns:
        a version 3 UUID
      • getNameBasedSha1

        public static UUID getNameBasedSha1​(String name)
        Returns a name-based UUID (SHA1).
         Details: 
         - Version number: 5 
         - Hash Algorithm: SHA1 
         - Name Space: NO
         
        The name string is encoded into a sequence of bytes using the UTF-8 charset. If you want another charset, use getNameBasedSha1(byte[]) instead.
        Parameters:
        name - a name string
        Returns:
        a version 5 UUID
      • getNameBasedSha1

        public static UUID getNameBasedSha1​(byte[] name)
        Returns a name-based UUID (SHA1).
         Details: 
         - Version number: 5 
         - Hash Algorithm: SHA1 
         - Name Space: NO
         
        Parameters:
        name - a byte array
        Returns:
        a version 5 UUID
      • getNameBasedSha1

        public static UUID getNameBasedSha1​(UUID namespace,
                                            String name)
        Returns a name-based UUID (SHA1).
         Details: 
         - Version number: 5 
         - Hash Algorithm: SHA1 
         - Name Space: YES (custom)
         
        The name string is encoded into a sequence of bytes using the UTF-8 charset. If you want another charset, use getNameBasedSha1(UUID, byte[]) instead.
        Parameters:
        namespace - a custom name space UUID
        name - a name string
        Returns:
        a version 5 UUID
      • getNameBasedSha1

        public static UUID getNameBasedSha1​(UUID namespace,
                                            byte[] name)
        Returns a name-based UUID (SHA1).
         Details: 
         - Version number: 5 
         - Hash Algorithm: SHA1 
         - Name Space: YES (custom)
         
        Parameters:
        namespace - a custom name space UUID
        name - a byte array
        Returns:
        a version 5 UUID
      • getNameBasedSha1

        public static UUID getNameBasedSha1​(String namespace,
                                            String name)
        Returns a name-based UUID (SHA1).
         Details: 
         - Version number: 5 
         - Hash Algorithm: SHA1 
         - Name Space: YES (custom)
         
        The name string is encoded into a sequence of bytes using the UTF-8 charset. If you want another charset, use getNameBasedSha1(String, byte[]) instead.
        Parameters:
        namespace - a custom name space UUID in string format
        name - a name string
        Returns:
        a version 5 UUID
        Throws:
        InvalidUuidException - if the namespace is invalid
      • getNameBasedSha1

        public static UUID getNameBasedSha1​(String namespace,
                                            byte[] name)
        Returns a name-based UUID (SHA1).
         Details: 
         - Version number: 5 
         - Hash Algorithm: SHA1 
         - Name Space: YES (custom)
         
        Parameters:
        namespace - a custom name space UUID in string format
        name - a byte array
        Returns:
        a version 5 UUID
        Throws:
        InvalidUuidException - if the namespace is invalid
      • getNameBasedSha1

        public static UUID getNameBasedSha1​(UuidNamespace namespace,
                                            String name)
        Returns a name-based UUID (SHA1).
         Details: 
         - Version number: 5 
         - Hash Algorithm: SHA1 
         - Name Space: YES (predefined)
         
        The name string is encoded into a sequence of bytes using the UTF-8 charset. If you want another charset, use getNameBasedSha1(UuidNamespace, byte[]) instead.
         Name spaces predefined by RFC-4122 (Appendix C):
         
         - NAMESPACE_DNS: Name string is a fully-qualified domain name;
         - NAMESPACE_URL: Name string is a URL;
         - NAMESPACE_ISO_OID: Name string is an ISO OID;
         - NAMESPACE_X500_DN: Name string is an X.500 DN (in DER or a text format).
         
        See: UuidNamespace.
        Parameters:
        namespace - a predefined name space enumeration
        name - a name string
        Returns:
        a version 5 UUID
      • getNameBasedSha1

        public static UUID getNameBasedSha1​(UuidNamespace namespace,
                                            byte[] name)
        Returns a name-based UUID (SHA1).
         Details: 
         - Version number: 5 
         - Hash Algorithm: SHA1 
         - Name Space: YES (predefined)
         
         Name spaces predefined by RFC-4122 (Appendix C):
         
         - NAMESPACE_DNS: Name string is a fully-qualified domain name;
         - NAMESPACE_URL: Name string is a URL;
         - NAMESPACE_ISO_OID: Name string is an ISO OID;
         - NAMESPACE_X500_DN: Name string is an X.500 DN (in DER or a text format).
         
        See: UuidNamespace.
        Parameters:
        namespace - a predefined name space enumeration
        name - a byte array
        Returns:
        a version 5 UUID
      • getDceSecurity

        public static UUID getDceSecurity​(byte localDomain,
                                          int localIdentifier)
        Returns a DCE Security UUID. See: UuidLocalDomain.
         Details: 
         - Version number: 2 
         - Node identifier: random
         
        Parameters:
        localDomain - a custom local domain byte
        localIdentifier - a local identifier
        Returns:
        a version 2 UUID
      • getDceSecurityWithMac

        public static UUID getDceSecurityWithMac​(byte localDomain,
                                                 int localIdentifier)
        Returns a DCE Security UUID with hardware address as node identifier. See: UuidLocalDomain.
         Details: 
         - Version number: 2 
         - Node identifier: MAC
         
        Parameters:
        localDomain - a custom local domain byte
        localIdentifier - a local identifier
        Returns:
        a version 2 UUID
      • getDceSecurityWithHash

        public static UUID getDceSecurityWithHash​(byte localDomain,
                                                  int localIdentifier)
        Returns a DCE Security UUID with system data hash as node identifier. See: UuidLocalDomain.
         Details: 
         - Version number: 2 
         - Node identifier: system data hash
         
        Parameters:
        localDomain - a custom local domain byte
        localIdentifier - a local identifier
        Returns:
        a version 2 UUID
      • getDceSecurity

        public static UUID getDceSecurity​(UuidLocalDomain localDomain,
                                          int localIdentifier)
        Returns a DCE Security UUID.
         Details: 
         - Version number: 2 
         - Node identifier: random
         
         Local domains predefined by DCE 1.1 Authentication and Security Services (Chapter 11):
         
         - LOCAL_DOMAIN_PERSON: 0 (interpreted as POSIX UID domain);
         - LOCAL_DOMAIN_GROUP: 1 (interpreted as POSIX GID domain);
         - LOCAL_DOMAIN_ORG: 2.
         
        See: UuidLocalDomain.
        Parameters:
        localDomain - a predefined local domain enumeration
        localIdentifier - a local identifier
        Returns:
        a version 2 UUID
      • getDceSecurityWithMac

        public static UUID getDceSecurityWithMac​(UuidLocalDomain localDomain,
                                                 int localIdentifier)
        Returns a DCE Security UUID with hardware address as node identifier.
         Details: 
         - Version number: 2 
         - Node identifier: MAC
         
         Local domains predefined by DCE 1.1 Authentication and Security Services (Chapter 11):
         
         - LOCAL_DOMAIN_PERSON: 0 (interpreted as POSIX UID domain);
         - LOCAL_DOMAIN_GROUP: 1 (interpreted as POSIX GID domain);
         - LOCAL_DOMAIN_ORG: 2.
         
        See: UuidLocalDomain.
        Parameters:
        localDomain - a predefined local domain enumeration
        localIdentifier - a local identifier
        Returns:
        a version 2 UUID
      • getDceSecurityWithHash

        public static UUID getDceSecurityWithHash​(UuidLocalDomain localDomain,
                                                  int localIdentifier)
        Returns a DCE Security UUID with system data hash as node identifier. See: UuidLocalDomain.
         Details: 
         - Version number: 2 
         - Node identifier: system data hash
         
        Parameters:
        localDomain - a predefined local domain enumeration
        localIdentifier - a local identifier
        Returns:
        a version 2 UUID
      • getPrefixComb

        public static UUID getPrefixComb()
        Returns a Prefix COMB. The creation millisecond is a 6 bytes PREFIX is at the MOST significant bits. Read: The Cost of GUIDs as Primary Keys http://www.informit.com/articles/article.aspx?p=25862
        Returns:
        a GUID
      • getSuffixComb

        public static UUID getSuffixComb()
        Returns a Suffix COMB. The creation millisecond is a 6 bytes SUFFIX is at the LEAST significant bits. Read: The Cost of GUIDs as Primary Keys http://www.informit.com/articles/article.aspx?p=25862
        Returns:
        a GUID
      • getShortPrefixComb

        public static UUID getShortPrefixComb()
        Returns n Short Prefix COMB. The creation minute is a 2 bytes PREFIX is at the MOST significant bits. The prefix wraps around every ~45 days (2^16/60/24 = ~45). Read: Sequential UUID Generators https://www.2ndquadrant.com/en/blog/sequential-uuid-generators/
        Returns:
        a GUID
      • getShortSuffixComb

        public static UUID getShortSuffixComb()
        Returns a Short Suffix COMB. The creation minute is a 2 bytes SUFFIX is at the LEAST significant bits. The suffix wraps around every ~45 days (2^16/60/24 = ~45). Read: Sequential UUID Generators https://www.2ndquadrant.com/en/blog/sequential-uuid-generators/
        Returns:
        a GUID