Package com.github.f4b6a3.uuid.codec
Class StringCodec
- java.lang.Object
-
- com.github.f4b6a3.uuid.codec.StringCodec
-
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) thanUUID.toString()andUUID.fromString(String)in JDK 8. If you prefer a string representation without hyphens, use theBase16Codecinstead ofStringCodec. This other codec may be much faster (10x) than doinguuid.toString().replaceAll("-", "")`. Read: https://tools.ietf.org/html/rfc4122 Read also: https://en.wikipedia.org/wiki/Universally_unique_identifier#Format
-
-
Field Summary
Fields Modifier and Type Field Description static StringCodecINSTANCEA shared immutable instance.
-
Constructor Summary
Constructors Constructor Description StringCodec()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description UUIDdecode(String string)Get a UUID from a string.Stringencode(UUID uuid)Get a string from a UUID.protected static intgetJavaVersion()Returns the java major version number.
-
-
-
Field Detail
-
INSTANCE
public static final StringCodec INSTANCE
A shared immutable instance.
-
-
Method Detail
-
encode
public String encode(UUID uuid)
Get a string from a UUID. It may be much faster thanUUID.toString()in JDK 8. In JDK9+ it usesUUID.toString().
-
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 thanUUID.fromString(String)in JDK 8. In JDK9+ it may be slightly faster.- Specified by:
decodein interfaceUuidCodec<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
-
-