An implementation of Universal Unique Identifier (UUID). See http://tools.ietf.org/html/rfc4122

The current implementation supports only a JVM backend due to dependencies upon MessageDigest, to produce random bytes, and to implement MD5/SHA-1 hashing, respectively.

According to the standard, the following components make up a UUID :

  • timeLow (8 digits)
  • timeMid (4 digits)
  • timeHiVersion (4 digits)
  • clockSeqHiVariant (2 digits)
  • clockSeqLow (2 digits)
  • node (12 digits)

For example, for the following UUID: 5561de0e-64ad-4d9b-94f2-46926fc44121:

  • timeLow = 5561de0e
  • timeMid = 64ad
  • timeHiVersion = 4d9b
  • clockSeqHiVariant = 94
  • clockSeqLow = f2
  • node = 46926fc44121

Since the above UUID was generated randomly, its version is 4, and is the first digit of the timeHiVersion.

The variant is 2. Despite any variant with a leading bit of 1 being supported, with all variants supported for either backward or future compatibility, only variant 2 is in actual use.

Currently, only versions 3 (MD5 sum), 4 (randomly generated), and 5 (SHA1) are supported.

Usage:

To obtain a UUID from a UUID String:

   UUID? uuidFromString = fromString("01c17b28-380d-48e2-9d6d-5c9f92b3546d");

To generate a UUID version 3 as an MD5 hash of a String with

   UUID uuidVersion3 = uuid3Md5("some string");

To generate a UUID version 5 as an SHA1 hash of a String and namespace with

   UUID uuidVersion5 = uuid5Sha1("some string", fromString("08110431-c913-4e72-b3e6-d3baad15f4db"));

To generate a random UUID version 4:

   UUID uuidVersion4 = uuid4Random();

In addition support functions to convert from/to Java UUIDs.

To obtain a Java UUID from a UUID:

   import java.util {
      JUUID=UUID
   }

   JUUID javaUuid = toJavaUuid(ceylonUuid);

To obtain a UUID from a Java UUID:

   UUID javaUuid = toUuid(javaUuid);

Also, there is a TypedUUID class that subclasses the herd.chayote TypedClass.

   class AccountId(UUID baseValue) extends TypedUUID(baseValue) {}
   class ReferenceId(UUID baseValue) extends TypedUUID(baseValue) {}

   value accountId1 = AccountId(fromString());
   value accountId2 = AccountId(fromString());

   value refId1 = ReferenceId(fromString());
   value refId2 = ReferenceId(fromString());

   assertFalse(accountId1.equals(accountId2);
   assertFalse(accountId1.equals(referenceId1);
   assertFalse(referenceId2.equals(accountId2);
Platform: Java
Packages
herd.uuid

Main outward facing package for the lukedegruchy.ceylon.uuid module

herd.uuid.interop.java

Interoperability functions with Java UUIDs.

Dependencies
ceylon.collection1.2.1
ceylon.interop.java1.2.1
ceylon.io1.2.1
ceylon.random1.2.1
ceylon.test1.2.1
com.vasileff.ceylon.integer641.0.0
herd.chayote0.0.13
java.base8

Main outward facing package for the lukedegruchy.ceylon.uuid module

Functions
determineVariantshared UuidSupportedVariant? determineVariant(Integer variantNumber)

Determine the UuidSupportedVariant from a variant number Integer (ex 2).

Parameters:
  • variantNumber

    Number corrsponding to the variant

determineVersionshared UuidSupportedVersion? determineVersion(Integer versionNumber)

Determine a UuidSupportedVersion from a version number Integer (ex 3).

Parameters:
  • versionNumber

    Number corresponding to the version

fromStringshared UUID? fromString(String uuidString)

Obtain a UUID from a UUID string. If the UUID string is malformed or incorrect in any way including version and variant, null will be returned.

Parameters:
  • uuidString

    UUID as String to parse

uuid3Md5shared UUID uuid3Md5(String name, UUID? namespace = null)

UUID version 3: A UUID generated from MD5 of namespace and name. The namespace parameter is optional and if its argument is not provided a randomly generated (version 4) UUID will be used in its in its place. The UUID generated will overwrite the hex value for version to 3 and variant with the two most significant bits of 1 and 0 for values of either 8, 9, A, or B.

Parameters:
  • name

    Text to hash as UUID

  • namespace = null

    Optional namespace

uuid4Randomshared UUID uuid4Random()

UUID version 4: A random UUID generated from randomly generated bytes using the applicable platform's randomization library.

uuid5Sha1shared UUID uuid5Sha1(String name, UUID? namespace = null)

UUID version 5: A UUID generated from SHA-1 of namespace and name. The namespace parameter is optional and if its argument is not provided a randomly generated (version 4) UUID will be used in its place. The UUID generated will overwrite the hex value for version to 3 and variant with the two most significant bits of 1 and 0 for values of either 8, 9, A, or B.

Parameters:
  • name

    Text to hash as UUID

  • namespace = null

    Optional namespace

Classes
TypedUUIDshared abstract TypedUUID

TypedClass implementation for uniquely typing UUIDs among different domains.

UUIDshared UUID

An implementation of Universal Unique Identifier (UUID). See http://tools.ietf.org/html/rfc4122

The current impelementation supports only a JDK backend due to dependencies upon MessageDigest, to produce random bytes, and to implement MD5/SHA-1 hashing, respectively.

According to the standard, the following components make up a UUID:

  • timeLow (8 digits)
  • timeMid (4 digits)
  • timeHiVersion (4 digits)
  • clockSeqHiVariant (2 digits)
  • clockSeqLow (2 digits)
  • node (12 digits)

For example, for the following UUID: 5561de0e-64ad-4d9b-94f2-46926fc44121:

  • timeLow = 5561de0e
  • timeMid = 64ad
  • timeHiVersion = 4d9b
  • clockSeqHiVariant = 94
  • clockSeqLow = f2
  • node = 46926fc44121

Since the above UUID was generated randomly, its version is 4, and is the first digit of the timeHiVersion.

The variant is 2. Despite any variant with a leading bit of 1 being supported, with all variants supported for either backward of future compatbility, only variant 2 is in actual use.

Currently, only versions 3 (MD5 sum), 4 (randomly generated), and 5 (SHA1) are supported.

UuidSupportedVariantshared UuidSupportedVariant

Enumerated type describing the supported UUID variants by the UUID class.

  • 0 - Reserved, NCS backward compatibility.
  • 2 - DCE variant (curently used by UUID).
  • 6 - Reserved, Microsoft Corporation GUID.
  • 7 - Reserved for future definition.
UuidSupportedVersionshared UuidSupportedVersion

Enumerated type describing the supported UUID versions by the UUID class.

  • 3: UUID generated from MD5 hashing a String and a namespace UUID?
  • 4: UUID generated randomly according to a randomization library
  • 5: UUID generated from SHA1 hashing a String and a namespace UUID?

Versions 1 and 2 are not supported currently.