Giter VIP home page Giter VIP logo

uuid-creator's Introduction

UUID Creator

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.

Maven dependency

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.

Modularity

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

How to Use

Library Facade

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();

Library Utilities

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

Library Codecs

This library also provides many codecs for canonical string, byte array, base-n, slugs, etc.

Main codecs

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

Base-N codecs

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

Other codecs

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);

Other identifier generators

Check out the other ID generators.

uuid-creator's People

Contributors

ahmadimt avatar chriscurtin avatar dependabot[bot] avatar fabiolimace avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.