Class StringCodec

  • All Implemented Interfaces:
    UuidCodec<String>

    public class StringCodec
    extends Object
    implements UuidCodec<String>
    Codec for UUID string representation as defined in 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 can be much faster (7x) than UUID.toString() and UUID.fromString(String) in JDK 8.

    If you prefer a string representation without hyphens, use Base16Codec instead of StringCodec. Base16Codec can be much faster (22x) than doing uuid.toString().replaceAll("-", "").

    • 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 can be much faster than UUID.fromString(String) in JDK 8.

        It also can be twice as fast as UUID.fromString(String) in JDK 11.

        Specified by:
        decode in interface UuidCodec<String>
        Parameters:
        string - a UUID string
        Returns:
        a UUID
        Throws:
        InvalidUuidException - if the argument is invalid
      • toCharArray

        protected static char[] toCharArray​(String string)
        Returns a char array of a string.

        It removes URN prefix and curly braces from the string.

        Parameters:
        string - a string
        Returns:
        a substring
      • getJavaVersion

        protected static int getJavaVersion()
        Returns the java major version number.
        Returns:
        major version number
        See Also:
        JDK Releases