Class Ulid

  • All Implemented Interfaces:
    Serializable, Comparable<Ulid>

    public final class Ulid
    extends Object
    implements Serializable, Comparable<Ulid>
    A class that represents ULIDs.

    ULID is a 128-bit value that has two components:

    • Time component: a number of milliseconds since 1970-01-01 (Unix epoch).
    • Random component: a sequence of 80 random bits generated by a secure random generator.

    ULID has 128-bit compatibility with UUID. Like a UUID, a ULID can also be stored as a 16-byte array.

    Instances of this class are immutable.

    See Also:
    ULID Specification, Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Ulid MAX
      A special ULID that has all 128 bits set to ONE.
      static Ulid MIN
      A special ULID that has all 128 bits set to ZERO.
      static int RANDOM_BYTES
      Number of bytes of the random component of a ULID.
      static int RANDOM_CHARS
      Number of characters of the random component of a ULID.
      static int TIME_BYTES
      Number of bytes of the time component of a ULID.
      static int TIME_CHARS
      Number of characters of the time component of a ULID.
      static int ULID_BYTES
      Number of bytes of a ULID.
      static int ULID_CHARS
      Number of characters of a ULID.
    • Constructor Summary

      Constructors 
      Constructor Description
      Ulid​(long time, byte[] random)
      Creates a new ULID.
      Ulid​(long mostSignificantBits, long leastSignificantBits)
      Creates a new ULID.
      Ulid​(Ulid ulid)
      Creates a new ULID.
    • Field Detail

      • ULID_CHARS

        public static final int ULID_CHARS
        Number of characters of a ULID.
        See Also:
        Constant Field Values
      • TIME_CHARS

        public static final int TIME_CHARS
        Number of characters of the time component of a ULID.
        See Also:
        Constant Field Values
      • RANDOM_CHARS

        public static final int RANDOM_CHARS
        Number of characters of the random component of a ULID.
        See Also:
        Constant Field Values
      • ULID_BYTES

        public static final int ULID_BYTES
        Number of bytes of a ULID.
        See Also:
        Constant Field Values
      • TIME_BYTES

        public static final int TIME_BYTES
        Number of bytes of the time component of a ULID.
        See Also:
        Constant Field Values
      • RANDOM_BYTES

        public static final int RANDOM_BYTES
        Number of bytes of the random component of a ULID.
        See Also:
        Constant Field Values
      • MIN

        public static final Ulid MIN
        A special ULID that has all 128 bits set to ZERO.
      • MAX

        public static final Ulid MAX
        A special ULID that has all 128 bits set to ONE.
    • Constructor Detail

      • Ulid

        public Ulid​(Ulid ulid)
        Creates a new ULID.

        Useful to make copies of ULIDs.

        Parameters:
        ulid - a ULID
      • Ulid

        public Ulid​(long mostSignificantBits,
                    long leastSignificantBits)
        Creates a new ULID.

        If you want to make a copy of a UUID, use from(UUID) instead.

        Parameters:
        mostSignificantBits - the first 8 bytes as a long value
        leastSignificantBits - the last 8 bytes as a long value
      • Ulid

        public Ulid​(long time,
                    byte[] random)
        Creates a new ULID.

        Time parameter is the number of milliseconds since 1970-01-01 (Unix epoch). It must be a positive number not larger than 2^48-1.

        Random parameter must be an array of 10 bytes.

        Parameters:
        time - the the number of milliseconds since 1970-01-01
        random - an array of 10 bytes
        Throws:
        IllegalArgumentException - if time is negative or larger than 2^48-1
        IllegalArgumentException - if random is null or its length is not 10
    • Method Detail

      • fast

        public static Ulid fast()
        Returns a fast new ULID.

        This static method is a quick alternative to UlidCreator.getUlid().

        It employs ThreadLocalRandom which works very well, although not cryptographically strong. It can be useful, for example, for logging.

        Security-sensitive applications that require a cryptographically secure pseudo-random generator should use UlidCreator.getUlid().

        Returns:
        a ULID
        Since:
        5.1.0
      • min

        public static Ulid min​(long time)
        Returns the minimum ULID for a given time.

        The 48 bits of the time component are filled with the given time and the 80 bits of the random component are all set to ZERO.

        For example, the minimum ULID for 2022-02-22 22:22:22.222 is `new Ulid(0x017f2387460e0000L, 0x0000000000000000L)`, where `0x017f2387460e` is the timestamp in hexadecimal.

        It can be useful to find all records before or after a specific timestamp in a table without a `created_at` field.

        Parameters:
        time - the the number of milliseconds since 1970-01-01
        Returns:
        a ULID
        Since:
        5.2.0
      • max

        public static Ulid max​(long time)
        Returns the maximum ULID for a given time.

        The 48 bits of the time component are filled with the given time and the 80 bits or the random component are all set to ONE.

        For example, the maximum ULID for 2022-02-22 22:22:22.222 is `new Ulid(0x017f2387460effffL, 0xffffffffffffffffL)`, where `0x017f2387460e` is the timestamp in hexadecimal.

        It can be useful to find all records before or after a specific timestamp in a table without a `created_at` field.

        Parameters:
        time - the the number of milliseconds since 1970-01-01
        Returns:
        a ULID
        Since:
        5.2.0
      • from

        public static Ulid from​(UUID uuid)
        Converts a UUID into a ULID.
        Parameters:
        uuid - a UUID
        Returns:
        a ULID
      • from

        public static Ulid from​(byte[] bytes)
        Converts a byte array into a ULID.
        Parameters:
        bytes - an array of 16 bytes
        Returns:
        a ULID
        Throws:
        IllegalArgumentException - if bytes are null or its length is not 16
      • from

        public static Ulid from​(String string)
        Converts a canonical string into a ULID.

        The input string must be 26 characters long and must contain only characters from Crockford's base 32 alphabet.

        The first character of the input string must be between 0 and 7.

        Parameters:
        string - a canonical string
        Returns:
        a ULID
        Throws:
        IllegalArgumentException - if the input string is invalid
        See Also:
        Crockford's Base 32
      • toUuid

        public UUID toUuid()
        Convert the ULID into a UUID.

        A ULID has 128-bit compatibility with a UUID.

        If you need a RFC-4122 UUIDv4 do this: Ulid.toRfc4122().toUuid().

        Returns:
        a UUID.
      • toBytes

        public byte[] toBytes()
        Convert the ULID into a byte array.
        Returns:
        an byte array.
      • toString

        public String toString()
        Converts the ULID into a canonical string in upper case.

        The output string is 26 characters long and contains only characters from Crockford's Base 32 alphabet.

        For lower case string, use the shorthand Ulid#toLowerCase(), instead of Ulid#toString()#toLowerCase().

        Overrides:
        toString in class Object
        Returns:
        a ULID string
        See Also:
        Crockford's Base 32
      • toLowerCase

        public String toLowerCase()
        Converts the ULID into a canonical string in lower case.

        The output string is 26 characters long and contains only characters from Crockford's Base 32 alphabet.

        It is a shorthand at least twice as fast as Ulid.toString().toLowerCase().

        Returns:
        a string
        See Also:
        Crockford's Base 32
      • toRfc4122

        public Ulid toRfc4122()
        Converts the ULID into into another ULID that is compatible with UUIDv4.

        The bytes of the returned ULID are compliant with the RFC-4122 version 4.

        If you need a RFC-4122 UUIDv4 do this: Ulid.toRfc4122().toUuid().

        Note: If you use this method, you can not get the original ULID, since it changes 6 bits of it to generate a UUIDv4.

        Returns:
        a ULID
        See Also:
        RFC-4122
      • getInstant

        public Instant getInstant()
        Returns the instant of creation.

        The instant of creation is extracted from the time component.

        Returns:
        the Instant of creation
      • getInstant

        public static Instant getInstant​(String string)
        Returns the instant of creation.

        The instant of creation is extracted from the time component.

        Parameters:
        string - a canonical string
        Returns:
        the Instant of creation
        Throws:
        IllegalArgumentException - if the input string is invalid
      • getTime

        public long getTime()
        Returns the time component as a number.

        The time component is a number between 0 and 2^48-1. It is equivalent to the count of milliseconds since 1970-01-01 (Unix epoch).

        Returns:
        a number of milliseconds
      • getTime

        public static long getTime​(String string)
        Returns the time component as a number.

        The time component is a number between 0 and 2^48-1. It is equivalent to the count of milliseconds since 1970-01-01 (Unix epoch).

        Parameters:
        string - a canonical string
        Returns:
        a number of milliseconds
        Throws:
        IllegalArgumentException - if the input string is invalid
      • getRandom

        public byte[] getRandom()
        Returns the random component as a byte array.

        The random component is an array of 10 bytes (80 bits).

        Returns:
        a byte array
      • getRandom

        public static byte[] getRandom​(String string)
        Returns the random component as a byte array.

        The random component is an array of 10 bytes (80 bits).

        Parameters:
        string - a canonical string
        Returns:
        a byte array
        Throws:
        IllegalArgumentException - if the input string is invalid
      • getMostSignificantBits

        public long getMostSignificantBits()
        Returns the most significant bits as a number.
        Returns:
        a number.
      • getLeastSignificantBits

        public long getLeastSignificantBits()
        Returns the least significant bits as a number.
        Returns:
        a number.
      • increment

        public Ulid increment()
        Returns a new ULID by incrementing the random component of the current ULID.

        Since the random component contains 80 bits:

        • (1) This method can generate up to 1208925819614629174706176 (2^80) ULIDs per millisecond;
        • (2) This method can generate monotonic increasing ULIDs 99.999999999999992% ((2^80 - 10^9) / (2^80)) of the time within a single millisecond interval, considering an unrealistic rate of 1,000,000,000 ULIDs per millisecond.

        Due to (1) and (2), it does not throw the error message recommended by the specification. When an overflow occurs in the random 80 bits, the time component is simply incremented to maintain monotonicity.

        Returns:
        a ULID
      • isValid

        public static boolean isValid​(String string)
        Checks if the input string is valid.

        The input string must be 26 characters long and must contain only characters from Crockford's base 32 alphabet.

        The first character of the input string must be between 0 and 7.

        Parameters:
        string - a canonical string
        Returns:
        true if the input string is valid
        See Also:
        Crockford's Base 32
      • hashCode

        public int hashCode()
        Returns a hash code value for the ULID.
        Overrides:
        hashCode in class Object
      • equals

        public boolean equals​(Object other)
        Checks if some other ULID is equal to this one.
        Overrides:
        equals in class Object
      • compareTo

        public int compareTo​(Ulid that)
        Compares two ULIDs as unsigned 128-bit integers.

        The first of two ULIDs is greater than the second if the most significant byte in which they differ is greater for the first ULID.

        Specified by:
        compareTo in interface Comparable<Ulid>
        Parameters:
        that - a ULID to be compared with
        Returns:
        -1, 0 or 1 as this is less than, equal to, or greater than that