Class UUIDs


  • public class UUIDs
    extends java.lang.Object
    Utilities for working with UUIDs, especially time-based UUIDs.
    Author:
    jjlauer
    • Constructor Summary

      Constructors 
      Constructor Description
      UUIDs()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.UUID fromBytes​(byte[] bytes)  
      static java.util.UUID fromTimeBytes​(byte[] bytes)  
      static long getEpochMillis​(java.util.UUID uuid)
      Extracts the timestamp embedded within a Type 1 time-based UUID and converts it to "unix time" as the number of milliseconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.
      static java.util.UUID setEpochMillis​(java.util.UUID uuid, long epochMillis)
      Creates a new time-based UUID based on the one provided and sets its embedded timestamp to the supplied one.
      static java.util.UUID setTimestamp​(java.util.UUID uuid, long type1Timestamp)
      Creates a new time-based UUID based on the one provided and sets its embedded timestamp to the supplied one.
      static byte[] toBytes​(java.lang.String uuid)  
      static byte[] toBytes​(java.util.UUID uuid)  
      static byte[] toTimeBytes​(java.util.UUID uuid)
      Creates a byte array of a time-based UUID that is suitable for sorting and lexigraphical ordering.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • UUIDs

        public UUIDs()
    • Method Detail

      • fromBytes

        public static java.util.UUID fromBytes​(byte[] bytes)
      • toBytes

        public static byte[] toBytes​(java.lang.String uuid)
      • toBytes

        public static byte[] toBytes​(java.util.UUID uuid)
      • toTimeBytes

        public static byte[] toTimeBytes​(java.util.UUID uuid)
        Creates a byte array of a time-based UUID that is suitable for sorting and lexigraphical ordering. Matches the algorithm in MySQL 8.0's uuid_to_bin() method. https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_uuid-to-bin For example, 6ccd780c-babb-1026-9564-5b8c656024db will return a byte array as 1026babb-6ccd-780c-9564-5b8c656024db. The embedded timestamp is moved to the front.
        Parameters:
        uuid - The time-based UUID
        Returns:
        A byte array representation
      • fromTimeBytes

        public static java.util.UUID fromTimeBytes​(byte[] bytes)
      • getEpochMillis

        public static long getEpochMillis​(java.util.UUID uuid)
        Extracts the timestamp embedded within a Type 1 time-based UUID and converts it to "unix time" as the number of milliseconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970. The same kind of value that System.currentTimeMillis() returns.
        Parameters:
        uuid - The time-based uuid
        Returns:
        The number of epoch milliseconds
      • setEpochMillis

        public static java.util.UUID setEpochMillis​(java.util.UUID uuid,
                                                    long epochMillis)
        Creates a new time-based UUID based on the one provided and sets its embedded timestamp to the supplied one. This method does not guarantee uniqueness since the caller is supplying a timestamp -- so you should only use this method if you will externally verify uniqueness in some other way.
        Parameters:
        uuid - The uuid to use as base
        epochMillis - The timestamp to use based on "unix time"
        Returns:
        A new UUID
      • setTimestamp

        public static java.util.UUID setTimestamp​(java.util.UUID uuid,
                                                  long type1Timestamp)
        Creates a new time-based UUID based on the one provided and sets its embedded timestamp to the supplied one. This method does not guarantee uniqueness since the caller is supplying a timestamp -- so you should only use this method if you will externally verify uniqueness in some other way.
        Parameters:
        uuid - The uuid to use as a base
        type1Timestamp - The type-1 timestamp to use. Type 1 timestamps are NOT epoch time!
        Returns:
        A new UUID