- java.lang.Object
-
- com.github.f4b6a3.uuid.codec.StandardStringCodec
-
public class StandardStringCodec extends Object implements UuidCodec<String>
Codec for UUID canonical string as defined in RFC 9562.In the canonical textual representation, the 16 bytes of a UUID are represented as 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:
- 000000000000V0000000000000000000 (hexadecimal string)
- 00000000-0000-0000-0000-000000000000 (THE canonical string)
- {00000000-0000-0000-0000-000000000000} (Microsoft string)
- urn:uuid:00000000-0000-0000-0000-000000000000 (URN 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 ofStandardStringCodec.Base16Codeccan be much faster (22x) than doinguuid.toString().replaceAll("-", "").- See Also:
- RFC 9562
-
-
Field Summary
Fields Modifier and Type Field Description static StandardStringCodecINSTANCEA shared immutable instance.
-
Constructor Summary
Constructors Constructor Description StandardStringCodec()
-
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 Stringmodify(String string)
-
-
-
Field Detail
-
INSTANCE
public static final StandardStringCodec 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
-
-