Class UUIDUtil

java.lang.Object
space.arim.omnibus.util.UUIDUtil

public final class UUIDUtil extends Object
Utility class for manipulating valid UUIDs. Allows conversions between UUIDs, full UUID strings, short UUID strings, and byte arrays.

All methods do not permit null, and may throw NullPointerException otherwise.

A Note on Preconditions
So that it may be used to operate on large volumes of data, this class does not check comprehensive preconditions. It assumes mostly valid representations. Callers are encouraged to validate their own input if necessary.

UUID Forms
This class recognises 3 forms of UUIDs besides java.util.UUID:
1. Full UUID strings. This the common string based representation of a UUID as defined by UUID.toString(). UUIDs in this form may be converted back to a java.util.UUID via the JDK's UUID.fromString(String).
2. Short UUID strings. These are the same as full UUID strings except that they are not hyphenated.
3. Byte arrays. These must be 16 bytes in length. They convey the same data as a pair of long values.

Methods are provided for efficient conversion between:
1. Full UUID strings and short UUID strings.
2. Short UUID strings and java.util.UUID.
3. Byte arrays and java.util.UUID.

Where applicable, conversion methods are designed to be at least as performant as more roundabout approaches. For example, fromShortString(String) should be faster than combining expandShortString(String) and UUID.fromString(String).
Author:
A248
  • Method Details

    • expandShortString

      public static String expandShortString(String shortUuid)
      Expands a shortened version of a UUID to the full string form. Inverse operation of contractFullString(String)
      Parameters:
      shortUuid - the short uuid string
      Returns:
      the full uuid string
      Throws:
      IllegalArgumentException - if shortUuid is not of length 32
    • contractFullString

      public static String contractFullString(String fullUuid)
      Contracts the full form of a UUID string to its shortened form. This is the inverse operation of expandShortString(String), and should be identical to replacing/removing all hyphens in the full uuid string, e.g. fullUuid.replace("-", "") for a valid full UUID string called fullUuid.
      Parameters:
      fullUuid - the full uuid string
      Returns:
      the short uuid string
      Throws:
      IllegalArgumentException - if fullUuid is not of length 36
    • toShortString

      public static String toShortString(UUID uuid)
      Converts a UUID to its short form string representation. This is the inverse operation of fromShortString(String).

      The returned string will always be of length 32.
      Parameters:
      uuid - the UUID
      Returns:
      the short uuid string
    • fromShortString

      public static UUID fromShortString(String shortUuid)
      Converts a short form uuid string to a UUID. This is the inverse operation of toShortString(UUID)
      Parameters:
      shortUuid - the short uuid string
      Returns:
      the UUID
      Throws:
      IllegalArgumentException - if shortUuid is not of length 32
      NumberFormatException - if shortUuid is not a valid short uuid string
    • toByteArray

      public static byte[] toByteArray(UUID uuid)
      Creates a byte array and writes a UUID to it. This would be the inverse operation of fromByteArray(byte[])
      Parameters:
      uuid - the UUID
      Returns:
      the byte array, will always be length 16
    • toByteArray

      public static void toByteArray(UUID uuid, byte[] byteArray, int offset)
      Writes a UUID to a byte array at the specified offset. This would be the inverse operation of fromByteArray(byte[], int)
      Parameters:
      uuid - the UUID
      byteArray - the byte array to write to, must be at least of length (offset + 16)
      offset - the offset in the byte array after which to write bytes
      Throws:
      IndexOutOfBoundsException - if the byte array is not of the right size
    • fromByteArray

      public static UUID fromByteArray(byte[] byteArray)
      Reads a UUID from a byte array. This is the inverse operation of toByteArray(UUID).
      Parameters:
      byteArray - the byte array to read from, must be at least of length 16
      Returns:
      the UUID
      Throws:
      IndexOutOfBoundsException - if the byte array is not of the right size
    • fromByteArray

      public static UUID fromByteArray(byte[] byteArray, int offset)
      Reads a UUID from a byte array with a specified offset. This is the inverse operation of toByteArray(UUID, byte[], int)
      Parameters:
      byteArray - the byte array to read from, must be at least of length (offset + 16)
      offset - the offset after which to begin reading bytes
      Returns:
      the UUID
      Throws:
      IndexOutOfBoundsException - if the byte array is not of the right size