Class UUIDConvertor


  • public final class UUIDConvertor
    extends Object
    This class provides utility method to convert different primitives from/to UUID. All the methods in this class are reversible. While this is not a recommended way to generate UUIDs in general, it can be useful in situations where you need to temporarily assign a UUID to an object based on another unique ID.

    For example if you need to convert a legacy object that has a integer or long ID into a new object that uses UUIDs, you could just generate a new UUID for the new object using UUID.randomUUID() but if you need to later link back the legacy object from the new one, you might not have a place to store the legacy ID on the new object. If you use this class's toUUID() methods to convert the legacy object's ID into the new object's UUID, you will be able to later convert back the UUID into the legacy ID. Provided the legacy IDs are unique (at least for the legacy object type), the new UUIDs produced will be as unique.

    Another use case could be to temporarily convert a legacy object into a new object (for example for processing via a new method) and then need to convert the result back into a legacy object. Then you can similarly use the methods in this class to go back and forth between legacy IDs and UUIDs.

    If the legacy ID that you use has more bits than an UUID (i.e. more than 16 bytes, 8 shorts, 2 doubles, 4 floats, 8 characters, 4 integers, or 2 longs), you need to truncate your input first. The library does not do this for you because it cannot now which part of the input would provide the best (i.e. unique) input. For example if you have long Strings that need to be converted to UUIDs, you need to choose 8 characters from these Strings that are guaranteed or very likely to be unique to avoid collisions. Otherwise you should consider using ReproducibleUUIDs instead. It won't produce reversible UUIDs but it will maximize the entropy of the produced UUIDs to avoid collisions. If needed, you can maintain a separate Map from the generated UUIDs to the corresponding original input to achieve reversibility.

    Since:
    1.0.0
    Author:
    Thomas Leplus
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_BYTES
      Maximum number of bytes that can be converted into a UUID.
      static int MAX_CHARS
      Maximum number of characters that can be converted into a UUID.
      static int MAX_DOUBLES
      Maximum number of doubles that can be converted into a UUID.
      static int MAX_FLOATS
      Maximum number of floats that can be converted into a UUID.
      static int MAX_INTS
      Maximum number of integers that can be converted into a UUID.
      static int MAX_LONGS
      Maximum number of longs that can be converted into a UUID.
      static int MAX_SHORTS
      Maximum number of shorts that can be converted into a UUID.
      static int UUID_BYTES
      Number of bytes in a UUID.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] toBytes​(UUID uuid)
      Converts a UUID into an array of 16 bytes.
      static char[] toChars​(UUID uuid)
      Converts a UUID into an array of 8 characters.
      static double[] toDoubles​(UUID uuid)
      Converts a UUID into an array of 2 doubles.
      static float[] toFloats​(UUID uuid)
      Converts a UUID into an array of 4 floats.
      static int[] toInts​(UUID uuid)
      Converts a UUID into an array of 4 integers.
      static long[] toLongs​(UUID uuid)
      Converts a UUID into an array of 2 longs.
      static short[] toShorts​(UUID uuid)
      Converts a UUID into an array of 8 shorts.
      static String toString​(UUID uuid)
      Converts a UUID into a String of 8 characters.
      static UUID toUUID​(byte... bytes)
      Converts an array of up to 16 bytes into an UUID.
      static UUID toUUID​(char... chars)
      Converts an array of up to 8 characters into an UUID.
      static UUID toUUID​(double... doubles)
      Converts an array of up to 2 doubles into an UUID.
      static UUID toUUID​(float... floats)
      Converts an array of up to 4 floats into an UUID.
      static UUID toUUID​(int... ints)
      Converts an array of up to 4 integers into an UUID.
      static UUID toUUID​(long... longs)
      Converts an array of up to 2 longs into an UUID.
      static UUID toUUID​(short... shorts)
      Converts an array of up to 8 shorts into an UUID.
      static UUID toUUID​(String string)
      Converts a String of up to 8 characters into an UUID.
    • Field Detail

      • UUID_BYTES

        public static final int UUID_BYTES
        Number of bytes in a UUID.
        See Also:
        Constant Field Values
      • MAX_BYTES

        public static final int MAX_BYTES
        Maximum number of bytes that can be converted into a UUID.
        See Also:
        Constant Field Values
      • MAX_SHORTS

        public static final int MAX_SHORTS
        Maximum number of shorts that can be converted into a UUID.
        See Also:
        Constant Field Values
      • MAX_DOUBLES

        public static final int MAX_DOUBLES
        Maximum number of doubles that can be converted into a UUID.
        See Also:
        Constant Field Values
      • MAX_FLOATS

        public static final int MAX_FLOATS
        Maximum number of floats that can be converted into a UUID.
        See Also:
        Constant Field Values
      • MAX_CHARS

        public static final int MAX_CHARS
        Maximum number of characters that can be converted into a UUID.
        See Also:
        Constant Field Values
      • MAX_INTS

        public static final int MAX_INTS
        Maximum number of integers that can be converted into a UUID.
        See Also:
        Constant Field Values
      • MAX_LONGS

        public static final int MAX_LONGS
        Maximum number of longs that can be converted into a UUID.
        See Also:
        Constant Field Values
    • Method Detail

      • toBytes

        public static byte[] toBytes​(UUID uuid)
        Converts a UUID into an array of 16 bytes.
        Parameters:
        uuid - the UUID to convert.
        Returns:
        the resulting array.
      • toChars

        public static char[] toChars​(UUID uuid)
        Converts a UUID into an array of 8 characters.
        Parameters:
        uuid - the UUID to convert.
        Returns:
        the resulting array.
      • toDoubles

        public static double[] toDoubles​(UUID uuid)
        Converts a UUID into an array of 2 doubles.
        Parameters:
        uuid - the UUID to convert.
        Returns:
        the resulting array.
      • toFloats

        public static float[] toFloats​(UUID uuid)
        Converts a UUID into an array of 4 floats.
        Parameters:
        uuid - the UUID to convert.
        Returns:
        the resulting array.
      • toInts

        public static int[] toInts​(UUID uuid)
        Converts a UUID into an array of 4 integers.
        Parameters:
        uuid - the UUID to convert.
        Returns:
        the resulting array.
      • toLongs

        public static long[] toLongs​(UUID uuid)
        Converts a UUID into an array of 2 longs.
        Parameters:
        uuid - the UUID to convert.
        Returns:
        the resulting array.
      • toShorts

        public static short[] toShorts​(UUID uuid)
        Converts a UUID into an array of 8 shorts.
        Parameters:
        uuid - the UUID to convert.
        Returns:
        the resulting array.
      • toString

        public static String toString​(UUID uuid)
        Converts a UUID into a String of 8 characters.
        Parameters:
        uuid - the UUID to convert.
        Returns:
        the resulting String.
      • toUUID

        public static UUID toUUID​(byte... bytes)
                           throws ArrayIndexOutOfBoundsException
        Converts an array of up to 16 bytes into an UUID. If the array is shorter than the maximum length, it will be padded with 0s. If the array is longer than the maximum length, this method will throw an ArrayIndexOutOfBoundsException.
        Parameters:
        bytes - the array to convert.
        Returns:
        the resulting UUID.
        Throws:
        ArrayIndexOutOfBoundsException - if the provided array is longer than 16.
      • toUUID

        public static UUID toUUID​(char... chars)
                           throws ArrayIndexOutOfBoundsException
        Converts an array of up to 8 characters into an UUID. If the array is shorter than the maximum length, it will be padded with 0s. If the array is longer than the maximum length, this method will throw an ArrayIndexOutOfBoundsException.
        Parameters:
        chars - the array to convert.
        Returns:
        the resulting UUID.
        Throws:
        ArrayIndexOutOfBoundsException - if the provided array is longer than 8.
      • toUUID

        public static UUID toUUID​(double... doubles)
                           throws ArrayIndexOutOfBoundsException
        Converts an array of up to 2 doubles into an UUID. If the array is shorter than the maximum length, it will be padded with 0s. If the array is longer than the maximum length, this method will throw an ArrayIndexOutOfBoundsException.
        Parameters:
        doubles - the array to convert.
        Returns:
        the resulting UUID.
        Throws:
        ArrayIndexOutOfBoundsException - if the provided array is longer than 2.
      • toUUID

        public static UUID toUUID​(float... floats)
                           throws ArrayIndexOutOfBoundsException
        Converts an array of up to 4 floats into an UUID. If the array is shorter than the maximum length, it will be padded with 0s. If the array is longer than the maximum length, this method will throw an ArrayIndexOutOfBoundsException.
        Parameters:
        floats - the array to convert.
        Returns:
        the resulting UUID.
        Throws:
        ArrayIndexOutOfBoundsException - if the provided array is longer than 4.
      • toUUID

        public static UUID toUUID​(int... ints)
                           throws ArrayIndexOutOfBoundsException
        Converts an array of up to 4 integers into an UUID. If the array is shorter than the maximum length, it will be padded with 0s. If the array is longer than the maximum length, this method will throw an ArrayIndexOutOfBoundsException.
        Parameters:
        ints - the array to convert.
        Returns:
        the resulting UUID.
        Throws:
        ArrayIndexOutOfBoundsException - if the provided array is longer than 4.
      • toUUID

        public static UUID toUUID​(long... longs)
                           throws ArrayIndexOutOfBoundsException
        Converts an array of up to 2 longs into an UUID. If the array is shorter than the maximum length, it will be padded with 0s. If the array is longer than the maximum length, this method will throw an ArrayIndexOutOfBoundsException.
        Parameters:
        longs - the array to convert.
        Returns:
        the resulting UUID.
        Throws:
        ArrayIndexOutOfBoundsException - if the provided array is longer than 2.
      • toUUID

        public static UUID toUUID​(short... shorts)
                           throws ArrayIndexOutOfBoundsException
        Converts an array of up to 8 shorts into an UUID. If the array is shorter than the maximum length, it will be padded with 0s. If the array is longer than the maximum length, this method will throw an ArrayIndexOutOfBoundsException.
        Parameters:
        shorts - the array to convert.
        Returns:
        the resulting UUID.
        Throws:
        ArrayIndexOutOfBoundsException - if the provided array is longer than 8.
      • toUUID

        public static UUID toUUID​(String string)
                           throws ArrayIndexOutOfBoundsException
        Converts a String of up to 8 characters into an UUID. If the String is shorter than the maximum length, it will be padded with 0s. If the String is longer than the maximum length, this method will throw an ArrayIndexOutOfBoundsException.
        Parameters:
        string - the String to convert.
        Returns:
        the resulting UUID.
        Throws:
        ArrayIndexOutOfBoundsException - if the provided String is longer than 8.