Class StringCodec

  • All Implemented Interfaces:
    UuidCodec<String>

    public class StringCodec
    extends Object
    implements UuidCodec<String>
    Codec for UUID string representation as defined in the RFC-4122. The string representation, also referred as canonical textual representation, is a string of 32 hexadecimal (base-16) digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 hexadecimal characters and 4 hyphens). This codec decodes (parses) strings in these formats, with/without hyphens: - 00000000-0000-V000-0000-000000000000 (canonical string) - {00000000-0000-V000-0000-000000000000} (MS GUID string) - urn:uuid:00000000-0000-V000-0000-000000000000 (URN UUID string) The encoding and decoding processes may be much faster (5 to 7x) than UUID.toString() and UUID.fromString(String) in JDK 8. If you prefer a string representation without hyphens, use the Base16Codec instead of StringCodec. This other codec may be much faster (10x) than doing uuid.toString().replaceAll("-", "")`. Read: https://tools.ietf.org/html/rfc4122 Read also: https://en.wikipedia.org/wiki/Universally_unique_identifier#Format
    • Field Detail

      • INSTANCE

        public static final StringCodec INSTANCE
        A shared immutable instance.
    • Constructor Detail

      • StringCodec

        public StringCodec()
    • Method Detail

      • decode

        public UUID decode​(String string)
        Get 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 may be much faster than UUID.fromString(String) in JDK 8. In JDK9+ it may be slightly faster.
        Specified by:
        decode in interface UuidCodec<String>
        Parameters:
        string - a UUID string
        Returns:
        a UUID
        Throws:
        InvalidUuidException - if invalid
      • getJavaVersion

        protected static int getJavaVersion()
        Returns the java major version number. See: https://www.oracle.com/java/technologies/javase/naming-and-versions.html
        Returns:
        major version number