- 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 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()andUUID.fromString(String)in JDK 8.If you prefer a string representation without hyphens, use
Base16Codecinstead ofStringCodec.Base16Codeccan be much faster (22x) than doinguuid.toString().replaceAll("-", "").
-
-
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.protected static char[]toCharArray(String string)Returns a char array of a string.
-
-
-
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 can be much faster than
UUID.toString()in JDK 8.- Specified by:
encodein interfaceUuidCodec<String>- Parameters:
uuid- a UUID- Returns:
- a UUID string
- Throws:
InvalidUuidException- if the argument is invalid
-
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:
decodein interfaceUuidCodec<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
-
-