A Java library for generating and handling RFC-4122 UUIDs.
RFC-4122 UUIDs:
- Version 1: Time-based;
- Version 2: DCE Security;
- Version 3: Name-based with MD5;
- Version 4: Random-based;
- Version 5: Name-based with SHA1;
- Version 6: Time-ordered (proposed).
Non-standard GUIDs:
- Prefix COMB: combination of the creation millisecond (prefix) with random bytes;
- Suffix COMB: combination of the creation millisecond (suffix) with random bytes;
- Short Prefix COMB: combination the creation minute (prefix) with random bytes;
- Short Suffix COMB: combination the creation minute (suffix) with random bytes.
Read the Wiki pages.
Add these lines to your pom.xml
:
<!-- https://search.maven.org/artifact/com.github.f4b6a3/uuid-creator -->
<dependency>
<groupId>com.github.f4b6a3</groupId>
<artifactId>uuid-creator</artifactId>
<version>4.3.1</version>
</dependency>
See more options in maven.org.
Module and bundle names are the same as the root package name.
- JPMS module name:
com.github.f4b6a3.uuid
- OSGi symbolic name:
com.github.f4b6a3.uuid
Create a Random-based UUID:
UUID uuid = UuidCreator.getRandomBased();
Create a Time-based UUID:
// with a static random number as node identifier
UUID uuid = UuidCreator.getTimeBased();
// with a MAC address as node identifier
UUID uuid = UuidCreator.getTimeBasedWithMac();
// with a hash of hostname, MAC and IP as node identifier
UUID uuid = UuidCreator.getTimeBasedWithHash();
// with a changing random number as node identifier
UUID uuid = UuidCreator.getTimeBasedWithRandom();
Create a Time-ordered UUID:
// with a static random number as node identifier
UUID uuid = UuidCreator.getTimeOrdered();
// with a MAC address as node identifier
UUID uuid = UuidCreator.getTimeOrderedWithMac();
// with a hash of hostname, MAC and IP as node identifier
UUID uuid = UuidCreator.getTimeOrderedWithHash();
// with a changing random number as node identifier
UUID uuid = UuidCreator.getTimeOrderedWithRandom();
Create a Name-based with MD5 UUID:
// without namespace
String name = "https://github.com/";
UUID uuid = UuidCreator.getNameBasedMd5(name);
// with namespace
String name = "https://github.com/";
UUID uuid = UuidCreator.getNameBasedMd5(UuidNamespace.NAMESPACE_URL, name);
Create a Name-based with SHA-1 UUID:
// without namespace
String name = "https://github.com/";
UUID uuid = UuidCreator.getNameBasedSha1(name);
// with namespace
String name = "https://github.com/";
UUID uuid = UuidCreator.getNameBasedSha1(UuidNamespace.NAMESPACE_URL, name);
Create a DCE Security UUID:
int localIdentifier = 1701;
UUID uuid = UuidCreator.getDceSecurity(UuidLocalDomain.LOCAL_DOMAIN_PERSON, localIdentifier);
Create a Prefix COMB GUID:
UUID uuid = UuidCreator.getPrefixComb();
Create a Suffix COMB GUID:
UUID uuid = UuidCreator.getSuffixComb();
Create a Short Prefix COMB GUID:
UUID uuid = UuidCreator.getShortPrefixComb();
Create a Short Suffix COMB GUID:
UUID uuid = UuidCreator.getShortSuffixComb();
This library provides many utilities for validation, version checking, information extraction, etc.
Validate a UUID string:
UuidValidator.isValid(uuid);
UuidValidator.validate(uuid); // Throws an exception if INVALID
Check the version of UUID:
UuidUtil.isTimeBased(uuid);
UuidUtil.isTimeOrdered(uuid);
UuidUtil.isRandomBased(uuid);
Extract information from a UUID:
Instant instant = UuidUtil.getInstant(uuid);
int clocksq = UuidUtil.getClockSequence(uuid);
long nodeid = UuidUtil.getNodeIdentifier(uuid);
UuidVersion version = UuidUtil.getVersion(uuid);
UuidVariant variant = UuidUtil.getVariant(uuid);
Extract information from a COMB GUID:
long prefix = UuidUtil.getPrefix(comb); // Unix milliseconds
long suffix = UuidUtil.getSuffix(comb); // Unix milliseconds
Instant instant = UuidUtil.getPrefixInstant(comb);
Instant instant = UuidUtil.getSuffixInstant(comb);
Get the machine ID:
long id = MachineId.getMachineId(); // 0x7bc3cfd7844f46ad (8918200211668420269)
UUID uuid = MachineId.getMachineUuid(); // 7bc3cfd7-844f-46ad-51a9-1aa22d3c427a
This library also provides many codecs for canonical string, byte array, base-n, slugs, etc.
Convert a UUID to and from byte array:
byte[] bytes = BinaryCodec.INSTANCE.encode(uuid);
UUID uuid = BinaryCodec.INSTANCE.decode(bytes);
Convert a UUID to and from canonical string:
String string = StringCodec.INSTANCE.encode(uuid); // 7x faster than `UUID.toString()`
UUID uuid = StringCodec.INSTANCE.decode(string); // 7x faster than `UUID.fromString()`
Convert a UUID to and from URI:
URI uri = UriCodec.INSTANCE.encode(uuid); // 01234567-89AB-4DEF-A123-456789ABCDEF
UUID uuid = UriCodec.INSTANCE.decode(uri); // urn:uuid:01234567-89ab-4def-a123-456789abcdef
There are base-n codecs for base-16, base-32, base-36, base-58, base-62 and base-64.
Custom codecs can be instantiated with BaseNCodec.newInstance(int|String)
.
Convert a UUID to and from base-n:
// 22x faster than `UUID.toString().replaceAll("-", "")`
String string = Base16Codec.INSTANCE.encode(uuid); // 01234567-89AB-4DEF-A123-456789ABCDEF
UUID uuid = Base16Codec.INSTANCE.decode(string); // 0123456789ab4defa123456789abcdef
String string = Base32Codec.INSTANCE.encode(uuid); // 01234567-89AB-4DEF-A123-456789ABCDEF
UUID uuid = Base32Codec.INSTANCE.decode(string); // aerukz4jvng67ijdivtytk6n54
String string = Base58BitcoinCodec.INSTANCE.encode(uuid); // 01234567-89AB-4DEF-A123-456789ABCDEF
UUID uuid = Base58BitcoinCodec.INSTANCE.decode(string); // 199dn6s7UNiX3LyNkQ1Cfx
String string = Base62Codec.INSTANCE.encode(uuid); // 01234567-89AB-4DEF-A123-456789ABCDEF
UUID uuid = Base62Codec.INSTANCE.decode(string); // 0296tiiBY28FKCYq1PVSGd
String string = Base64UrlCodec.INSTANCE.encode(uuid); // 01234567-89AB-4DEF-A123-456789ABCDEF
UUID uuid = Base64UrlCodec.INSTANCE.decode(string); // ASNFZ4mrTe-hI0VniavN7w
Convert a UUID to and from a custom base-n:
// Returns a base-20 string using a CUSTOM radix (20)
UUID uuid = UUID.fromString("01234567-89AB-4DEF-A123-456789ABCDEF");
int radix = 20; // expanded to alphabet "0123456789abcdefghij"
UuidCodec<String> codec = BaseNCodec.newInstance(radix);
String string = codec.encode(uuid); // 00b5740h195313554732654bjhj9e7
// Returns a base-10 string using a CUSTOM alphabet ("0-9")
UUID uuid = UUID.fromString("01234567-89AB-4DEF-A123-456789ABCDEF");
String alphabet = "0-9"; // expanded to alphabet "0123456789"
UuidCodec<String> codec = BaseNCodec.newInstance(alphabet);
String string = codec.encode(uuid); // 001512366075203566477668990085887675887
Convert a UUID to and from Slug:
// uuid: 01234567-89AB-4DEF-A123-456789ABCDEF
// slug: SgEjRWeJq97xI0VniavN7w
String slug = SlugCodec.INSTANCE.encode(uuid);
UUID uuid = SlugCodec.INSTANCE.decode(slug);
Convert a UUID to and from NCName:
// uuid: 01234567-89AB-4DEF-A123-456789ABCDEF
// name: EASNFZ4mr3vEjRWeJq83vK
String name = NcnameCodec.INSTANCE.encode(uuid);
UUID uuid = NcnameCodec.INSTANCE.decode(name);
Convert a UUID to and from .Net Guid:
// Convert time-based (version 1) to .Net Guid
UUID guid = DotNetGuid1Codec.INSTANCE.encode(uuid);
UUID uuid = DotNetGuid1Codec.INSTANCE.decode(guid);
// Convert time-based (version 4) to .Net Guid
UUID guid = DotNetGuid4Codec.INSTANCE.encode(uuid);
UUID uuid = DotNetGuid4Codec.INSTANCE.encode(guid);
Check out the other ID generators.